ZDEBUG is default

- ZDEBUG2 is now ZDEBUG ... although admittedly, it doesn't seem that useful, it just enables some CONS_Debug's ... but now there's a convenient way to enable them now I suppose
- Mostly just means the I_Errors give __FILE__ and __LINE__.
- Also enables a "memdump" command that just prints a bunch of zone memory info.

I don't think any of this should be guarded, so I went ahead and did this.
This commit is contained in:
Sally Coolatta 2021-04-26 14:49:45 -04:00
parent be91f05393
commit 44ed9fcd4c
5 changed files with 28 additions and 135 deletions

View file

@ -659,9 +659,7 @@ flatfound:
levelflat->u.flat.baselumpnum = LUMPERROR; levelflat->u.flat.baselumpnum = LUMPERROR;
} }
#ifndef ZDEBUG
CONS_Debug(DBG_SETUP, "flat #%03d: %s\n", atoi(sizeu1(numlevelflats)), levelflat->name); CONS_Debug(DBG_SETUP, "flat #%03d: %s\n", atoi(sizeu1(numlevelflats)), levelflat->name);
#endif
return ( numlevelflats++ ); return ( numlevelflats++ );
} }

View file

@ -1611,9 +1611,7 @@ INT32 R_CheckTextureNumForName(const char *name)
Z_Realloc(tidcache, tidcachelen * sizeof(*tidcache), PU_STATIC, &tidcache); Z_Realloc(tidcache, tidcachelen * sizeof(*tidcache), PU_STATIC, &tidcache);
strncpy(tidcache[tidcachelen-1].name, name, 8); strncpy(tidcache[tidcachelen-1].name, name, 8);
tidcache[tidcachelen-1].name[8] = '\0'; tidcache[tidcachelen-1].name[8] = '\0';
#ifndef ZDEBUG
CONS_Debug(DBG_SETUP, "texture #%s: %s\n", sizeu1(tidcachelen), tidcache[tidcachelen-1].name); CONS_Debug(DBG_SETUP, "texture #%s: %s\n", sizeu1(tidcachelen), tidcache[tidcachelen-1].name);
#endif
tidcache[tidcachelen-1].id = i; tidcache[tidcachelen-1].id = i;
return i; return i;
} }

View file

@ -484,9 +484,7 @@ void R_AddSpriteDefs(UINT16 wadnum)
#endif #endif
// if a new sprite was added (not just replaced) // if a new sprite was added (not just replaced)
addsprites++; addsprites++;
#ifndef ZDEBUG
CONS_Debug(DBG_SETUP, "sprite %s set in pwad %d\n", sprnames[i], wadnum); CONS_Debug(DBG_SETUP, "sprite %s set in pwad %d\n", sprnames[i], wadnum);
#endif
} }
} }

View file

