1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // $Id: z_zone.h 1562 2020-11-29 11:51:00Z wesleyjohnson $
5 //
6 // Copyright (C) 1993-1996 by id Software, Inc.
7 // Copyright (C) 1998-2012 by DooM Legacy Team.
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 //
20 // $Log: z_zone.h,v $
21 // Revision 1.9  2001/03/13 22:14:20  stroggonmeth
22 // Long time no commit. 3D floors, FraggleScript, portals, ect.
23 //
24 // Revision 1.8  2000/11/02 17:50:10  stroggonmeth
25 // Big 3Dfloors & FraggleScript commit!!
26 //
27 // Revision 1.7  2000/10/08 13:30:01  bpereira
28 //
29 // Revision 1.6  2000/10/04 16:19:24  hurdler
30 // Change all those "3dfx names" to more appropriate names
31 //
32 // Revision 1.5  2000/10/02 18:25:46  bpereira
33 // Revision 1.4  2000/07/01 09:23:49  bpereira
34 // Revision 1.3  2000/04/30 10:30:10  bpereira
35 // Revision 1.2  2000/02/27 00:42:11  hurdler
36 // Revision 1.1.1.1  2000/02/22 20:32:32  hurdler
37 // Initial import into CVS (v1.29 pr3)
38 //
39 //
40 // DESCRIPTION:
41 //      Zone Memory Allocation, perhaps NeXT ObjectiveC inspired.
42 //      Remark: this was the only stuff that, according
43 //       to John Carmack, might have been useful for Quake.
44 //
45 //---------------------------------------------------------------------
46 
47 
48 #ifndef Z_ZONE_H
49 #define Z_ZONE_H
50 
51 //#define DEBUG_ZONE
52 
53 #include <stdio.h>
54 #include "doomdef.h"
55   // PARNOIA
56 #include "doomtype.h"
57 
58 
59 //
60 // ZONE MEMORY
61 // PU - purge tags.
62 // Order is important, inequality tests are used.
63 typedef enum
64 {
65 // Internal use tags, do NOT use for user allocations
66   PU_FREE = 0, // free, unallocated block
67   PU_ZONE,     // head of a zone allocation, exclude from some checks
68   PU_INVALID,  // no longer a valid memory block
69 // Do not move PU_INVALID, it is used to protect tags below it.
70 
71 // User allocation tags
72 // Non-purgable tags.
73   PU_STATIC,   // static entire execution time
74   PU_SOUND,    // static while playing
75   PU_MUSIC,    // static while playing
76   PU_DAVE,     // anything else Dave wants static
77   PU_COLORMAP,
78   PU_HWRPATCHINFO,      // Hardware MipPatch_t struct for OpenGl/Glide texture cache
79   PU_HWRPATCHCOLMIPMAP, // Hardware Mipmap_t struct colormap variation of patch
80   PU_LOCK_SB,  // static and protected against change, must use PU_UNLOCK_CACHE
81 // Tags that convert to PU_CACHE at level exit.
82 // Will not override more restrictive existing tag.
83   PU_LUMP,      // Generic temp Lump.
84   PU_IN_USE,    // Temp in use, user degraded to
85 		// PU_CACHE using Z_ChangeTags_To.
86 // Tags purged at level exit.
87   PU_LEVEL,    // static until level exited
88   PU_LEVSPEC,  // a special thinker in a level
89   PU_HWRPLANE,
90 
91 // Tags >= PU_PURGELEVEL are purgable whenever needed.
92   PU_PURGELEVEL, // Tags >= PU_PURGELEVEL are purgable whenever needed.
93   PU_PRIV_CACHE,
94   PU_HWRCACHE,   // 'second-level' cache for graphics stored in hardware format and downloaded as needed
95   PU_CACHE,
96   PU_STALE_CACHE,	// not referenced recently
97   PU_UNLOCK_CACHE,	// set to PU_CACHE, including those PU_LOCK_STATIC
98 
99 // Tag param, conditional on existing tag
100   PU_CACHE_DEFAULT	// set to PU_CACHE, but not when
101      			// already < PU_PURGELEVEL
102 } memtag_e;
103 
104 
105 
106 void    Z_Init (void);
107 void    Z_FreeTags(memtag_e lowtag, memtag_e hightag);
108 void    Z_DumpHeap(memtag_e lowtag, memtag_e hightag);
109 void    Z_FileDumpHeap (FILE *f);
110 void    Z_CheckHeap (int i);
111 
112 #ifdef PARANOIA
113 #define Z_ChangeTag(p,t)   Z_ChangeTag_debug((p), (t), __FILE__, __LINE__)
114 void  Z_ChangeTag_debug (void *ptr, memtag_e chtag, char * fn, int ln);
115 #else
116 void    Z_ChangeTag (void *ptr, memtag_e chtag);
117 #endif
118 
119 // Change all allocations of old_tag to new_tag.
120 void	Z_ChangeTags_To( memtag_e old_tag, memtag_e new_tag );
121 
122 // returns number of bytes allocated for one tag type
123 int     Z_TagUsage(memtag_e usetag);
124 
125 void    Z_FreeMemory (int *realfree, int *cachemem, int *usedmem, int *largefreeblock);
126 
127 #ifdef DEBUG_ZONE
128 #define Z_Free(p) Z_Free2(p,__FILE__,__LINE__)
129 void    Z_Free2 (void *ptr,char *file,int line);
130 #define Z_Malloc(s,t,p) Z_Malloc2(s,t,p,0,__FILE__,__LINE__)
131 #define Z_MallocAlign(s,t,p,a) Z_Malloc2(s,t,p,a,__FILE__,__LINE__)
132 void*   Z_Malloc2 (int reqsize, memtag_e tag, void **user, int alignbits, char *file,int line);
133 #else
134 void    Z_Free (void *ptr);
135 void*   Z_MallocAlign(int reqsize, memtag_e tag, void **user, int alignbits);
136 #define Z_Malloc(s,t,p) Z_MallocAlign(s,t,p,0)
137 #endif
138 
139 char * Z_Strdup(const char *s, memtag_e tag, void **user);
140 
141 // return size of data of this block.
142 int Z_Datasize( void* ptr );
143 
144 /// memblock header
145 typedef struct memblock_s
146 {
147   // [WDJ] only works for int >= 32bit, or else havoc in Z_ALLOC
148   int                 id;     // should be == ZONEID (first field, first to be corrupted if the previous block overflows)
149   int                 size;   // including the header and possibly tiny fragments
150   memtag_e            memtag;    // purgelevel
151 
152   void**              user;   // if the block has a single owner, *user points to the beginning of the data area after header
153   struct memblock_s*  next;
154   struct memblock_s*  prev;
155 #ifdef DEBUG_ZONE
156     char             *ownerfile;
157     int               ownerline;
158 #endif
159 } memblock_t;
160 
161 
162 #ifdef PARANOIA
163 // This would be inline, except that a usage in a define would not resolve.
164 // Return true when the memory block is valid
165 byte  verify_Z_Malloc( void * mp );
166 #endif
167 
168 
169 #endif
170 
171