2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #define MEMHEADER_SENTINEL_FOR_ADDRESS(p) ((sentinel_seed ^ (unsigned int) (uintptr_t) (p)) + sentinel_seed)
31 unsigned int sentinel_seed;
33 #define MEMHEADER_SENTINEL1 0xDEADF00D
34 #define MEMHEADER_SENTINEL2 0xDF
38 #define MEMCLUMP_SENTINEL 0xABADCAFE
41 cvar_t developer_memory = {0, "developer_memory", "0", "prints debugging information about memory allocations"};
42 cvar_t developer_memorydebug = {0, "developer_memorydebug", "0", "enables memory corruption checks (very slow)"};
44 mempool_t *poolchain = NULL;
46 void *_Mem_Alloc(mempool_t *pool, size_t size, const char *filename, int fileline)
49 int i, j, k, needed, endbit, largest;
50 memclump_t *clump, **clumpchainpointer;
53 unsigned int sentinel2;
59 Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
60 if (developer.integer && developer_memory.integer)
61 Con_Printf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, (int)size);
62 if (developer.integer && developer_memorydebug.integer)
63 _Mem_CheckSentinelsGlobal(filename, fileline);
64 pool->totalsize += size;
70 needed = (sizeof(memheader_t) + size + sizeof(unsigned int) + (MEMUNIT - 1)) / MEMUNIT;
72 needed = (sizeof(memheader_t) + size + sizeof(unsigned char) + (MEMUNIT - 1)) / MEMUNIT;
74 endbit = MEMBITS - needed;
75 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
77 clump = *clumpchainpointer;
78 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
79 Sys_Error("Mem_Alloc: trashed clump sentinel 1 (alloc at %s:%d)", filename, fileline);
80 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
81 Sys_Error("Mem_Alloc: trashed clump sentinel 2 (alloc at %s:%d)", filename, fileline);
82 if (clump->largestavailable >= needed)
85 for (i = 0;i < endbit;i++)
87 if (clump->bits[i >> 5] & (1 << (i & 31)))
91 if (clump->bits[i >> 5] & (1 << (i & 31)))
98 // since clump falsely advertised enough space (nothing wrong
99 // with that), update largest count to avoid wasting time in
101 clump->largestavailable = largest;
104 pool->realsize += sizeof(memclump_t);
105 clump = malloc(sizeof(memclump_t));
107 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
108 memset(clump, 0, sizeof(memclump_t));
109 *clumpchainpointer = clump;
110 clump->sentinel1 = MEMCLUMP_SENTINEL;
111 clump->sentinel2 = MEMCLUMP_SENTINEL;
113 clump->blocksinuse = 0;
114 clump->largestavailable = MEMBITS - needed;
117 mem = (memheader_t *)((unsigned char *) clump->block + j * MEMUNIT);
119 clump->blocksinuse += needed;
120 for (i = j + needed;j < i;j++)
121 clump->bits[j >> 5] |= (1 << (j & 31));
125 // big allocations are not clumped
128 pool->realsize += sizeof(memheader_t) + size + sizeof(sentinel2);
129 mem = (memheader_t *)malloc(sizeof(memheader_t) + size + sizeof(sentinel2));
131 pool->realsize += sizeof(memheader_t) + size + 1;
132 mem = (memheader_t *)malloc(sizeof(memheader_t) + size + 1);
135 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
140 mem->filename = filename;
141 mem->fileline = fileline;
145 mem->sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&mem->sentinel1);
146 sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS((unsigned char *) mem + sizeof(memheader_t) + mem->size);
147 memcpy((unsigned char *) mem + sizeof(memheader_t) + mem->size, &sentinel2, sizeof(sentinel2));
149 mem->sentinel1 = MEMHEADER_SENTINEL1;
150 // we have to use only a single byte for this sentinel, because it may not be aligned, and some platforms can't use unaligned accesses
151 *((unsigned char *) mem + sizeof(memheader_t) + mem->size) = MEMHEADER_SENTINEL2;
153 // append to head of list
154 mem->next = pool->chain;
158 mem->next->prev = mem;
159 memset((void *)((unsigned char *) mem + sizeof(memheader_t)), 0, mem->size);
160 return (void *)((unsigned char *) mem + sizeof(memheader_t));
163 // only used by _Mem_Free and _Mem_FreePool
164 static void _Mem_FreeBlock(memheader_t *mem, const char *filename, int fileline)
167 int i, firstblock, endblock;
168 memclump_t *clump, **clumpchainpointer;
172 unsigned int sentinel2;
173 if (mem->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&mem->sentinel1))
174 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
175 sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS((unsigned char *) mem + sizeof(memheader_t) + mem->size);
176 if (memcmp((unsigned char *) mem + sizeof(memheader_t) + mem->size, &sentinel2, sizeof(sentinel2)))
177 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
179 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
180 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
181 if (*((unsigned char *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
182 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
185 if (developer.integer && developer_memory.integer)
186 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));
187 // unlink memheader from doubly linked list
188 if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
189 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
191 mem->prev->next = mem->next;
193 pool->chain = mem->next;
195 mem->next->prev = mem->prev;
196 // memheader has been unlinked, do the actual free now
197 pool->totalsize -= mem->size;
199 if ((clump = mem->clump))
201 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
202 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
203 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
204 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
205 firstblock = ((unsigned char *) mem - (unsigned char *) clump->block);
206 if (firstblock & (MEMUNIT - 1))
207 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
208 firstblock /= MEMUNIT;
209 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
210 clump->blocksinuse -= endblock - firstblock;
211 // could use &, but we know the bit is set
212 for (i = firstblock;i < endblock;i++)
213 clump->bits[i >> 5] -= (1 << (i & 31));
214 if (clump->blocksinuse <= 0)
217 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
219 if (*clumpchainpointer == clump)
221 *clumpchainpointer = clump->chain;
225 pool->realsize -= sizeof(memclump_t);
226 memset(clump, 0xBF, sizeof(memclump_t));
231 // clump still has some allocations
232 // force re-check of largest available space on next alloc
233 clump->largestavailable = MEMBITS - clump->blocksinuse;
239 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
240 if (developer_memorydebug.integer)
241 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
248 void _Mem_Free(void *data, const char *filename, int fileline)
252 Con_DPrintf("Mem_Free: data == NULL (called at %s:%i)\n", filename, fileline);
256 if (developer.integer && developer_memorydebug.integer)
258 _Mem_CheckSentinelsGlobal(filename, fileline);
259 if (!Mem_IsAllocated(NULL, data))
260 Sys_Error("Mem_Free: data is not allocated (called at %s:%i)", filename, fileline);
263 _Mem_FreeBlock((memheader_t *)((unsigned char *) data - sizeof(memheader_t)), filename, fileline);
266 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline)
269 if (developer.integer && developer_memorydebug.integer)
270 _Mem_CheckSentinelsGlobal(filename, fileline);
271 pool = (mempool_t *)malloc(sizeof(mempool_t));
273 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
274 memset(pool, 0, sizeof(mempool_t));
276 pool->sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1);
277 pool->sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2);
279 pool->sentinel1 = MEMHEADER_SENTINEL1;
280 pool->sentinel2 = MEMHEADER_SENTINEL1;
282 pool->filename = filename;
283 pool->fileline = fileline;
287 pool->realsize = sizeof(mempool_t);
288 strlcpy (pool->name, name, sizeof (pool->name));
289 pool->parent = parent;
290 pool->next = poolchain;
295 void _Mem_FreePool(mempool_t **poolpointer, const char *filename, int fileline)
297 mempool_t *pool = *poolpointer;
298 mempool_t **chainaddress, *iter, *temp;
300 if (developer.integer && developer_memorydebug.integer)
301 _Mem_CheckSentinelsGlobal(filename, fileline);
304 // unlink pool from chain
305 for (chainaddress = &poolchain;*chainaddress && *chainaddress != pool;chainaddress = &((*chainaddress)->next));
306 if (*chainaddress != pool)
307 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
309 if (pool->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1))
310 Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
311 if (pool->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2))
312 Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
314 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
315 Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
316 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
317 Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
319 *chainaddress = pool->next;
321 // free memory owned by the pool
323 _Mem_FreeBlock(pool->chain, filename, fileline);
325 // free child pools, too
326 for(iter = poolchain; iter; temp = iter = iter->next)
327 if(iter->parent == pool)
328 _Mem_FreePool(&temp, filename, fileline);
330 // free the pool itself
331 memset(pool, 0xBF, sizeof(mempool_t));
338 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
340 mempool_t *chainaddress;
342 if (developer.integer && developer_memorydebug.integer)
344 _Mem_CheckSentinelsGlobal(filename, fileline);
345 // check if this pool is in the poolchain
346 for (chainaddress = poolchain;chainaddress;chainaddress = chainaddress->next)
347 if (chainaddress == pool)
350 Sys_Error("Mem_EmptyPool: pool is already free (emptypool at %s:%i)", filename, fileline);
353 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
355 if (pool->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1))
356 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
357 if (pool->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2))
358 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
360 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
361 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
362 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
363 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
366 // free memory owned by the pool
368 _Mem_FreeBlock(pool->chain, filename, fileline);
370 // empty child pools, too
371 for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
372 if(chainaddress->parent == pool)
373 _Mem_EmptyPool(chainaddress, filename, fileline);
377 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
381 unsigned int sentinel2;
385 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
387 mem = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
389 if (mem->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&mem->sentinel1))
390 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
391 sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS((unsigned char *) mem + sizeof(memheader_t) + mem->size);
392 if (memcmp((unsigned char *) mem + sizeof(memheader_t) + mem->size, &sentinel2, sizeof(sentinel2)))
393 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
395 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
396 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
397 if (*((unsigned char *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
398 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
403 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
405 // this isn't really very useful
406 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
407 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
408 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
409 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
413 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
420 for (pool = poolchain;pool;pool = pool->next)
423 if (pool->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1))
424 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
425 if (pool->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2))
426 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
428 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
429 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
430 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
431 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
434 for (pool = poolchain;pool;pool = pool->next)
435 for (mem = pool->chain;mem;mem = mem->next)
436 _Mem_CheckSentinels((void *)((unsigned char *) mem + sizeof(memheader_t)), filename, fileline);
438 for (pool = poolchain;pool;pool = pool->next)
439 for (clump = pool->clumpchain;clump;clump = clump->chain)
440 _Mem_CheckClumpSentinels(clump, filename, fileline);
444 qboolean Mem_IsAllocated(mempool_t *pool, void *data)
451 // search only one pool
452 target = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
453 for( header = pool->chain ; header ; header = header->next )
454 if( header == target )
460 for (pool = poolchain;pool;pool = pool->next)
461 if (Mem_IsAllocated(pool, data))
467 void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray)
469 memset(l, 0, sizeof(*l));
470 l->mempool = mempool;
471 l->recordsize = recordsize;
472 l->numrecordsperarray = numrecordsperarray;
475 void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l)
480 for (i = 0;i != l->numarrays;i++)
481 Mem_Free(l->arrays[i].data);
484 memset(l, 0, sizeof(*l));
487 void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l)
492 if (i == l->numarrays)
494 if (l->numarrays == l->maxarrays)
496 memexpandablearray_array_t *oldarrays = l->arrays;
497 l->maxarrays = max(l->maxarrays * 2, 128);
498 l->arrays = (memexpandablearray_array_t*) Mem_Alloc(l->mempool, l->maxarrays * sizeof(*l->arrays));
501 memcpy(l->arrays, oldarrays, l->numarrays * sizeof(*l->arrays));
505 l->arrays[i].numflaggedrecords = 0;
506 l->arrays[i].data = (unsigned char *) Mem_Alloc(l->mempool, (l->recordsize + 1) * l->numrecordsperarray);
507 l->arrays[i].allocflags = l->arrays[i].data + l->recordsize * l->numrecordsperarray;
510 if (l->arrays[i].numflaggedrecords < l->numrecordsperarray)
512 for (j = 0;j < l->numrecordsperarray;j++)
514 if (!l->arrays[i].allocflags[j])
516 l->arrays[i].allocflags[j] = true;
517 l->arrays[i].numflaggedrecords++;
518 memset(l->arrays[i].data + l->recordsize * j, 0, l->recordsize);
519 return (void *)(l->arrays[i].data + l->recordsize * j);
526 /*****************************************************************************
528 * If this function was to change the size of the "expandable" array, you have
529 * to update r_shadow.c
530 * Just do a search for "range =", R_ShadowClearWorldLights would be the first
531 * function to look at. (And also seems like the only one?) You might have to
532 * move the call to Mem_ExpandableArray_IndexRange back into for(...) loop's
535 void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record) // const!
538 unsigned char *p = (unsigned char *)record;
539 for (i = 0;i != l->numarrays;i++)
541 if (p >= l->arrays[i].data && p < (l->arrays[i].data + l->recordsize * l->numrecordsperarray))
543 j = (p - l->arrays[i].data) / l->recordsize;
544 if (p != l->arrays[i].data + j * l->recordsize)
545 Sys_Error("Mem_ExpandableArray_FreeRecord: no such record %p\n", p);
546 if (!l->arrays[i].allocflags[j])
547 Sys_Error("Mem_ExpandableArray_FreeRecord: record %p is already free!\n", p);
548 l->arrays[i].allocflags[j] = false;
549 l->arrays[i].numflaggedrecords--;
555 size_t Mem_ExpandableArray_IndexRange(const memexpandablearray_t *l)
557 size_t i, j, k, end = 0;
558 for (i = 0;i < l->numarrays;i++)
560 for (j = 0, k = 0;k < l->arrays[i].numflaggedrecords;j++)
562 if (l->arrays[i].allocflags[j])
564 end = l->numrecordsperarray * i + j + 1;
572 void *Mem_ExpandableArray_RecordAtIndex(const memexpandablearray_t *l, size_t index)
575 i = index / l->numrecordsperarray;
576 j = index % l->numrecordsperarray;
577 if (i >= l->numarrays || !l->arrays[i].allocflags[j])
579 return (void *)(l->arrays[i].data + j * l->recordsize);
583 // used for temporary memory allocations around the engine, not for longterm
584 // storage, if anything in this pool stays allocated during gameplay, it is
586 mempool_t *tempmempool;
588 mempool_t *zonemempool;
590 void Mem_PrintStats(void)
592 size_t count = 0, size = 0, realsize = 0;
595 Mem_CheckSentinelsGlobal();
596 for (pool = poolchain;pool;pool = pool->next)
599 size += pool->totalsize;
600 realsize += pool->realsize;
602 Con_Printf("%lu memory pools, totalling %lu bytes (%.3fMB)\n", (unsigned long)count, (unsigned long)size, size / 1048576.0);
603 Con_Printf("total allocated size: %lu bytes (%.3fMB)\n", (unsigned long)realsize, realsize / 1048576.0);
604 for (pool = poolchain;pool;pool = pool->next)
606 if ((pool->flags & POOLFLAG_TEMP) && pool->chain)
608 Con_Printf("Memory pool %p has sprung a leak totalling %lu bytes (%.3fMB)! Listing contents...\n", (void *)pool, (unsigned long)pool->totalsize, pool->totalsize / 1048576.0);
609 for (mem = pool->chain;mem;mem = mem->next)
610 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
615 void Mem_PrintList(size_t minallocationsize)
619 Mem_CheckSentinelsGlobal();
620 Con_Print("memory pool list:\n"
622 for (pool = poolchain;pool;pool = pool->next)
624 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" : "");
625 pool->lastchecksize = pool->totalsize;
626 for (mem = pool->chain;mem;mem = mem->next)
627 if (mem->size >= minallocationsize)
628 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
637 Mem_PrintList(1<<30);
641 Mem_PrintList(atoi(Cmd_Argv(1)) * 1024);
645 Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
650 extern void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal);
651 void MemStats_f(void)
653 Mem_CheckSentinelsGlobal();
654 R_TextureStats_Print(false, false, true);
655 GL_Mesh_ListVBOs(false);
660 char* Mem_strdup (mempool_t *pool, const char* s)
663 size_t sz = strlen (s) + 1;
664 if (s == NULL) return NULL;
665 p = (char*)Mem_Alloc (pool, sz);
671 ========================
673 ========================
675 void Memory_Init (void)
678 sentinel_seed = rand();
681 tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL);
682 zonemempool = Mem_AllocPool("Zone", 0, NULL);
685 void Memory_Shutdown (void)
687 // Mem_FreePool (&zonemempool);
688 // Mem_FreePool (&tempmempool);
691 void Memory_Init_Commands (void)
693 Cmd_AddCommand ("memstats", MemStats_f, "prints memory system statistics");
694 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)");
695 Cvar_RegisterVariable (&developer_memory);
696 Cvar_RegisterVariable (&developer_memorydebug);