1 // SONIC ROBO BLAST 2
2 //-----------------------------------------------------------------------------
3 // Copyright (C) 1993-1996 by id Software, Inc.
4 // Copyright (C) 1998-2000 by DooM Legacy Team.
5 // Copyright (C) 1999-2020 by Sonic Team Junior.
6 //
7 // This program is free software distributed under the
8 // terms of the GNU General Public License, version 2.
9 // See the 'LICENSE' file for more details.
10 //-----------------------------------------------------------------------------
11 /// \file  z_zone.h
12 /// \brief Zone Memory Allocation, perhaps NeXT ObjectiveC inspired
13 
14 #ifndef __Z_ZONE__
15 #define __Z_ZONE__
16 
17 #include <stdio.h>
18 #include "doomtype.h"
19 
20 #ifdef __GNUC__ // __attribute__ ((X))
21 #if (__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 3 || (__GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ >= 5)))
22 #define FUNCALLOC(X) __attribute__((alloc_size(X)))
23 #endif // odd, it is documented in GCC 4.3.0 but it exists in 4.2.4, at least
24 #endif
25 
26 #ifndef FUNCALLOC
27 #define FUNCALLOC(x)
28 #endif
29 
30 //#define ZDEBUG
31 
32 //
33 // Purge tags
34 //
35 // Now they are an enum! -- Monster Iestyn 15/02/18
36 //
37 enum
38 {
39 	// Tags < PU_LEVEL are not purged until freed explicitly.
40 	PU_STATIC                = 1, // static entire execution time
41 	PU_LUA                   = 2, // static entire execution time -- used by lua so it doesn't get caught in loops forever
42 
43 	PU_SOUND                 = 11, // static while playing
44 	PU_MUSIC                 = 12, // static while playing
45 
46 	PU_PATCH                 = 14, // static entire execution time
47 	PU_PATCH_LOWPRIORITY     = 15, // lower priority patch, static until level exited
48 	PU_PATCH_ROTATED         = 16, // rotated patch, static until level exited or WAD added
49 	PU_PATCH_DATA            = 17, // patch data, lifetime depends on the patch that owns it
50 	PU_SPRITE                = 18, // sprite patch, static until WAD added
51 	PU_HUDGFX                = 19, // HUD patch, static until WAD added
52 
53 	PU_HWRPATCHINFO          = 21, // Hardware GLPatch_t struct for OpenGL texture cache
54 	PU_HWRPATCHCOLMIPMAP     = 22, // Hardware GLMipmap_t struct colormap variation of patch
55 	PU_HWRMODELTEXTURE       = 23, // Hardware model texture
56 
57 	PU_HWRCACHE              = 48, // static until unlocked
58 	PU_CACHE                 = 49, // static until unlocked
59 
60 	// Tags s.t. PU_LEVEL <= tag < PU_PURGELEVEL are purged at level start
61 	PU_LEVEL                 = 50, // static until level exited
62 	PU_LEVSPEC               = 51, // a special thinker in a level
63 	PU_HWRPLANE              = 52, // if ZPLANALLOC is enabled in hw_bsp.c, this is used to alloc polygons for OpenGL
64 
65 	// Tags >= PU_PURGELEVEL are purgable whenever needed
66 	PU_PURGELEVEL            = 100, // Note: this is never actually used as a tag
67 	PU_CACHE_UNLOCKED        = 101, // Note: unused
68 	PU_HWRCACHE_UNLOCKED     = 102, // 'unlocked' PU_HWRCACHE memory:
69 									// 'second-level' cache for graphics
70                                     // stored in hardware format and downloaded as needed
71 	PU_HWRMODELTEXTURE_UNLOCKED = 103, // 'unlocked' PU_HWRMODELTEXTURE memory
72 };
73 
74 //
75 // Zone memory initialisation
76 //
77 void Z_Init(void);
78 
79 //
80 // Zone memory allocation
81 //
82 // enable ZDEBUG to get the file + line the functions were called from
83 // for ZZ_Alloc, see doomdef.h
84 //
85 
86 // Z_Free and alloc with alignment
87 #ifdef ZDEBUG
88 #define Z_Free(p)                 Z_Free2(p, __FILE__, __LINE__)
89 #define Z_MallocAlign(s,t,u,a)    Z_Malloc2(s, t, u, a, __FILE__, __LINE__)
90 #define Z_CallocAlign(s,t,u,a)    Z_Calloc2(s, t, u, a, __FILE__, __LINE__)
91 #define Z_ReallocAlign(p,s,t,u,a) Z_Realloc2(p,s, t, u, a, __FILE__, __LINE__)
92 void Z_Free2(void *ptr, const char *file, INT32 line);
93 void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1);
94 void *Z_Calloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1);
95 void *Z_Realloc2(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(2);
96 #else
97 void Z_Free(void *ptr);
98 void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(1);
99 void *Z_CallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(1);
100 void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(2);
101 #endif
102 
103 // Alloc with no alignment
104 #define Z_Malloc(s,t,u)    Z_MallocAlign(s, t, u, 0)
105 #define Z_Calloc(s,t,u)    Z_CallocAlign(s, t, u, 0)
106 #define Z_Realloc(p,s,t,u) Z_ReallocAlign(p, s, t, u, 0)
107 
108 // Free all memory by tag
109 // these don't give line numbers for ZDEBUG currently though
110 // (perhaps this should be changed in future?)
111 #define Z_FreeTag(tagnum) Z_FreeTags(tagnum, tagnum)
112 void Z_FreeTags(INT32 lowtag, INT32 hightag);
113 
114 // Iterate memory by tag
115 #define Z_IterateTag(tagnum, func) Z_IterateTags(tagnum, tagnum, func)
116 void Z_IterateTags(INT32 lowtag, INT32 hightag, boolean (*iterfunc)(void *));
117 
118 //
119 // Utility functions
120 //
121 void Z_CheckMemCleanup(void);
122 void Z_CheckHeap(INT32 i);
123 
124 //
125 // Zone memory modification
126 //
127 // enable PARANOIA to get the file + line the functions were called from
128 //
129 #ifdef PARANOIA
130 #define Z_ChangeTag(p,t) Z_ChangeTag2(p, t, __FILE__, __LINE__)
131 #define Z_SetUser(p,u)   Z_SetUser2(p, u, __FILE__, __LINE__)
132 void Z_ChangeTag2(void *ptr, INT32 tag, const char *file, INT32 line);
133 void Z_SetUser2(void *ptr, void **newuser, const char *file, INT32 line);
134 #else
135 void Z_ChangeTag(void *ptr, INT32 tag);
136 void Z_SetUser(void *ptr, void **newuser);
137 #endif
138 
139 //
140 // Zone memory usage
141 //
142 // Note: These give the memory used in bytes,
143 // shift down by 10 to convert to KB
144 //
145 #define Z_TagUsage(tagnum) Z_TagsUsage(tagnum, tagnum)
146 size_t Z_TagsUsage(INT32 lowtag, INT32 hightag);
147 #define Z_TotalUsage() Z_TagsUsage(0, INT32_MAX)
148 
149 //
150 // Miscellaneous functions
151 //
152 char *Z_StrDup(const char *in);
153 #define Z_Unlock(p) (void)p // TODO: remove this now that NDS code has been removed
154 
155 #endif
156