From 44c9b875571ca79ca8513a3f582922bb1b536b39 Mon Sep 17 00:00:00 2001 From: alufolie91 Date: Mon, 28 Aug 2023 00:52:03 +0200 Subject: [PATCH] Revert "Clean up and simplify zone memory allocation" and "Some memory allocations have been properly aligned" Fixes #35 seems like screwing with the extrabytes/alignbits specifically in the zone memory allocations corrupt something in 32bit software mode reverting this fixes the issue but comes with a small performance hit on busy maps --- src/d_netfil.c | 2 +- src/lua_hudlib_drawlist.c | 6 +- src/r_fps.c | 17 +- src/z_zone.c | 502 +++++++++++++++++++------------------- src/z_zone.h | 152 +++++------- 5 files changed, 329 insertions(+), 350 deletions(-) diff --git a/src/d_netfil.c b/src/d_netfil.c index 49c12ce25..0436f05d3 100644 --- a/src/d_netfil.c +++ b/src/d_netfil.c @@ -562,7 +562,7 @@ INT32 CL_CheckFiles(void) CONS_Debug(DBG_NETPLAY, "searching for '%s' ", fileneeded[i].filename); // Check in already loaded files - for (j = mainwads+1; j < numwadfiles; j++) + for (j = mainwads+1; wadfiles[j]; j++) { nameonly(strcpy(wadfilename, wadfiles[j]->filename)); if (!stricmp(wadfilename, fileneeded[i].filename) && diff --git a/src/lua_hudlib_drawlist.c b/src/lua_hudlib_drawlist.c index d36d99dd4..cfadcc30a 100644 --- a/src/lua_hudlib_drawlist.c +++ b/src/lua_hudlib_drawlist.c @@ -84,7 +84,7 @@ huddrawlist_h LUA_HUD_CreateDrawList(void) { huddrawlist_h drawlist; - drawlist = (huddrawlist_h) Z_Calloc(sizeof(struct huddrawlist_s), PU_STATIC, NULL); + drawlist = (huddrawlist_h) Z_CallocAlign(sizeof(struct huddrawlist_s), PU_STATIC, NULL, 64); drawlist->items = NULL; drawlist->items_capacity = 0; drawlist->items_len = 0; @@ -145,7 +145,7 @@ static size_t AllocateDrawItem(huddrawlist_h list) { if (list->items_capacity == 0) list->items_capacity = 128; else list->items_capacity *= 2; - list->items = (drawitem_t *) Z_Realloc(list->items, sizeof(struct drawitem_s) * list->items_capacity, PU_STATIC, NULL); + list->items = (drawitem_t *) Z_ReallocAlign(list->items, sizeof(struct drawitem_s) * list->items_capacity, PU_STATIC, NULL, 64); } return list->items_len++; @@ -164,7 +164,7 @@ static size_t CopyString(huddrawlist_h list, const char* str) { if (list->strbuf_capacity == 0) list->strbuf_capacity = 256; else list->strbuf_capacity *= 2; - list->strbuf = (char*) Z_Realloc(list->strbuf, sizeof(char) * list->strbuf_capacity, PU_STATIC, NULL); + list->strbuf = (char*) Z_ReallocAlign(list->strbuf, sizeof(char) * list->strbuf_capacity, PU_STATIC, NULL, 8); } { diff --git a/src/r_fps.c b/src/r_fps.c index 67865d016..ac315c6a7 100644 --- a/src/r_fps.c +++ b/src/r_fps.c @@ -357,11 +357,12 @@ static void AddInterpolator(levelinterpolator_t* interpolator) levelinterpolators_size *= 2; } - levelinterpolators = Z_Realloc( + levelinterpolators = Z_ReallocAlign( (void*) levelinterpolators, sizeof(levelinterpolator_t*) * levelinterpolators_size, PU_LEVEL, - NULL + NULL, + sizeof(levelinterpolator_t*) * 8 ); } @@ -371,8 +372,11 @@ static void AddInterpolator(levelinterpolator_t* interpolator) static levelinterpolator_t *CreateInterpolator(levelinterpolator_type_e type, thinker_t *thinker) { - levelinterpolator_t *ret = (levelinterpolator_t*) Z_Calloc( - sizeof(levelinterpolator_t), PU_LEVEL, NULL + levelinterpolator_t *ret = (levelinterpolator_t*) Z_CallocAlign( + sizeof(levelinterpolator_t), + PU_LEVEL, + NULL, + sizeof(levelinterpolator_t) * 8 ); ret->type = type; @@ -687,11 +691,12 @@ void R_AddMobjInterpolator(mobj_t *mobj) interpolated_mobjs_capacity *= 2; } - interpolated_mobjs = Z_Realloc( + interpolated_mobjs = Z_ReallocAlign( interpolated_mobjs, sizeof(mobj_t *) * interpolated_mobjs_capacity, PU_LEVEL, - NULL + NULL, + 64 ); } diff --git a/src/z_zone.c b/src/z_zone.c index 0109d21c6..428ca721e 100644 --- a/src/z_zone.c +++ b/src/z_zone.c @@ -1,7 +1,7 @@ // SONIC ROBO BLAST 2 //----------------------------------------------------------------------------- // Copyright (C) 2006 by Graue. -// Copyright (C) 2006-2023 by Sonic Team Junior. +// Copyright (C) 2006-2018 by Sonic Team Junior. // // This program is free software distributed under the // terms of the GNU General Public License, version 2. @@ -27,7 +27,6 @@ #include "doomdef.h" #include "doomstat.h" -#include "r_patch.h" #include "i_system.h" // I_GetFreeMem #include "i_video.h" // rendermode #include "z_zone.h" @@ -50,11 +49,27 @@ static boolean Z_calloc = false; //#define ZDEBUG2 #endif +struct memblock_s; + +typedef struct +{ + struct memblock_s *block; // Describing this memory + UINT32 id; // Should be ZONEID +} ATTRPACK memhdr_t; + +// Some code might want aligned memory. Assume it wants memory n bytes +// aligned -- then we allocate n-1 extra bytes and return a pointer to +// the first byte aligned as requested. +// Thus, "real" is the pointer we get from malloc() and will free() +// later, but "hdr" is where the memhdr_t starts. +// For non-aligned allocations they will be the same. typedef struct memblock_s { + void *real; + memhdr_t *hdr; + void **user; INT32 tag; // purgelevel - UINT32 id; // Should be ZONEID size_t size; // including the header and blocks size_t realsize; // size of real data only @@ -65,41 +80,74 @@ typedef struct memblock_s #endif struct memblock_s *next, *prev; -} memblock_t; +} ATTRPACK memblock_t; -#define MEMORY(x) (void *)((uintptr_t)(x) + sizeof(memblock_t)) -#define MEMBLOCK(x) (memblock_t *)((uintptr_t)(x) - sizeof(memblock_t)) +#ifdef ZDEBUG +#define Ptr2Memblock(s, f) Ptr2Memblock2(s, f, __FILE__, __LINE__) +static memblock_t *Ptr2Memblock2(void *ptr, const char* func, const char *file, INT32 line) +#else +static memblock_t *Ptr2Memblock(void *ptr, const char* func) +#endif +{ + memhdr_t *hdr; + memblock_t *block; + + if (ptr == NULL) + return NULL; + +#ifdef ZDEBUG2 + CONS_Printf("%s %s:%d\n", func, file, line); +#endif + + hdr = (memhdr_t *)((UINT8 *)ptr - sizeof *hdr); + +#ifdef VALGRIND_MAKE_MEM_DEFINED + VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr); +#endif + +#ifdef VALGRIND_MEMPOOL_EXISTS + if (!VALGRIND_MEMPOOL_EXISTS(hdr->block)) + { +#ifdef ZDEBUG + I_Error("%s: bad memblock from %s:%d", func, file, line); +#else + I_Error("%s: bad memblock", func); +#endif + } +#endif + if (hdr->id != ZONEID) + { +#ifdef ZDEBUG + I_Error("%s: wrong id from %s:%d", func, file, line); +#else + I_Error("%s: wrong id", func); +#endif + } + block = hdr->block; +#ifdef VALGRIND_MAKE_MEM_NOACCESS + VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr); +#endif + return block; + +} -// both the head and tail of the zone memory block list static memblock_t head; -// -// Function prototypes -// static void Command_Memfree_f(void); #ifdef ZDEBUG static void Command_Memdump_f(void); #endif -// -------------------------- -// Zone memory initialisation -// -------------------------- - -/** Initialises zone memory. - * Used at game startup. - * - * \sa I_GetFreeMem, Command_Memfree_f, Command_Memdump_f - */ void Z_Init(void) { - size_t total, memfree; + UINT32 total, memfree; memset(&head, 0x00, sizeof(head)); head.next = head.prev = &head; memfree = I_GetFreeMem(&total)>>20; - CONS_Printf("System memory: %sMB - Free: %sMB\n", sizeu1(total>>20), sizeu2(memfree)); + CONS_Printf("System memory: %uMB - Free: %uMB\n", total>>20, memfree); // Note: This allocates memory. Watch out. COM_AddCommand("memfree", Command_Memfree_f); @@ -109,17 +157,6 @@ void Z_Init(void) #endif } - -// ---------------------- -// Zone memory allocation -// ---------------------- - -/** Frees allocated memory. - * - * \param ptr A pointer to allocated memory, - * assumed to have been allocated with Z_Malloc/Z_Calloc. - * \sa Z_FreeTags - */ #ifdef ZDEBUG void Z_Free2(void *ptr, const char *file, INT32 line) #else @@ -135,14 +172,10 @@ void Z_Free(void *ptr) CONS_Debug(DBG_MEMORY, "Z_Free %s:%d\n", file, line); #endif - block = MEMBLOCK(ptr); -#ifdef PARANOIA - if (block->id != ZONEID) #ifdef ZDEBUG - I_Error("Z_Free at %s:%d: wrong id", file, line); + block = Ptr2Memblock2(ptr, "Z_Free", file, line); #else - I_Error("Z_Free: wrong id"); -#endif + block = Ptr2Memblock(ptr, "Z_Free"); #endif #ifdef ZDEBUG @@ -150,9 +183,11 @@ void Z_Free(void *ptr) CONS_Debug(DBG_MEMORY, "Z_Free at %s:%d\n", file, line); #endif +#ifdef HAVE_BLUA // anything that isn't by lua gets passed to lua just in case. if (block->tag != PU_LUA) LUA_InvalidateUserdata(ptr); +#endif // TODO: if zdebugging, make sure no other block has a user // that is about to be freed. @@ -161,27 +196,21 @@ void Z_Free(void *ptr) if (block->user != NULL) *block->user = NULL; -#ifdef VALGRIND_DESTROY_MEMPOOL - VALGRIND_DESTROY_MEMPOOL(block); -#endif + // Free the memory and get rid of the block. + free(block->real); block->prev->next = block->next; block->next->prev = block->prev; free(block); +#ifdef VALGRIND_DESTROY_MEMPOOL + VALGRIND_DESTROY_MEMPOOL(block); +#endif } -/** malloc() that doesn't accept failure. - * - * \param size Amount of memory to be allocated, in bytes. - * \return A pointer to the allocated memory. - */ +// malloc() that doesn't accept failure. static void *xm(size_t size) { const size_t padedsize = size+sizeof (size_t); - void *p; - - if (padedsize < size)/* overflow check */ - I_Error("You are allocating memory too large!"); - p = malloc(padedsize); + void *p = malloc(padedsize); if (p == NULL) { @@ -191,6 +220,10 @@ static void *xm(size_t size) if (p == NULL) { +#if defined (_NDS) | defined (_PSP) + // Temporary-ish debugging measure + Command_Memfree_f(); +#endif I_Error("Out of memory allocating %s bytes", sizeu1(size)); } } @@ -198,18 +231,10 @@ static void *xm(size_t size) return p; } -/** The Z_MallocAlign function. - * Allocates a block of memory, adds it to a linked list so we can keep track of it. - * - * \param size Amount of memory to be allocated, in bytes. - * \param tag Purge tag. - * \param user The address of a pointer to the memory to be allocated. - * When the memory is freed by Z_Free later, - * the pointer at this address will then be automatically set to NULL. - * \param alignbits The alignment of the memory to be allocated, in bits. Can be 0. - * \note You can pass Z_Malloc() a NULL user if the tag is less than PU_PURGELEVEL. - * \sa Z_CallocAlign, Z_ReallocAlign - */ +// Z_Malloc +// You can pass Z_Malloc() a NULL user if the tag is less than +// PU_PURGELEVEL. + #ifdef ZDEBUG void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) @@ -217,67 +242,75 @@ void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) #endif { + size_t extrabytes = (1<next = head.next; block->prev = &head; head.next = block; block->next->prev = block; + block->real = ptr; + block->hdr = hdr; block->tag = tag; block->user = NULL; #ifdef ZDEBUG block->ownerline = line; block->ownerfile = file; #endif - block->size = sizeof (memblock_t) + size; + block->size = blocksize; block->realsize = size; -#ifdef VALGRIND_CREATE_MEMPOOL - VALGRIND_CREATE_MEMPOOL(block, size, Z_calloc); -#endif + hdr->id = ZONEID; + hdr->block = block; - block->id = ZONEID; +#ifdef VALGRIND_MAKE_MEM_NOACCESS + VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr); +#endif if (user != NULL) { block->user = user; - *(void **)user = ptr; + *(void **)user = given; } else if (tag >= PU_PURGELEVEL) I_Error("Z_Malloc: attempted to allocate purgable block " "(size %s) with no user", sizeu1(size)); - return ptr; + return given; } -/** The Z_CallocAlign function. - * Allocates a block of memory, adds it to a linked list so we can keep track of it. - * Unlike Z_MallocAlign, this also initialises the bytes to zero. - * - * \param size Amount of memory to be allocated, in bytes. - * \param tag Purge tag. - * \param user The address of a pointer to the memory to be allocated. - * When the memory is freed by Z_Free later, - * the pointer at this address will then be automatically set to NULL. - * \param alignbits The alignment of the memory to be allocated, in bits. Can be 0. - * \note You can pass Z_Calloc() a NULL user if the tag is less than PU_PURGELEVEL. - * \sa Z_MallocAlign, Z_ReallocAlign - */ #ifdef ZDEBUG void *Z_Calloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) #else @@ -294,26 +327,10 @@ void *Z_CallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) #endif } -/** The Z_ReallocAlign function. - * Reallocates a block of memory with a new size. - * - * \param ptr A pointer to allocated memory, - * assumed to have been allocated with Z_Malloc/Z_Calloc. - * If NULL, this function instead acts as a wrapper for Z_CallocAlign. - * \param size New size of memory block, in bytes. - * If zero, then the memory is freed and NULL is returned. - * \param tag New purge tag. - * \param user The address of a pointer to the memory to be reallocated. - * This can be a different user to the one originally assigned to the memory block. - * \param alignbits The alignment of the memory to be allocated, in bits. Can be 0. - * \return A pointer to the reallocated memory. Can be NULL if memory was freed. - * \note You can pass Z_Realloc() a NULL user if the tag is less than PU_PURGELEVEL. - * \sa Z_MallocAlign, Z_CallocAlign - */ #ifdef ZDEBUG void *Z_Realloc2(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) #else -void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits) +void *Z_ReallocAlign(void *ptr, size_t size,INT32 tag, void *user, INT32 alignbits) #endif { void *rez; @@ -339,14 +356,10 @@ void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignb #endif } - block = MEMBLOCK(ptr); -#ifdef PARANOIA - if (block->id != ZONEID) #ifdef ZDEBUG - I_Error("Z_ReallocAlign at %s:%d: wrong id", file, line); + block = Ptr2Memblock2(ptr, "Z_Realloc", file, line); #else - I_Error("Z_ReallocAlign: wrong id"); -#endif + block = Ptr2Memblock(ptr, "Z_Realloc"); #endif if (block == NULL) @@ -384,11 +397,6 @@ void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignb return rez; } -/** Frees all memory for a given set of tags. - * - * \param lowtag The lowest tag to consider. - * \param hightag The highest tag to consider. - */ void Z_FreeTags(INT32 lowtag, INT32 hightag) { memblock_t *block, *next; @@ -397,57 +405,28 @@ void Z_FreeTags(INT32 lowtag, INT32 hightag) for (block = head.next; block != &head; block = next) { next = block->next; // get link before freeing + if (block->tag >= lowtag && block->tag <= hightag) - Z_Free(MEMORY(block)); + Z_Free((UINT8 *)block->hdr + sizeof *block->hdr); } } -/** Iterates through all memory for a given set of tags. - * - * \param lowtag The lowest tag to consider. - * \param hightag The highest tag to consider. - * \param iterfunc The iterator function. - */ -void Z_IterateTags(INT32 lowtag, INT32 hightag, boolean (*iterfunc)(void *)) -{ - memblock_t *block, *next; +// +// Z_CheckMemCleanup +// +// TODO: Currently blocks >= PU_PURGELEVEL are freed every +// CLEANUPCOUNT. It might be better to keep track of +// the total size of all purgable memory and free it when the +// size exceeds some value. +// +// This was in Z_Malloc, but was freeing data at +// unsafe times. Now it is only called when it is safe +// to cleanup memory. - if (!iterfunc) - I_Error("Z_IterateTags: no iterator function was given"); - - for (block = head.next; block != &head; block = next) - { - next = block->next; // get link before possibly freeing - - if (block->tag >= lowtag && block->tag <= hightag) - { - void *mem = MEMORY(block); - boolean free = iterfunc(mem); - if (free) - Z_Free(mem); - } - } -} - -// ----------------- -// Utility functions -// ----------------- - -// starting value of nextcleanup #define CLEANUPCOUNT 2000 -// number of function calls left before next cleanup static INT32 nextcleanup = CLEANUPCOUNT; -/** This was in Z_Malloc, but was freeing data at - * unsafe times. Now it is only called when it is safe - * to cleanup memory. - * - * \todo Currently blocks >= PU_PURGELEVEL are freed every - * CLEANUPCOUNT. It might be better to keep track of - * the total size of all purgable memory and free it when the - * size exceeds some value. - */ void Z_CheckMemCleanup(void) { if (nextcleanup-- == 0) @@ -466,13 +445,15 @@ void Z_CheckMemCleanup(void) void Z_CheckHeap(INT32 i) { memblock_t *block; + memhdr_t *hdr; UINT32 blocknumon = 0; void *given; for (block = head.next; block != &head; block = block->next) { blocknumon++; - given = MEMORY(block); + hdr = block->hdr; + given = (UINT8 *)hdr + sizeof *hdr; #ifdef ZDEBUG2 CONS_Debug(DBG_MEMORY, "block %u owned by %s:%d\n", blocknumon, block->ownerfile, block->ownerline); @@ -488,7 +469,7 @@ void Z_CheckHeap(INT32 i) #ifdef ZDEBUG , block->ownerfile, block->ownerline #endif - ); + ); } #endif if (block->user != NULL && *(block->user) != given) @@ -501,7 +482,7 @@ void Z_CheckHeap(INT32 i) #ifdef ZDEBUG , block->ownerfile, block->ownerline #endif - ); + ); } if (block->next->prev != block) { @@ -513,7 +494,7 @@ void Z_CheckHeap(INT32 i) #ifdef ZDEBUG , block->ownerfile, block->ownerline #endif - ); + ); } if (block->prev->next != block) { @@ -525,9 +506,25 @@ void Z_CheckHeap(INT32 i) #ifdef ZDEBUG , block->ownerfile, block->ownerline #endif - ); + ); } - if (block->id != ZONEID) +#ifdef VALGRIND_MAKE_MEM_DEFINED + VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr); +#endif + if (hdr->block != block) + { + I_Error("Z_CheckHeap %d: block %u" +#ifdef ZDEBUG + "(owned by %s:%d)" +#endif + " doesn't have linkback from allocated memory", + i, blocknumon +#ifdef ZDEBUG + , block->ownerfile, block->ownerline +#endif + ); + } + if (hdr->id != ZONEID) { I_Error("Z_CheckHeap %d: block %u" #ifdef ZDEBUG @@ -537,37 +534,50 @@ void Z_CheckHeap(INT32 i) #ifdef ZDEBUG , block->ownerfile, block->ownerline #endif - ); + ); } +#ifdef VALGRIND_MAKE_MEM_NOACCESS + VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr); +#endif } } -// ------------------------ -// Zone memory modification -// ------------------------ - -/** Changes a memory block's purge tag. - * - * \param ptr A pointer to allocated memory, - * assumed to have been allocated with Z_Malloc/Z_Calloc. - * \param tag The new tag. - * \sa Z_SetUser - */ #ifdef PARANOIA void Z_ChangeTag2(void *ptr, INT32 tag, const char *file, INT32 line) #else -void Z_ChangeTag(void *ptr, INT32 tag) +void Z_ChangeTag2(void *ptr, INT32 tag) #endif { memblock_t *block; + memhdr_t *hdr; if (ptr == NULL) return; - block = MEMBLOCK(ptr); + hdr = (memhdr_t *)((UINT8 *)ptr - sizeof *hdr); +#ifdef VALGRIND_MAKE_MEM_DEFINED + VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr); +#endif + +#ifdef VALGRIND_MEMPOOL_EXISTS + if (!VALGRIND_MEMPOOL_EXISTS(hdr->block)) + { #ifdef PARANOIA - if (block->id != ZONEID) I_Error("Z_ChangeTag at %s:%d: wrong id", file, line); + I_Error("Z_CT at %s:%d: bad memblock", file, line); +#else + I_Error("Z_CT: bad memblock"); +#endif + } +#endif +#ifdef PARANOIA + if (hdr->id != ZONEID) I_Error("Z_CT at %s:%d: wrong id", file, line); +#endif + + block = hdr->block; + +#ifdef VALGRIND_MAKE_MEM_NOACCESS + VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr); #endif if (tag >= PU_PURGELEVEL && block->user == NULL) @@ -577,48 +587,12 @@ void Z_ChangeTag(void *ptr, INT32 tag) block->tag = tag; } -/** Changes a memory block's user. - * - * \param ptr A pointer to allocated memory, - * assumed to have been allocated with Z_Malloc/Z_Calloc. - * \param newuser The new user for the memory block. - * \sa Z_ChangeTag - */ -#ifdef PARANOIA -void Z_SetUser2(void *ptr, void **newuser, const char *file, INT32 line) -#else -void Z_SetUser(void *ptr, void **newuser) -#endif -{ - memblock_t *block; - - if (ptr == NULL) - return; - - block = MEMBLOCK(ptr); - -#ifdef PARANOIA - if (block->id != ZONEID) I_Error("Z_SetUser at %s:%d: wrong id", file, line); -#endif - - if (block->tag >= PU_PURGELEVEL && newuser == NULL) - I_Error("Internal memory management error: " - "tried to make block purgable but it has no owner"); - - block->user = (void*)newuser; - *newuser = ptr; -} - -// ----------------- -// Zone memory usage -// ----------------- - /** Calculates memory usage for a given set of tags. - * * \param lowtag The lowest tag to consider. * \param hightag The highest tag to consider. * \return Number of bytes currently allocated in the heap for the * given tags. + * \sa Z_TagUsage */ size_t Z_TagsUsage(INT32 lowtag, INT32 hightag) { @@ -635,54 +609,45 @@ size_t Z_TagsUsage(INT32 lowtag, INT32 hightag) return cnt; } -// ----------------------- -// Miscellaneous functions -// ----------------------- - -/** The function called by the "memfree" console command. - * Prints the memory being used by each part of the game to the console. - */ -static void Command_Memfree_f(void) +size_t Z_TagUsage(INT32 tagnum) { - size_t freebytes, totalbytes; + return Z_TagsUsage(tagnum, tagnum); +} + +void Command_Memfree_f(void) +{ + UINT32 freebytes, totalbytes; Z_CheckHeap(-1); CONS_Printf("\x82%s", M_GetText("Memory Info\n")); - CONS_Printf(M_GetText("Total heap used : %7s KB\n"), sizeu1(Z_TotalUsage()>>10)); - CONS_Printf(M_GetText("Static : %7s KB\n"), sizeu1(Z_TagUsage(PU_STATIC)>>10)); - CONS_Printf(M_GetText("Static (sound) : %7s KB\n"), sizeu1(Z_TagUsage(PU_SOUND)>>10)); - CONS_Printf(M_GetText("Static (music) : %7s KB\n"), sizeu1(Z_TagUsage(PU_MUSIC)>>10)); - CONS_Printf(M_GetText("Locked cache : %7s KB\n"), sizeu1(Z_TagUsage(PU_CACHE)>>10)); - CONS_Printf(M_GetText("Level : %7s KB\n"), sizeu1(Z_TagUsage(PU_LEVEL)>>10)); - CONS_Printf(M_GetText("Special thinker : %7s KB\n"), sizeu1(Z_TagUsage(PU_LEVSPEC)>>10)); - CONS_Printf(M_GetText("All purgable : %7s KB\n"), + CONS_Printf(M_GetText("Total heap used : %7s KB\n"), sizeu1(Z_TagsUsage(0, INT32_MAX)>>10)); + CONS_Printf(M_GetText("Static : %7s KB\n"), sizeu1(Z_TagUsage(PU_STATIC)>>10)); + CONS_Printf(M_GetText("Static (sound) : %7s KB\n"), sizeu1(Z_TagUsage(PU_SOUND)>>10)); + CONS_Printf(M_GetText("Static (music) : %7s KB\n"), sizeu1(Z_TagUsage(PU_MUSIC)>>10)); + CONS_Printf(M_GetText("Locked cache : %7s KB\n"), sizeu1(Z_TagUsage(PU_CACHE)>>10)); + CONS_Printf(M_GetText("Level : %7s KB\n"), sizeu1(Z_TagUsage(PU_LEVEL)>>10)); + CONS_Printf(M_GetText("Special thinker : %7s KB\n"), sizeu1(Z_TagUsage(PU_LEVSPEC)>>10)); + CONS_Printf(M_GetText("All purgable : %7s KB\n"), sizeu1(Z_TagsUsage(PU_PURGELEVEL, INT32_MAX)>>10)); #ifdef HWRENDER if (rendermode != render_soft && rendermode != render_none) { - CONS_Printf(M_GetText("Patch info headers : %7s KB\n"), sizeu1(Z_TagUsage(PU_HWRPATCHINFO)>>10)); - CONS_Printf(M_GetText("Cached textures : %7s KB\n"), sizeu1(Z_TagUsage(PU_HWRCACHE)>>10)); - CONS_Printf(M_GetText("Texture colormaps : %7s KB\n"), sizeu1(Z_TagUsage(PU_HWRPATCHCOLMIPMAP)>>10)); - CONS_Printf(M_GetText("Plane polygons : %7s KB\n"), sizeu1(Z_TagUsage(PU_HWRPLANE)>>10)); - CONS_Printf(M_GetText("All GPU textures : %7d KB\n"), HWR_GetTextureUsed()>>10); + CONS_Printf(M_GetText("Patch info headers: %7s KB\n"), sizeu1(Z_TagUsage(PU_HWRPATCHINFO)>>10)); + CONS_Printf(M_GetText("Mipmap patches : %7s KB\n"), sizeu1(Z_TagUsage(PU_HWRPATCHCOLMIPMAP)>>10)); + CONS_Printf(M_GetText("HW Texture cache : %7s KB\n"), sizeu1(Z_TagUsage(PU_HWRCACHE)>>10)); + CONS_Printf(M_GetText("Plane polygons : %7s KB\n"), sizeu1(Z_TagUsage(PU_HWRPLANE)>>10)); + CONS_Printf(M_GetText("All GPU textures : %7d KB\n"), sizeu1(Z_TagUsage(HWR_GetTextureUsed())>>10)); } #endif CONS_Printf("\x82%s", M_GetText("System Memory Info\n")); freebytes = I_GetFreeMem(&totalbytes); - CONS_Printf(M_GetText(" Total physical memory: %s KB\n"), sizeu1(totalbytes>>10)); - CONS_Printf(M_GetText("Available physical memory: %s KB\n"), sizeu1(freebytes>>10)); + CONS_Printf(M_GetText(" Total physical memory: %7u KB\n"), totalbytes>>10); + CONS_Printf(M_GetText("Available physical memory: %7u KB\n"), freebytes>>10); } - - #ifdef ZDEBUG -/** The function called by the "memdump" console command. - * Prints zone memory debugging information (i.e. tag, size, location in code allocated). - * Can be all memory allocated in game, or between a set of tags (if -min/-max args used). - * This command is available only if ZDEBUG is enabled. - */ static void Command_Memdump_f(void) { memblock_t *block; @@ -704,12 +669,45 @@ static void Command_Memdump_f(void) } #endif -/** Creates a copy of a string. - * - * \param s The string to be copied. - * \return A copy of the string, allocated in zone memory. - */ +// Creates a copy of a string. char *Z_StrDup(const char *s) { return strcpy(ZZ_Alloc(strlen(s) + 1), s); } + + +#ifdef PARANOIA +void Z_SetUser2(void *ptr, void **newuser, const char *file, INT32 line) +#else +void Z_SetUser2(void *ptr, void **newuser) +#endif +{ + memblock_t *block; + memhdr_t *hdr; + + if (ptr == NULL) + return; + + hdr = (memhdr_t *)((UINT8 *)ptr - sizeof *hdr); + +#ifdef VALGRIND_MAKE_MEM_DEFINED + VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr); +#endif + +#ifdef PARANOIA + if (hdr->id != ZONEID) I_Error("Z_CT at %s:%d: wrong id", file, line); +#endif + + block = hdr->block; + +#ifdef VALGRIND_MAKE_MEM_NOACCESS + VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr); +#endif + + if (block->tag >= PU_PURGELEVEL && newuser == NULL) + I_Error("Internal memory management error: " + "tried to make block purgable but it has no owner"); + + block->user = (void*)newuser; + *newuser = ptr; +} diff --git a/src/z_zone.h b/src/z_zone.h index 91743cfae..638d5f4b6 100644 --- a/src/z_zone.h +++ b/src/z_zone.h @@ -2,7 +2,7 @@ //----------------------------------------------------------------------------- // Copyright (C) 1993-1996 by id Software, Inc. // Copyright (C) 1998-2000 by DooM Legacy Team. -// Copyright (C) 1999-2023 by Sonic Team Junior. +// Copyright (C) 1999-2018 by Sonic Team Junior. // // This program is free software distributed under the // terms of the GNU General Public License, version 2. @@ -15,7 +15,6 @@ #define __Z_ZONE__ #include -#include "doomdef.h" #include "doomtype.h" #ifdef __GNUC__ // __attribute__ ((X)) @@ -31,120 +30,97 @@ //#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 = 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_PERFSTATS = 3, // static between changes to ps_samplesize cvar +// ZONE MEMORY +// PU - purge tags. +// Tags < PU_LEVEL are not purged until freed explicitly. +#define PU_STATIC 1 // static entire execution time +#define PU_LUA 2 // static entire execution time -- used by lua so it doesn't get caught in loops forever +#define PU_PERFSTATS 3 // static between changes to ps_samplesize cvar - PU_SOUND = 11, // static while playing - PU_MUSIC = 12, // static while playing - PU_HUDGFX = 13, // static until WAD added +#define PU_SOUND 11 // static while playing +#define PU_MUSIC 12 // static while playing +#define PU_HUDGFX 13 // static until WAD added - PU_HWRPATCHINFO = 21, // Hardware GLPatch_t struct for OpenGL texture cache - PU_HWRPATCHCOLMIPMAP = 22, // Hardware GLMipmap_t struct colormap variation of patch +#define PU_HWRPATCHINFO 21 // Hardware GLPatch_t struct for OpenGL texture cache +#define PU_HWRPATCHCOLMIPMAP 22 // Hardware GLMipmap_t struct colromap variation of patch - PU_HWRCACHE = 48, // static until unlocked - PU_CACHE = 49, // static until unlocked +#define PU_HWRCACHE 48 // static until unlocked +#define 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 s.t. PU_LEVEL <= tag < PU_PURGELEVEL are purged at level start +#define PU_LEVEL 50 // static until level exited +#define PU_LEVSPEC 51 // a special thinker in a level +#define PU_HWRPLANE 52 - // 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 - PU_HWRPATCHINFO_UNLOCKED = 103, // 'unlocked' PU_HWRMODELTEXTURE memory -}; +// Tags >= PU_PURGELEVEL are purgable whenever needed +#define PU_PURGELEVEL 100 +#define PU_CACHE_UNLOCKED 101 +#define PU_HWRCACHE_UNLOCKED 102 // 'second-level' cache for graphics + // stored in hardware format and downloaded as needed +#define PU_HWRPATCHINFO_UNLOCKED 103 -// -// Zone memory initialisation -// void Z_Init(void); +void Z_FreeTags(INT32 lowtag, INT32 hightag); +void Z_CheckMemCleanup(void); +void Z_CheckHeap(INT32 i); +#ifdef PARANOIA +void Z_ChangeTag2(void *ptr, INT32 tag, const char *file, INT32 line); +#else +void Z_ChangeTag2(void *ptr, INT32 tag); +#endif -// -// Zone memory allocation -// -// enable ZDEBUG to get the file + line the functions were called from -// for ZZ_Alloc, see doomdef.h -// +#ifdef PARANOIA +void Z_SetUser2(void *ptr, void **newuser, const char *file, INT32 line); +#else +void Z_SetUser2(void *ptr, void **newuser); +#endif -// Z_Free and alloc with alignment #ifdef ZDEBUG -#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__) +#define Z_Free(p) Z_Free2(p, __FILE__, __LINE__) void Z_Free2(void *ptr, const char *file, INT32 line); +#define Z_Malloc(s,t,u) Z_Malloc2(s, t, u, 0, __FILE__, __LINE__) +#define Z_MallocAlign(s,t,u,a) Z_Malloc2(s, t, u, a, __FILE__, __LINE__) void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1); +#define Z_Calloc(s,t,u) Z_Calloc2(s, t, u, 0, __FILE__, __LINE__) +#define Z_CallocAlign(s,t,u,a) Z_Calloc2(s, t, u, a, __FILE__, __LINE__) void *Z_Calloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1); +#define Z_Realloc(p,s,t,u) Z_Realloc2(p, s, t, u, 0, __FILE__, __LINE__) +#define Z_ReallocAlign(p,s,t,u,a) Z_Realloc2(p,s, t, u, a, __FILE__, __LINE__) 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); +#define Z_Malloc(s,t,u) Z_MallocAlign(s, t, u, 0) void *Z_CallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(1); -void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(2); +#define Z_Calloc(s,t,u) Z_CallocAlign(s, t, u, 0) +void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(2) ; +#define Z_Realloc(p, s,t,u) Z_ReallocAlign(p, s, t, u, 0) #endif -// Alloc with standard alignment -#define Z_Malloc(s,t,u) Z_MallocAlign(s, t, u, sizeof(void *)) -#define Z_Calloc(s,t,u) Z_CallocAlign(s, t, u, sizeof(void *)) -#define Z_Realloc(p,s,t,u) Z_ReallocAlign(p, s, t, u, sizeof(void *)) +size_t Z_TagUsage(INT32 tagnum); +size_t Z_TagsUsage(INT32 lowtag, INT32 hightag); -// 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); +char *Z_StrDup(const char *in); -// 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); -void Z_CheckHeap(INT32 i); - -// -// Zone memory modification -// -// enable PARANOIA to get the file + line the functions were called from +// This is used to get the local FILE : LINE info from CPP +// prior to really call the function in question. // #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); +#define Z_ChangeTag(p,t) Z_ChangeTag2(p, t) #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, INT32_MAX) +#ifdef PARANOIA +#define Z_SetUser(p,u) Z_SetUser2(p, u, __FILE__, __LINE__) +#else +#define Z_SetUser(p,u) Z_SetUser2(p, u) +#endif -// -// Miscellaneous functions -// -char *Z_StrDup(const char *in); -#define Z_Unlock(p) (void)p // TODO: remove this now that NDS code has been removed +#ifdef _NDS ///TODO: need a lock reference system +#define Z_Unlock(p) Z_ChangeTag(p, PU_CACHE_UNLOCKED) +#else +#define Z_Unlock(p) (void)p +#endif #endif