]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - zone.c
made darkplaces compile successfully with g++ to test for errors C doesn't care about...
[xonotic/darkplaces.git] / zone.c
diff --git a/zone.c b/zone.c
index e20b42035b499d1eb5d610c63daf920a45ce7c55..590dd8393ca8d74321b6615da17cdfe0d2bbc39f 100644 (file)
--- a/zone.c
+++ b/zone.c
@@ -26,7 +26,7 @@ cvar_t developer_memorydebug = {0, "developer_memorydebug", "0"};
 
 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;
@@ -101,7 +101,7 @@ choseclump:
                // big allocations are not clumped
 #endif
                pool->realsize += sizeof(memheader_t) + size + sizeof(int);
-               mem = malloc(sizeof(memheader_t) + size + sizeof(int));
+               mem = (memheader_t *)malloc(sizeof(memheader_t) + size + sizeof(int));
                if (mem == NULL)
                        Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
 #if MEMCLUMPING
@@ -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)
@@ -197,17 +203,20 @@ 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));
+       if (developer.integer && developer_memorydebug.integer)
+               _Mem_CheckSentinelsGlobal(filename, fileline);
+       pool = (mempool_t *)malloc(sizeof(mempool_t));
        if (pool == NULL)
                Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
        memset(pool, 0, sizeof(mempool_t));
@@ -215,10 +224,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);
        strlcpy (pool->name, name, sizeof (pool->name));
+       pool->parent = parent;
        pool->next = poolchain;
        poolchain = pool;
        return pool;
@@ -226,22 +237,30 @@ 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 (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));
@@ -252,6 +271,18 @@ 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 (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)
@@ -261,7 +292,13 @@ 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)));
+               _Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)), filename, fileline);
+
+       // 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)
@@ -313,6 +350,30 @@ void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
 #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
@@ -322,7 +383,7 @@ mempool_t *zonemempool;
 
 void Mem_PrintStats(void)
 {
-       int count = 0, size = 0;
+       size_t count = 0, size = 0, realsize = 0;
        mempool_t *pool;
        memheader_t *mem;
        Mem_CheckSentinelsGlobal();
@@ -330,16 +391,18 @@ void Mem_PrintStats(void)
        {
                count++;
                size += pool->totalsize;
+               realsize += pool->realsize;
        }
-       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)
+       Con_Printf("%lu memory pools, totalling %lu bytes (%.3fMB)\n", (unsigned long)count, (unsigned long)size, size / 1048576.0);
+       Con_Printf("total allocated size: %lu bytes (%.3fMB)\n", (unsigned long)realsize, realsize / 1048576.0);
+       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 %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("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
+               }
        }
 }
 
@@ -348,18 +411,15 @@ 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("%10ik (%10ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
-               else
-                       Con_Printf("%10ik (%10ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
+               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);
        }
 }
 
@@ -380,16 +440,16 @@ 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;
        }
 }
 
-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();
 }
 
@@ -401,8 +461,15 @@ 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_Shutdown (void)
+{
+//     Mem_FreePool (&zonemempool);
+//     Mem_FreePool (&tempmempool);
 }
 
 void Memory_Init_Commands (void)