From fa330326d7e8f8b15f73f77a7cce86a6e954f935 Mon Sep 17 00:00:00 2001 From: havoc Date: Mon, 15 Aug 2005 07:02:51 +0000 Subject: [PATCH] added more developer_memorydebug sentinel checks, and made developer_memorydebug do not-allocated checks in a few cases that were not checked before modified Mem_IsAllocated to be able to take a NULL pool pointer to scan all pools for the allocation git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5580 d7cf8633-e32d-0410-b094-e92efae38249 --- zone.c | 47 +++++++++++++++++++++++++++++++++++++++-------- zone.h | 1 + 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/zone.c b/zone.c index 7b83e25a..0f1678d8 100644 --- a/zone.c +++ b/zone.c @@ -135,6 +135,12 @@ void _Mem_Free(void *data, const char *filename, int fileline) mempool_t *pool; if (data == NULL) Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline); + if (developer.integer && developer_memorydebug.integer) + { + _Mem_CheckSentinelsGlobal(filename, fileline); + if (!Mem_IsAllocated(NULL, data)) + Sys_Error("Mem_Free: data is not allocated (called at %s:%i)", filename, fileline); + } mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t)); if (mem->sentinel1 != MEMHEADER_SENTINEL1) @@ -208,6 +214,8 @@ void _Mem_Free(void *data, 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; + if (developer.integer && developer_memorydebug.integer) + _Mem_CheckSentinelsGlobal(filename, fileline); pool = malloc(sizeof(mempool_t)); if (pool == NULL) Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline); @@ -231,16 +239,18 @@ void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline) { mempool_t **chainaddress, *iter, *temp; + if (developer.integer && developer_memorydebug.integer) + _Mem_CheckSentinelsGlobal(filename, fileline); if (*pool) { - if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1) - Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline); - if ((*pool)->sentinel2 != MEMHEADER_SENTINEL1) - Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline); // unlink pool from chain for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next)); if (*chainaddress != *pool) Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline); + if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1) + Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline); + if ((*pool)->sentinel2 != MEMHEADER_SENTINEL1) + Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline); *chainaddress = (*pool)->next; // free memory owned by the pool @@ -263,6 +273,16 @@ void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline) { mempool_t *chainaddress; + if (developer.integer && developer_memorydebug.integer) + { + _Mem_CheckSentinelsGlobal(filename, fileline); + // check if this pool is in the poolchain + for (chainaddress = poolchain;chainaddress;chainaddress = chainaddress->next) + if (chainaddress == pool) + break; + if (!chainaddress) + Sys_Error("Mem_EmptyPool: pool is already free (emptypool at %s:%i)", filename, fileline); + } if (pool == NULL) Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline); if (pool->sentinel1 != MEMHEADER_SENTINEL1) @@ -335,10 +355,21 @@ 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; + if (pool) + { + // search only one pool + target = (memheader_t *)((qbyte *) data - sizeof(memheader_t)); + for( header = pool->chain ; header ; header = header->next ) + if( header == target ) + return true; + } + else + { + // search all pools + for (pool = poolchain;pool;pool = pool->next) + if (Mem_IsAllocated(pool, data)) + return true; + } return false; } diff --git a/zone.h b/zone.h index 0ef231be..af6ddfd5 100644 --- a/zone.h +++ b/zone.h @@ -132,6 +132,7 @@ void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline); void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline); void _Mem_CheckSentinels(void *data, const char *filename, int fileline); void _Mem_CheckSentinelsGlobal(const char *filename, int fileline); +// if pool is NULL this searches ALL pools for the allocation qboolean Mem_IsAllocated(mempool_t *pool, void *data); // used for temporary allocations -- 2.39.2