mempool_t *poolchain = NULL;
-void *_Mem_Alloc(mempool_t *pool, int size, const char *filename, int fileline)
+void *_Mem_Alloc(mempool_t *pool, size_t size, const char *filename, int fileline)
{
#if MEMCLUMPING
int i, j, k, needed, endbit, largest;
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)
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);
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
while ((*pool)->chain)
- Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
+ _Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)), filename, fileline);
// 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);
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)
// free memory owned by the pool
while (pool->chain)
- Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
+ _Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)), filename, fileline);
// empty child pools, too
for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
#endif
}
+qboolean Mem_IsAllocated(mempool_t *pool, void *data)
+{
+ memheader_t *header;
+ memheader_t *target;
+
+ 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;
+}
+
+
// 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
void Mem_PrintStats(void)
{
- int count = 0, size = 0;
+ size_t count = 0, size = 0;
mempool_t *pool;
memheader_t *mem;
Mem_CheckSentinelsGlobal();
count++;
size += pool->totalsize;
}
- Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
+ Con_Printf("%lu memory pools, totalling %lu bytes (%.3fMB)\n", (unsigned long)count, (unsigned long)size, size / 1048576.0);
for (pool = poolchain;pool;pool = pool->next)
{
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);
+ Con_Printf("Memory pool %p has sprung a leak totalling %lu bytes (%.3fMB)! Listing contents...\n", pool, (unsigned long)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);
+ Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
}
}
}
"size name\n");
for (pool = poolchain;pool;pool = pool->next)
{
- 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" : "");
+ Con_Printf("%10luk (%10luk actual) %s (%+li byte change) %s\n", (unsigned long) ((pool->totalsize + 1023) / 1024), (unsigned long)((pool->realsize + 1023) / 1024), pool->name, (long)pool->totalsize - pool->lastchecksize, (pool->flags & POOLFLAG_TEMP) ? "TEMP" : "");
pool->lastchecksize = pool->totalsize;
if (listallocations)
for (mem = pool->chain;mem;mem = mem->next)
- Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
+ Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
}
}
}
}
-extern void R_TextureStats_PrintTotal(void);
+extern void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal);
void MemStats_f(void)
{
Mem_CheckSentinelsGlobal();
- R_TextureStats_PrintTotal();
+ R_TextureStats_Print(false, false, true);
Mem_PrintStats();
}
poolchain = NULL;
}
+void Memory_Shutdown (void)
+{
+// Mem_FreePool (&zonemempool);
+// Mem_FreePool (&tempmempool);
+}
+
void Memory_Init_Commands (void)
{
Cmd_AddCommand ("memstats", MemStats_f);