X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=zone.c;h=f58aa54a38a94d8356088ecb87dfd5834d9ca862;hb=a2445d85eccf4d0c5766f44f02cd04a600c4bad4;hp=196324375527c9e48170c890c0180366323116e5;hpb=53ffc01f08cc3a88091a966034600eef4a8e2c99;p=xonotic%2Fdarkplaces.git diff --git a/zone.c b/zone.c index 19632437..f58aa54a 100644 --- a/zone.c +++ b/zone.c @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" cvar_t developer_memory = {0, "developer_memory", "0"}; +cvar_t developer_memorydebug = {0, "developer_memorydebug", "0"}; mempool_t *poolchain = NULL; @@ -38,6 +39,8 @@ void *_Mem_Alloc(mempool_t *pool, int size, const char *filename, int fileline) Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline); if (developer.integer && developer_memory.integer) Con_Printf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, size); + if (developer.integer && developer_memorydebug.integer) + _Mem_CheckSentinelsGlobal(filename, fileline); pool->totalsize += size; #if MEMCLUMPING if (size < 4096) @@ -194,14 +197,15 @@ void _Mem_Free(void *data, const char *filename, int fileline) { #endif pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int); - memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int)); + if (developer.integer) + memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int)); free(mem); #if MEMCLUMPING } #endif } -mempool_t *_Mem_AllocPool(const char *name, const char *filename, int fileline) +mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline) { mempool_t *pool; pool = malloc(sizeof(mempool_t)); @@ -212,10 +216,12 @@ mempool_t *_Mem_AllocPool(const char *name, const char *filename, int fileline) pool->sentinel2 = MEMHEADER_SENTINEL1; pool->filename = filename; pool->fileline = fileline; + pool->flags = flags; pool->chain = NULL; pool->totalsize = 0; pool->realsize = sizeof(mempool_t); - strcpy(pool->name, name); + strlcpy (pool->name, name, sizeof (pool->name)); + pool->parent = parent; pool->next = poolchain; poolchain = pool; return pool; @@ -223,7 +229,8 @@ mempool_t *_Mem_AllocPool(const char *name, const char *filename, int fileline) void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline) { - mempool_t **chainaddress; + mempool_t **chainaddress, *iter, *temp; + if (*pool) { if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1) @@ -240,6 +247,11 @@ void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline) while ((*pool)->chain) Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t))); + // free child pools, too + for(iter = poolchain; iter; temp = iter = iter->next) + if(iter->parent == *pool) + _Mem_FreePool(&temp, filename, fileline); + // free the pool itself memset(*pool, 0xBF, sizeof(mempool_t)); free(*pool); @@ -249,6 +261,8 @@ void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline) void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline) { + mempool_t *chainaddress; + if (pool == NULL) Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline); if (pool->sentinel1 != MEMHEADER_SENTINEL1) @@ -259,6 +273,12 @@ void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline) // free memory owned by the pool while (pool->chain) Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t))); + + // empty child pools, too + for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next) + if(chainaddress->parent == pool) + _Mem_EmptyPool(chainaddress, filename, fileline); + } void _Mem_CheckSentinels(void *data, const char *filename, int fileline) @@ -310,6 +330,19 @@ void _Mem_CheckSentinelsGlobal(const char *filename, int fileline) #endif } +qboolean Mem_IsAllocated(mempool_t *pool, void *data) +{ + memheader_t *header; + memheader_t *target; + + target = (memheader_t *)((qbyte *) data - sizeof(memheader_t)); + for( header = pool->chain ; header ; header = header->next ) + if( header == target ) + return true; + return false; +} + + // used for temporary memory allocations around the engine, not for longterm // storage, if anything in this pool stays allocated during gameplay, it is // considered a leak @@ -329,14 +362,14 @@ void Mem_PrintStats(void) size += pool->totalsize; } Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0); - if (tempmempool == NULL) - Con_Printf("Error: no tempmempool allocated\n"); - else if (tempmempool->chain) + for (pool = poolchain;pool;pool = pool->next) { - Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0); - Con_Printf("listing temporary memory allocations:\n"); - for (mem = tempmempool->chain;mem;mem = mem->next) - Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline); + if ((pool->flags & POOLFLAG_TEMP) && pool->chain) + { + Con_Printf("Memory pool %p has sprung a leak totalling %i bytes (%.3fMB)! Listing contents...\n", pool, pool->totalsize, pool->totalsize / 1048576.0); + for (mem = pool->chain;mem;mem = mem->next) + Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline); + } } } @@ -345,14 +378,11 @@ void Mem_PrintList(int listallocations) mempool_t *pool; memheader_t *mem; Mem_CheckSentinelsGlobal(); - Con_Printf("memory pool list:\n" + Con_Print("memory pool list:\n" "size name\n"); for (pool = poolchain;pool;pool = pool->next) { - if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize) - Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize); - else - Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name); + Con_Printf("%10ik (%10ik actual) %s (%+i byte change) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize, (pool->flags & POOLFLAG_TEMP) ? "TEMP" : ""); pool->lastchecksize = pool->totalsize; if (listallocations) for (mem = pool->chain;mem;mem = mem->next) @@ -377,7 +407,7 @@ void MemList_f(void) } // drop through default: - Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n"); + Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n"); break; } } @@ -398,8 +428,9 @@ Memory_Init */ void Memory_Init (void) { - tempmempool = Mem_AllocPool("Temporary Memory"); - zonemempool = Mem_AllocPool("Zone"); + tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL); + zonemempool = Mem_AllocPool("Zone", 0, NULL); + poolchain = NULL; } void Memory_Init_Commands (void) @@ -407,5 +438,6 @@ void Memory_Init_Commands (void) Cmd_AddCommand ("memstats", MemStats_f); Cmd_AddCommand ("memlist", MemList_f); Cvar_RegisterVariable (&developer_memory); + Cvar_RegisterVariable (&developer_memorydebug); }