@ -47,10 +47,6 @@ static boolean Z_calloc = false;
#define ZONEID 0xa441d13d #define ZONEID 0xa441d13d
#ifdef ZDEBUG
//#define ZDEBUG2
#endif
struct memblock_s; struct memblock_s;
typedef struct typedef struct
@ -76,10 +72,8 @@ typedef struct memblock_s
size_t size; // including the header and blocks size_t size; // including the header and blocks
size_t realsize; // size of real data only size_t realsize; // size of real data only
#ifdef ZDEBUG
const char *ownerfile; const char *ownerfile;
INT32 ownerline; INT32 ownerline;
#endif
struct memblock_s *next, *prev; struct memblock_s *next, *prev;
} ATTRPACK memblock_t; } ATTRPACK memblock_t;
@ -91,9 +85,7 @@ static memblock_t head;
// Function prototypes // Function prototypes
// //
static void Command_Memfree_f(void); static void Command_Memfree_f(void);
#ifdef ZDEBUG
static void Command_Memdump_f(void); static void Command_Memdump_f(void);
#endif
// -------------------------- // --------------------------
// Zone memory initialisation // Zone memory initialisation
@ -117,10 +109,7 @@ void Z_Init(void)
// Note: This allocates memory. Watch out. // Note: This allocates memory. Watch out.
COM_AddCommand("memfree", Command_Memfree_f); COM_AddCommand("memfree", Command_Memfree_f);
#ifdef ZDEBUG
COM_AddCommand("memdump", Command_Memdump_f); COM_AddCommand("memdump", Command_Memdump_f);
#endif
} }
@ -137,12 +126,8 @@ void Z_Init(void)
* \return A pointer to the memblock_t for the given memory. * \return A pointer to the memblock_t for the given memory.
* \sa Z_Free, Z_ReallocAlign * \sa Z_Free, Z_ReallocAlign
*/ */
#ifdef ZDEBUG
#define Ptr2Memblock(s, f) Ptr2Memblock2(s, f, __FILE__, __LINE__) #define Ptr2Memblock(s, f) Ptr2Memblock2(s, f, __FILE__, __LINE__)
static memblock_t *Ptr2Memblock2(void *ptr, const char* func, const char *file, INT32 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; memhdr_t *hdr;
memblock_t *block; memblock_t *block;
@ -150,8 +135,8 @@ static memblock_t *Ptr2Memblock(void *ptr, const char* func)
if (ptr == NULL) if (ptr == NULL)
return NULL; return NULL;
#ifdef ZDEBUG2 #ifdef ZDEBUG
CONS_Printf("%s %s:%d\n", func, file, line); CONS_Debug(DBG_MEMORY, "%s %s:%d\n", func, file, line);
#endif #endif
hdr = (memhdr_t *)((UINT8 *)ptr - sizeof *hdr); hdr = (memhdr_t *)((UINT8 *)ptr - sizeof *hdr);
@ -163,20 +148,12 @@ static memblock_t *Ptr2Memblock(void *ptr, const char* func)
#ifdef VALGRIND_MEMPOOL_EXISTS #ifdef VALGRIND_MEMPOOL_EXISTS
if (!VALGRIND_MEMPOOL_EXISTS(hdr->block)) if (!VALGRIND_MEMPOOL_EXISTS(hdr->block))
{ {
#ifdef ZDEBUG
I_Error("%s: bad memblock from %s:%d", func, file, line); I_Error("%s: bad memblock from %s:%d", func, file, line);
#else
I_Error("%s: bad memblock", func);
#endif
} }
#endif #endif
if (hdr->id != ZONEID) if (hdr->id != ZONEID)
{ {
#ifdef ZDEBUG
I_Error("%s: wrong id from %s:%d", func, file, line); I_Error("%s: wrong id from %s:%d", func, file, line);
#else
I_Error("%s: wrong id", func);
#endif
} }
block = hdr->block; block = hdr->block;
#ifdef VALGRIND_MAKE_MEM_NOACCESS #ifdef VALGRIND_MAKE_MEM_NOACCESS
@ -192,31 +169,24 @@ static memblock_t *Ptr2Memblock(void *ptr, const char* func)
* assumed to have been allocated with Z_Malloc/Z_Calloc. * assumed to have been allocated with Z_Malloc/Z_Calloc.
* \sa Z_FreeTags * \sa Z_FreeTags
*/ */
#ifdef ZDEBUG
void Z_Free2(void *ptr, const char *file, INT32 line) void Z_Free2(void *ptr, const char *file, INT32 line)
#else
void Z_Free(void *ptr)
#endif
{ {
memblock_t *block; memblock_t *block;
if (ptr == NULL) if (ptr == NULL)
return; return;
#ifdef ZDEBUG2 /*
// Sal: There's a print exactly like this just below?
#ifdef ZDEBUG
CONS_Debug(DBG_MEMORY, "Z_Free %s:%d\n", file, line); CONS_Debug(DBG_MEMORY, "Z_Free %s:%d\n", file, line);
#endif #endif
*/
#ifdef ZDEBUG
block = Ptr2Memblock2(ptr, "Z_Free", file, line); block = Ptr2Memblock2(ptr, "Z_Free", file, line);
#else
block = Ptr2Memblock(ptr, "Z_Free");
#endif
#ifdef ZDEBUG
// Write every Z_Free call to a debug file. // Write every Z_Free call to a debug file.
CONS_Debug(DBG_MEMORY, "Z_Free at %s:%d\n", file, line); CONS_Debug(DBG_MEMORY, "Z_Free at %s:%d\n", file, line);
#endif
// anything that isn't by lua gets passed to lua just in case. // anything that isn't by lua gets passed to lua just in case.
if (block->tag != PU_LUA) if (block->tag != PU_LUA)
@ -280,12 +250,8 @@ static void *xm(size_t size)
* \note You can pass Z_Malloc() a NULL user if the tag is less than PU_PURGELEVEL. * \note You can pass Z_Malloc() a NULL user if the tag is less than PU_PURGELEVEL.
* \sa Z_CallocAlign, Z_ReallocAlign * \sa Z_CallocAlign, Z_ReallocAlign
*/ */
#ifdef ZDEBUG
void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits,
const char *file, INT32 line) const char *file, INT32 line)
#else
void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits)
#endif
{ {
size_t extrabytes = (1<<alignbits) - 1; size_t extrabytes = (1<<alignbits) - 1;
size_t padsize = 0; size_t padsize = 0;
@ -295,7 +261,7 @@ void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits)
void *given; void *given;
size_t blocksize = extrabytes + sizeof *hdr + size; size_t blocksize = extrabytes + sizeof *hdr + size;
#ifdef ZDEBUG2 #ifdef ZDEBUG
CONS_Debug(DBG_MEMORY, "Z_Malloc %s:%d\n", file, line); CONS_Debug(DBG_MEMORY, "Z_Malloc %s:%d\n", file, line);
#endif #endif
@ -329,10 +295,8 @@ void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits)
block->hdr = hdr; block->hdr = hdr;
block->tag = tag; block->tag = tag;
block->user = NULL; block->user = NULL;
#ifdef ZDEBUG
block->ownerline = line; block->ownerline = line;
block->ownerfile = file; block->ownerfile = file;
#endif
block->size = blocksize; block->size = blocksize;
block->realsize = size; block->realsize = size;
@ -375,20 +339,12 @@ void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits)
* \note You can pass Z_Calloc() a NULL user if the tag is less than PU_PURGELEVEL. * \note You can pass Z_Calloc() a NULL user if the tag is less than PU_PURGELEVEL.
* \sa Z_MallocAlign, Z_ReallocAlign * \sa Z_MallocAlign, Z_ReallocAlign
*/ */
#ifdef ZDEBUG
void *Z_Calloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) void *Z_Calloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line)
#else
void *Z_CallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits)
#endif
{ {
#ifdef VALGRIND_MEMPOOL_ALLOC #ifdef VALGRIND_MEMPOOL_ALLOC
Z_calloc = true; Z_calloc = true;
#endif #endif
#ifdef ZDEBUG
return memset(Z_Malloc2 (size, tag, user, alignbits, file, line), 0, size); return memset(Z_Malloc2 (size, tag, user, alignbits, file, line), 0, size);
#else
return memset(Z_MallocAlign(size, tag, user, alignbits ), 0, size);
#endif
} }
/** The Z_ReallocAlign function. /** The Z_ReallocAlign function.
@ -407,17 +363,13 @@ void *Z_CallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits)
* \note You can pass Z_Realloc() a NULL user if the tag is less than PU_PURGELEVEL. * \note You can pass Z_Realloc() a NULL user if the tag is less than PU_PURGELEVEL.
* \sa Z_MallocAlign, Z_CallocAlign * \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) 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)
#endif
{ {
void *rez; void *rez;
memblock_t *block; memblock_t *block;
size_t copysize; size_t copysize;
#ifdef ZDEBUG2 #ifdef ZDEBUG
CONS_Debug(DBG_MEMORY, "Z_Realloc %s:%d\n", file, line); CONS_Debug(DBG_MEMORY, "Z_Realloc %s:%d\n", file, line);
#endif #endif
@ -429,29 +381,17 @@ void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignb
if (!ptr) if (!ptr)
{ {
#ifdef ZDEBUG
return Z_Calloc2(size, tag, user, alignbits, file , line); return Z_Calloc2(size, tag, user, alignbits, file , line);
#else
return Z_CallocAlign(size, tag, user, alignbits);
#endif
} }
#ifdef ZDEBUG
block = Ptr2Memblock2(ptr, "Z_Realloc", file, line); block = Ptr2Memblock2(ptr, "Z_Realloc", file, line);
#else
block = Ptr2Memblock(ptr, "Z_Realloc");
#endif
if (block == NULL) if (block == NULL)
return NULL; return NULL;
#ifdef ZDEBUG
// Write every Z_Realloc call to a debug file. // Write every Z_Realloc call to a debug file.
DEBFILE(va("Z_Realloc at %s:%d\n", file, line)); DEBFILE(va("Z_Realloc at %s:%d\n", file, line));
rez = Z_Malloc2(size, tag, user, alignbits, file, line); rez = Z_Malloc2(size, tag, user, alignbits, file, line);
#else
rez = Z_MallocAlign(size, tag, user, alignbits);
#endif
if (size < block->realsize) if (size < block->realsize)
copysize = size; copysize = size;
@ -460,11 +400,7 @@ void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignb
M_Memcpy(rez, ptr, copysize); M_Memcpy(rez, ptr, copysize);
#ifdef ZDEBUG
Z_Free2(ptr, file, line); Z_Free2(ptr, file, line);
#else
Z_Free(ptr);
#endif
// Need to set the user in case the old block had the same one, in // Need to set the user in case the old block had the same one, in
// which case the Z_Free will just have NULLed it out. // which case the Z_Free will just have NULLed it out.
@ -569,7 +505,7 @@ void Z_CheckHeap(INT32 i)
blocknumon++; blocknumon++;
hdr = block->hdr; hdr = block->hdr;
given = (UINT8 *)hdr + sizeof *hdr; given = (UINT8 *)hdr + sizeof *hdr;
#ifdef ZDEBUG2 #ifdef ZDEBUG
CONS_Debug(DBG_MEMORY, "block %u owned by %s:%d\n", CONS_Debug(DBG_MEMORY, "block %u owned by %s:%d\n",
blocknumon, block->ownerfile, block->ownerline); blocknumon, block->ownerfile, block->ownerline);
#endif #endif
@ -577,51 +513,35 @@ void Z_CheckHeap(INT32 i)
if (!VALGRIND_MEMPOOL_EXISTS(block)) if (!VALGRIND_MEMPOOL_EXISTS(block))
{ {
I_Error("Z_CheckHeap %d: block %u" I_Error("Z_CheckHeap %d: block %u"
#ifdef ZDEBUG
"(owned by %s:%d)" "(owned by %s:%d)"
#endif " should not exist", i, blocknumon,
" should not exist", i, blocknumon block->ownerfile, block->ownerline
#ifdef ZDEBUG );
, block->ownerfile, block->ownerline
#endif
);
} }
#endif #endif
if (block->user != NULL && *(block->user) != given) if (block->user != NULL && *(block->user) != given)
{ {
I_Error("Z_CheckHeap %d: block %u" I_Error("Z_CheckHeap %d: block %u"
#ifdef ZDEBUG
"(owned by %s:%d)" "(owned by %s:%d)"
#endif " doesn't have a proper user", i, blocknumon,
" doesn't have a proper user", i, blocknumon block->ownerfile, block->ownerline
#ifdef ZDEBUG );
, block->ownerfile, block->ownerline
#endif
);
} }
if (block->next->prev != block) if (block->next->prev != block)
{ {
I_Error("Z_CheckHeap %d: block %u" I_Error("Z_CheckHeap %d: block %u"
#ifdef ZDEBUG
"(owned by %s:%d)" "(owned by %s:%d)"
#endif " lacks proper backlink", i, blocknumon,
" lacks proper backlink", i, blocknumon block->ownerfile, block->ownerline
#ifdef ZDEBUG );
, block->ownerfile, block->ownerline
#endif
);
} }
if (block->prev->next != block) if (block->prev->next != block)
{ {
I_Error("Z_CheckHeap %d: block %u" I_Error("Z_CheckHeap %d: block %u"
#ifdef ZDEBUG
"(owned by %s:%d)" "(owned by %s:%d)"
#endif " lacks proper forward link", i, blocknumon,
" lacks proper forward link", i, blocknumon block->ownerfile, block->ownerline
#ifdef ZDEBUG );
, block->ownerfile, block->ownerline
#endif
);
} }
#ifdef VALGRIND_MAKE_MEM_DEFINED #ifdef VALGRIND_MAKE_MEM_DEFINED
VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr); VALGRIND_MAKE_MEM_DEFINED(hdr, sizeof *hdr);
@ -629,27 +549,19 @@ void Z_CheckHeap(INT32 i)
if (hdr->block != block) if (hdr->block != block)
{ {
I_Error("Z_CheckHeap %d: block %u" I_Error("Z_CheckHeap %d: block %u"
#ifdef ZDEBUG
"(owned by %s:%d)" "(owned by %s:%d)"
#endif
" doesn't have linkback from allocated memory", " doesn't have linkback from allocated memory",
i, blocknumon i, blocknumon,
#ifdef ZDEBUG block->ownerfile, block->ownerline
, block->ownerfile, block->ownerline );
#endif
);
} }
if (hdr->id != ZONEID) if (hdr->id != ZONEID)
{ {
I_Error("Z_CheckHeap %d: block %u" I_Error("Z_CheckHeap %d: block %u"
#ifdef ZDEBUG
"(owned by %s:%d)" "(owned by %s:%d)"
#endif " have the wrong ID", i, blocknumon,
" have the wrong ID", i, blocknumon block->ownerfile, block->ownerline
#ifdef ZDEBUG );
, block->ownerfile, block->ownerline
#endif
);
} }
#ifdef VALGRIND_MAKE_MEM_NOACCESS #ifdef VALGRIND_MAKE_MEM_NOACCESS
VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr); VALGRIND_MAKE_MEM_NOACCESS(hdr, sizeof *hdr);
@ -828,11 +740,9 @@ static void Command_Memfree_f(void)
CONS_Printf(M_GetText("Available physical memory: %7u KB\n"), freebytes>>10); CONS_Printf(M_GetText("Available physical memory: %7u KB\n"), freebytes>>10);
} }
#ifdef ZDEBUG
/** The function called by the "memdump" console command. /** The function called by the "memdump" console command.
* Prints zone memory debugging information (i.e. tag, size, location in code allocated). * 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). * 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) static void Command_Memdump_f(void)
{ {
@ -853,7 +763,6 @@ static void Command_Memdump_f(void)
CONS_Printf("[%3d] %s (%s) bytes @ %s:%d\n", block->tag, sizeu1(block->size), sizeu2(block->realsize), filename ? filename + 1 : block->ownerfile, block->ownerline); CONS_Printf("[%3d] %s (%s) bytes @ %s:%d\n", block->tag, sizeu1(block->size), sizeu2(block->realsize), filename ? filename + 1 : block->ownerfile, block->ownerline);
} }
} }
#endif
/** Creates a copy of a string. /** Creates a copy of a string.
* *

View file

@ -79,12 +79,8 @@ void Z_Init(void);
// //
// Zone memory allocation // 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 // Z_Free and alloc with alignment
#ifdef ZDEBUG
#define Z_Free(p) Z_Free2(p, __FILE__, __LINE__) #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_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_CallocAlign(s,t,u,a) Z_Calloc2(s, t, u, a, __FILE__, __LINE__)
@ -93,12 +89,6 @@ 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_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_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); 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);
void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(2);
#endif
// Alloc with no alignment // Alloc with no alignment
#define Z_Malloc(s,t,u) Z_MallocAlign(s, t, u, 0) #define Z_Malloc(s,t,u) Z_MallocAlign(s, t, u, 0)
@ -106,7 +96,7 @@ void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignb
#define Z_Realloc(p,s,t,u) Z_ReallocAlign(p, s, t, u, 0) #define Z_Realloc(p,s,t,u) Z_ReallocAlign(p, s, t, u, 0)
// Free all memory by tag // Free all memory by tag
// these don't give line numbers for ZDEBUG currently though // these don't give line numbers currently though
// (perhaps this should be changed in future?) // (perhaps this should be changed in future?)
#define Z_FreeTag(tagnum) Z_FreeTags(tagnum, tagnum) #define Z_FreeTag(tagnum) Z_FreeTags(tagnum, tagnum)
void Z_FreeTags(INT32 lowtag, INT32 hightag); void Z_FreeTags(INT32 lowtag, INT32 hightag);