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;
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);
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;i++)
+ 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;
+ }
+ }
+}
+
+size_t Mem_ExpandableArray_IndexRange(memexpandablearray_t *l)
+{
+ size_t i, j, k;
+ if (!l->numarrays)
+ return 0;
+ i = l->numarrays - 1;
+ for (j = 0, k = 0;k < l->arrays[i].numflaggedrecords;j++)
+ if (l->arrays[i].allocflags[j])
+ k++;
+ return l->numrecordsperarray * i + j;
+}
+
+void *Mem_ExpandableArray_RecordAtIndex(memexpandablearray_t *l, size_t index)
+{
+ size_t i, j;
+ i = index / l->numrecordsperarray;
+ j = index % l->numrecordsperarray;
+ if (i >= l->numarrays || !l->arrays[i].allocflags[j])
+ return NULL;
+ return (void *)(l->arrays[i].data + j * l->recordsize);
+}
+
// used for temporary memory allocations around the engine, not for longterm
// storage, if anything in this pool stays allocated during gameplay, it is
{
Mem_CheckSentinelsGlobal();
R_TextureStats_Print(false, false, true);
+ GL_Mesh_ListVBOs(false);
Mem_PrintStats();
}
+char* Mem_strdup (mempool_t *pool, const char* s)
+{
+ char* p;
+ size_t sz = strlen (s) + 1;
+ if (s == NULL) return NULL;
+ p = (char*)Mem_Alloc (pool, sz);
+ strlcpy (p, s, sz);
+ return p;
+}
+
/*
========================
Memory_Init