From: havoc Date: Fri, 6 Sep 2002 16:51:51 +0000 (+0000) Subject: mempools now have sentinel checks, for completeness X-Git-Tag: RELEASE_0_2_0_RC1~264 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=c901931fd040f01b09e1e20905bba38f7aee6f87;p=xonotic%2Fdarkplaces.git mempools now have sentinel checks, for completeness git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2342 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/zone.c b/zone.c index f5fdf73c..db9952ff 100644 --- a/zone.c +++ b/zone.c @@ -201,6 +201,10 @@ mempool_t *_Mem_AllocPool(char *name, char *filename, int fileline) if (pool == NULL) Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline); memset(pool, 0, sizeof(mempool_t)); + pool->sentinel1 = MEMHEADER_SENTINEL1; + pool->sentinel2 = MEMHEADER_SENTINEL1; + pool->filename = filename; + pool->fileline = fileline; pool->chain = NULL; pool->totalsize = 0; pool->realsize = sizeof(mempool_t); @@ -215,6 +219,10 @@ void _Mem_FreePool(mempool_t **pool, char *filename, int fileline) mempool_t **chainaddress; 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) @@ -236,6 +244,10 @@ void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline) { if (pool == NULL) Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline); + if (pool->sentinel1 != MEMHEADER_SENTINEL1) + Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline); + if (pool->sentinel2 != MEMHEADER_SENTINEL1) + Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline); // free memory owned by the pool while (pool->chain) @@ -275,15 +287,19 @@ void _Mem_CheckSentinelsGlobal(char *filename, int fileline) #endif mempool_t *pool; for (pool = poolchain;pool;pool = pool->next) -#if MEMCLUMPING { -#endif + if (pool->sentinel1 != MEMHEADER_SENTINEL1) + Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline); + if (pool->sentinel2 != MEMHEADER_SENTINEL1) + Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline); + } + for (pool = poolchain;pool;pool = pool->next) for (mem = pool->chain;mem;mem = mem->chain) _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline); #if MEMCLUMPING + for (pool = poolchain;pool;pool = pool->next) for (clump = pool->clumpchain;clump;clump = clump->chain) _Mem_CheckClumpSentinels(clump, filename, fileline); - } #endif } diff --git a/zone.h b/zone.h index 9cdd9ff9..c76867d2 100644 --- a/zone.h +++ b/zone.h @@ -84,6 +84,8 @@ memclump_t; typedef struct mempool_s { + // should always be MEMHEADER_SENTINEL1 + int sentinel1; // chain of individual memory allocations struct memheader_s *chain; #if MEMCLUMPING @@ -100,6 +102,11 @@ typedef struct mempool_s char name[POOLNAMESIZE]; // linked into global mempool list struct mempool_s *next; + // file name and line where Mem_AllocPool was called + char *filename; + int fileline; + // should always be MEMHEADER_SENTINEL1 + int sentinel2; } mempool_t;