I was thinking to myself about how 2.1 and before did thinkers and how 2.2 made it into thinker lists and thought "wait isn't memory allocated similar to that?" and well here we are.
200 lines
6.0 KiB
C
200 lines
6.0 KiB
C
// SONIC ROBO BLAST 2
|
|
//-----------------------------------------------------------------------------
|
|
// Copyright (C) 1993-1996 by id Software, Inc.
|
|
// Copyright (C) 1998-2000 by DooM Legacy Team.
|
|
// Copyright (C) 1999-2023 by Sonic Team Junior.
|
|
//
|
|
// 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__
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include "doomdef.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
|
|
|
|
//
|
|
// Purge tags
|
|
//
|
|
// Now they are an enum! -- Monster Iestyn 15/02/18
|
|
//
|
|
enum
|
|
{
|
|
// Tags < PU_LEVEL are not purged until freed explicitly.
|
|
PU_STATIC = 0, // static entire execution time
|
|
PU_LUA, // static entire execution time -- used by lua so it doesn't get caught in loops forever
|
|
PU_PERFSTATS, // static between changes to ps_samplesize cvar
|
|
|
|
PU_SOUND, // static while playing
|
|
PU_MUSIC, // static while playing
|
|
|
|
PU_PATCH, // static entire execution time
|
|
PU_PATCH_LOWPRIORITY, // lower priority patch, static until level exited
|
|
PU_PATCH_ROTATED, // rotated patch, static until level exited or WAD added
|
|
PU_PATCH_DATA, // patch data, lifetime depends on the patch that owns it
|
|
PU_SPRITE, // sprite patch, static until WAD added
|
|
PU_HUDGFX, // HUD patch, static until WAD added
|
|
|
|
PU_HWRPATCHINFO, // Hardware GLPatch_t struct for OpenGL texture cache
|
|
PU_HWRPATCHCOLMIPMAP, // Hardware GLMipmap_t struct colormap variation of patch
|
|
PU_HWRMODELTEXTURE, // Hardware model texture
|
|
|
|
PU_HWRCACHE, // static until unlocked
|
|
PU_CACHE, // static until unlocked
|
|
|
|
// Tags s.t. PU_LEVEL <= tag < PU_PURGELEVEL are purged at level start
|
|
PU_LEVEL, // static until level exited
|
|
PU_LEVSPEC, // a special thinker in a level
|
|
PU_HWRPLANE, // 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, // Note: this is never actually used as a tag
|
|
PU_CACHE_UNLOCKED, // Note: unused
|
|
PU_HWRCACHE_UNLOCKED, // 'unlocked' PU_HWRCACHE memory:
|
|
// 'second-level' cache for graphics
|
|
|
|
// stored in hardware format and downloaded as needed
|
|
PU_HWRMODELTEXTURE_UNLOCKED, // 'unlocked' PU_HWRMODELTEXTURE memory
|
|
|
|
NUM_PURGETAGS
|
|
};
|
|
|
|
//
|
|
// Zone memory initialisation
|
|
//
|
|
void Z_Init(void);
|
|
|
|
//
|
|
// Zone memory allocation
|
|
//
|
|
// enable ZDEBUG to get the file + line the functions were called from
|
|
// for ZZ_Alloc, see doomdef.h
|
|
//
|
|
|
|
// Z_Free and alloc with alignment
|
|
#ifdef ZDEBUG
|
|
#define Z_Free(p) Z_Free2(p, __FILE__, __LINE__)
|
|
#define Z_Malloc(s,t,u) Z_Malloc2(s, t, u, __FILE__, __LINE__)
|
|
#define Z_Calloc(s,t,u) Z_Calloc2(s, t, u, __FILE__, __LINE__)
|
|
#define Z_Realloc(p,s,t,u) Z_Realloc2(p,s, t, u, __FILE__, __LINE__)
|
|
void Z_Free2(void *ptr, const char *file, INT32 line);
|
|
void *Z_Malloc2(size_t size, INT32 tag, void *user, const char *file, INT32 line) FUNCALLOC(1);
|
|
void *Z_Calloc2(size_t size, INT32 tag, void *user, const char *file, INT32 line) FUNCALLOC(1);
|
|
void *Z_Realloc2(void *ptr, size_t size, INT32 tag, void *user, const char *file, INT32 line) FUNCALLOC(2);
|
|
#else
|
|
void Z_Free(void *ptr);
|
|
void *Z_Malloc(size_t size, INT32 tag, void *user) FUNCALLOC(1);
|
|
void *Z_Calloc(size_t size, INT32 tag, void *user) FUNCALLOC(1);
|
|
void *Z_Realloc(void *ptr, size_t size, INT32 tag, void *user) FUNCALLOC(2);
|
|
#endif
|
|
|
|
// Free all memory by tag
|
|
// these don't give line numbers for ZDEBUG currently though
|
|
// (perhaps this should be changed in future?)
|
|
#ifdef ZDEBUG
|
|
#define Z_FreeTag(tagnum) Z_FreeTags2(tagnum, tagnum, __FILE__, __LINE__)
|
|
#define Z_FreeTags(tagnum, tagnum2) Z_FreeTags2(tagnum, tagnum2, __FILE__, __LINE__)
|
|
void Z_FreeTags2(INT32 lowtag, INT32 hightag, const char *file, INT32 line);
|
|
#else
|
|
#define Z_FreeTag(tagnum) Z_FreeTags(tagnum, tagnum)
|
|
void Z_FreeTags(INT32 lowtag, INT32 hightag);
|
|
#endif
|
|
|
|
// Iterate memory by tag
|
|
#define Z_IterateTag(tagnum, func) Z_IterateTags(tagnum, tagnum, func)
|
|
void Z_IterateTags(INT32 lowtag, INT32 hightag, boolean (*iterfunc)(void *));
|
|
|
|
//
|
|
// Utility functions
|
|
//
|
|
void Z_CheckMemCleanup(void);
|
|
|
|
#ifdef ZDEBUG
|
|
#define Z_CheckHeap(tag) Z_CheckHeap2(__FILE__, __LINE__)
|
|
void Z_CheckHeap2(const char *file, INT32 line);
|
|
#else
|
|
void Z_CheckHeap(INT32 tag);
|
|
#endif
|
|
|
|
//
|
|
// 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
|
|
//
|
|
#define Z_TagUsage(tagnum) Z_TagsUsage(tagnum, tagnum)
|
|
size_t Z_TagsUsage(INT32 lowtag, INT32 hightag);
|
|
#define Z_TotalUsage() Z_TagsUsage(0, NUM_PURGETAGS-1)
|
|
|
|
//
|
|
// Miscellaneous functions
|
|
//
|
|
char *Z_StrDup(const char *in);
|
|
|
|
//
|
|
// Specialty allocation functions
|
|
//
|
|
size_t Z_LevelPoolUsage(void);
|
|
void *Z_LevelPoolMalloc(size_t size);
|
|
void *Z_LevelPoolCalloc(size_t size);
|
|
void Z_LevelPoolFree(void *p, size_t size);
|
|
|
|
// for use with CLEANUP macro
|
|
FUNCINLINE static ATTRINLINE void Z_Pfree(void *p)
|
|
{
|
|
Z_Free(*(void **)p);
|
|
}
|
|
|
|
// for use with CLEANUP macro
|
|
// not sure where to put this
|
|
FUNCINLINE static ATTRINLINE void pfree(void *p)
|
|
{
|
|
if (*(void **)p)
|
|
{
|
|
free(*(void **)p);
|
|
*(void **)p = NULL;
|
|
}
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
} // extern "C"
|
|
#endif
|
|
|
|
#endif
|