z_zone.h 5.77 KB
Newer Older
Alam Ed Arias committed
1 2 3 4
// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 1998-2000 by DooM Legacy Team.
James R. committed
5
// Copyright (C) 1999-2020 by Sonic Team Junior.
Alam Ed Arias committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
/// \file  z_zone.h
/// \brief Zone Memory Allocation, perhaps NeXT ObjectiveC inspired

#ifndef __Z_ZONE__
#define __Z_ZONE__

#include <stdio.h>
#include "doomtype.h"

#ifdef __GNUC__ // __attribute__ ((X))
#if (__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 3 || (__GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ >= 5)))
#define FUNCALLOC(X) __attribute__((alloc_size(X)))
#endif // odd, it is documented in GCC 4.3.0 but it exists in 4.2.4, at least
#endif

#ifndef FUNCALLOC
#define FUNCALLOC(x)
#endif

//#define ZDEBUG

//
33 34 35 36 37 38 39 40 41 42 43 44
// Purge tags
//
// Now they are an enum! -- Monster Iestyn 15/02/18
//
enum
{
	// Tags < PU_LEVEL are not purged until freed explicitly.
	PU_STATIC                = 1, // static entire execution time
	PU_LUA                   = 2, // static entire execution time -- used by lua so it doesn't get caught in loops forever

	PU_SOUND                 = 11, // static while playing
	PU_MUSIC                 = 12, // static while playing
45 46 47

	PU_PATCH                 = 14, // static entire execution time
	PU_PATCH_LOWPRIORITY     = 15, // lower priority patch, static until level exited
48 49 50 51
	PU_PATCH_ROTATED         = 16, // rotated patch, static until level exited or WAD added
	PU_PATCH_DATA            = 17, // patch data, lifetime depends on the patch that owns it
	PU_SPRITE                = 18, // sprite patch, static until WAD added
	PU_HUDGFX                = 19, // HUD patch, static until WAD added
52 53 54

	PU_HWRPATCHINFO          = 21, // Hardware GLPatch_t struct for OpenGL texture cache
	PU_HWRPATCHCOLMIPMAP     = 22, // Hardware GLMipmap_t struct colormap variation of patch
55
	PU_HWRMODELTEXTURE       = 23, // Hardware model texture
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

	PU_HWRCACHE              = 48, // static until unlocked
	PU_CACHE                 = 49, // static until unlocked

	// Tags s.t. PU_LEVEL <= tag < PU_PURGELEVEL are purged at level start
	PU_LEVEL                 = 50, // static until level exited
	PU_LEVSPEC               = 51, // a special thinker in a level
	PU_HWRPLANE              = 52, // if ZPLANALLOC is enabled in hw_bsp.c, this is used to alloc polygons for OpenGL

	// Tags >= PU_PURGELEVEL are purgable whenever needed
	PU_PURGELEVEL            = 100, // Note: this is never actually used as a tag
	PU_CACHE_UNLOCKED        = 101, // Note: unused
	PU_HWRCACHE_UNLOCKED     = 102, // 'unlocked' PU_HWRCACHE memory:
									// 'second-level' cache for graphics
                                    // stored in hardware format and downloaded as needed
71
	PU_HWRMODELTEXTURE_UNLOCKED = 103, // 'unlocked' PU_HWRMODELTEXTURE memory
72
};
Alam Ed Arias committed
73

74 75 76
//
// Zone memory initialisation
//
Alam Ed Arias committed
77 78
void Z_Init(void);

79
//
80 81 82 83 84
// Zone memory allocation
//
// enable ZDEBUG to get the file + line the functions were called from
// for ZZ_Alloc, see doomdef.h
//
Alam Ed Arias committed
85

86
// Z_Free and alloc with alignment
Alam Ed Arias committed
87
#ifdef ZDEBUG
88 89 90 91
#define Z_Free(p)                 Z_Free2(p, __FILE__, __LINE__)
#define Z_MallocAlign(s,t,u,a)    Z_Malloc2(s, t, u, a, __FILE__, __LINE__)
#define Z_CallocAlign(s,t,u,a)    Z_Calloc2(s, t, u, a, __FILE__, __LINE__)
#define Z_ReallocAlign(p,s,t,u,a) Z_Realloc2(p,s, t, u, a, __FILE__, __LINE__)
Alam Ed Arias committed
92 93 94 95 96 97 98 99
void Z_Free2(void *ptr, const char *file, INT32 line);
void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1);
void *Z_Calloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1);
void *Z_Realloc2(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(2);
#else
void Z_Free(void *ptr);
void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(1);
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);
Alam Ed Arias committed
101 102
#endif

103
// Alloc with no alignment
104 105 106 107
#define Z_Malloc(s,t,u)    Z_MallocAlign(s, t, u, 0)
#define Z_Calloc(s,t,u)    Z_CallocAlign(s, t, u, 0)
#define Z_Realloc(p,s,t,u) Z_ReallocAlign(p, s, t, u, 0)

108 109 110 111 112 113
// Free all memory by tag
// these don't give line numbers for ZDEBUG currently though
// (perhaps this should be changed in future?)
#define Z_FreeTag(tagnum) Z_FreeTags(tagnum, tagnum)
void Z_FreeTags(INT32 lowtag, INT32 hightag);

114 115 116 117
// Iterate memory by tag
#define Z_IterateTag(tagnum, func) Z_IterateTags(tagnum, tagnum, func)
void Z_IterateTags(INT32 lowtag, INT32 hightag, boolean (*iterfunc)(void *));

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
//
// Utility functions
//
void Z_CheckMemCleanup(void);
void Z_CheckHeap(INT32 i);

//
// Zone memory modification
//
// enable PARANOIA to get the file + line the functions were called from
//
#ifdef PARANOIA
#define Z_ChangeTag(p,t) Z_ChangeTag2(p, t, __FILE__, __LINE__)
#define Z_SetUser(p,u)   Z_SetUser2(p, u, __FILE__, __LINE__)
void Z_ChangeTag2(void *ptr, INT32 tag, const char *file, INT32 line);
void Z_SetUser2(void *ptr, void **newuser, const char *file, INT32 line);
#else
void Z_ChangeTag(void *ptr, INT32 tag);
void Z_SetUser(void *ptr, void **newuser);
#endif

//
// Zone memory usage
//
// Note: These give the memory used in bytes,
// shift down by 10 to convert to KB
//
145
#define Z_TagUsage(tagnum) Z_TagsUsage(tagnum, tagnum)
Alam Ed Arias committed
146
size_t Z_TagsUsage(INT32 lowtag, INT32 hightag);
147
#define Z_TotalUsage() Z_TagsUsage(0, INT32_MAX)
Alam Ed Arias committed
148

149 150 151
//
// Miscellaneous functions
//
Alam Ed Arias committed
152
char *Z_StrDup(const char *in);
153
#define Z_Unlock(p) (void)p // TODO: remove this now that NDS code has been removed
Alam Ed Arias committed
154 155

#endif