]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - zone.c
added Mem_ExpandableArray functions, which are not currently used but
[xonotic/darkplaces.git] / zone.c
diff --git a/zone.c b/zone.c
index 1065ab7d107503e0ce461d9b98c8180d6ab91a8b..0bb325385f1ded23fe763e170f93bdadeb954bde 100644 (file)
--- a/zone.c
+++ b/zone.c
@@ -38,7 +38,7 @@ void *_Mem_Alloc(mempool_t *pool, size_t size, const char *filename, int filelin
        if (pool == NULL)
                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);
+               Con_Printf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, (int)size);
        if (developer.integer && developer_memorydebug.integer)
                _Mem_CheckSentinelsGlobal(filename, fileline);
        pool->totalsize += size;
@@ -139,7 +139,7 @@ static void _Mem_FreeBlock(memheader_t *mem, const char *filename, int fileline)
                Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
        pool = mem->pool;
        if (developer.integer && developer_memory.integer)
-               Con_Printf("Mem_Free: pool %s, alloc %s:%i, free %s:%i, size %i bytes\n", pool->name, mem->filename, mem->fileline, filename, fileline, mem->size);
+               Con_Printf("Mem_Free: pool %s, alloc %s:%i, free %s:%i, size %i bytes\n", pool->name, mem->filename, mem->fileline, filename, fileline, (int)(mem->size));
        // unlink memheader from doubly linked list
        if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
                Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
@@ -193,7 +193,7 @@ static void _Mem_FreeBlock(memheader_t *mem, const char *filename, int fileline)
        {
 #endif
                pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
-               if (developer.integer)
+               if (developer_memorydebug.integer)
                        memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
                free(mem);
 #if MEMCLUMPING
@@ -380,6 +380,84 @@ qboolean Mem_IsAllocated(mempool_t *pool, void *data)
        return false;
 }
 
+void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray)
+{
+       memset(l, 0, sizeof(*l));
+       l->mempool = mempool;
+       l->recordsize = recordsize;
+       l->numrecordsperarray = numrecordsperarray;
+}
+
+void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l)
+{
+       size_t i;
+       if (l->maxarrays)
+       {
+               for (i = 0;i != l->numarrays;l++)
+                       Mem_Free(l->arrays[i].data);
+               Mem_Free(l->arrays);
+       }
+       memset(l, 0, sizeof(*l));
+}
+
+void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l)
+{
+       size_t i, j;
+       for (i = 0;;i++)
+       {
+               if (i == l->numarrays)
+               {
+                       if (l->numarrays == l->maxarrays)
+                       {
+                               memexpandablearray_array_t *oldarrays = l->arrays;
+                               l->maxarrays = max(l->maxarrays * 2, 128);
+                               l->arrays = Mem_Alloc(l->mempool, l->maxarrays * sizeof(*l->arrays));
+                               if (oldarrays)
+                               {
+                                       memcpy(l->arrays, oldarrays, l->numarrays * sizeof(*l->arrays));
+                                       Mem_Free(oldarrays);
+                               }
+                       }
+                       l->arrays[i].numflaggedrecords = 0;
+                       l->arrays[i].data = Mem_Alloc(l->mempool, (l->recordsize + 1) * l->numrecordsperarray);
+                       l->arrays[i].allocflags = l->arrays[i].data + l->recordsize * l->numrecordsperarray;
+                       l->numarrays++;
+               }
+               if (l->arrays[i].numflaggedrecords < l->numrecordsperarray)
+               {
+                       for (j = 0;j < l->numrecordsperarray;j++)
+                       {
+                               if (!l->arrays[i].allocflags[j])
+                               {
+                                       l->arrays[i].allocflags[j] = true;
+                                       l->arrays[i].numflaggedrecords++;
+                                       return (void *)(l->arrays[i].data + l->recordsize * j);
+                               }
+                       }
+               }
+       }
+}
+
+void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record)
+{
+       size_t i, j;
+       unsigned char *p = (unsigned char *)record;
+       for (i = 0;i != l->numarrays;i++)
+       {
+               if (p >= l->arrays[i].data && p < (l->arrays[i].data + l->recordsize * l->numrecordsperarray))
+               {
+                       j = (p - l->arrays[i].data) / l->recordsize;
+                       if (p != l->arrays[i].data + j * l->recordsize)
+                               Sys_Error("Mem_ExpandableArray_FreeRecord: no such record %p\n", p);
+                       if (!l->arrays[i].allocflags[j])
+                               Sys_Error("Mem_ExpandableArray_FreeRecord: record %p is already free!\n", p);
+                       l->arrays[i].allocflags[j] = false;
+                       l->arrays[i].numflaggedrecords--;
+                       return;
+               }
+       }
+}
+
 
 // used for temporary memory allocations around the engine, not for longterm
 // storage, if anything in this pool stays allocated during gameplay, it is
@@ -413,7 +491,7 @@ void Mem_PrintStats(void)
        }
 }
 
-void Mem_PrintList(int listallocations)
+void Mem_PrintList(size_t minallocationsize)
 {
        mempool_t *pool;
        memheader_t *mem;
@@ -424,8 +502,8 @@ void Mem_PrintList(int listallocations)
        {
                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)
+               for (mem = pool->chain;mem;mem = mem->next)
+                       if (mem->size >= minallocationsize)
                                Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
        }
 }
@@ -435,17 +513,13 @@ void MemList_f(void)
        switch(Cmd_Argc())
        {
        case 1:
-               Mem_PrintList(false);
+               Mem_PrintList(1<<30);
                Mem_PrintStats();
                break;
        case 2:
-               if (!strcmp(Cmd_Argv(1), "all"))
-               {
-                       Mem_PrintList(true);
-                       Mem_PrintStats();
-                       break;
-               }
-               // drop through
+               Mem_PrintList(atoi(Cmd_Argv(1)) * 1024);
+               Mem_PrintStats();
+               break;
        default:
                Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
                break;
@@ -482,7 +556,7 @@ void Memory_Shutdown (void)
 void Memory_Init_Commands (void)
 {
        Cmd_AddCommand ("memstats", MemStats_f, "prints memory system statistics");
-       Cmd_AddCommand ("memlist", MemList_f, "prints memory pool information (and individual allocations if used as memlist all)");
+       Cmd_AddCommand ("memlist", MemList_f, "prints memory pool information (or if used as memlist 5 lists individual allocations of 5K or larger, 0 lists all allocations)");
        Cvar_RegisterVariable (&developer_memory);
        Cvar_RegisterVariable (&developer_memorydebug);
 }