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.
35 #define MEMHEADER_SENTINEL_FOR_ADDRESS(p) ((sentinel_seed ^ (unsigned int) (uintptr_t) (p)) + sentinel_seed)
36 unsigned int sentinel_seed;
38 qboolean mem_bigendian = false;
40 // LordHavoc: enables our own low-level allocator (instead of malloc)
42 #define MEMCLUMPING_FREECLUMPS 0
45 // smallest unit we care about is this many bytes
47 // try to do 32MB clumps, but overhead eats into this
48 #define MEMWANTCLUMPSIZE (1<<27)
49 // give malloc padding so we can't waste most of a page at the end
50 #define MEMCLUMPSIZE (MEMWANTCLUMPSIZE - MEMWANTCLUMPSIZE/MEMUNIT/32 - 128)
51 #define MEMBITS (MEMCLUMPSIZE / MEMUNIT)
52 #define MEMBITINTS (MEMBITS / 32)
54 typedef struct memclump_s
56 // contents of the clump
57 unsigned char block[MEMCLUMPSIZE];
58 // should always be MEMCLUMP_SENTINEL
59 unsigned int sentinel1;
60 // if a bit is on, it means that the MEMUNIT bytes it represents are
61 // allocated, otherwise free
62 unsigned int bits[MEMBITINTS];
63 // should always be MEMCLUMP_SENTINEL
64 unsigned int sentinel2;
65 // if this drops to 0, the clump is freed
67 // largest block of memory available (this is reset to an optimistic
68 // number when anything is freed, and updated when alloc fails the clump)
69 size_t largestavailable;
70 // next clump in the chain
71 struct memclump_s *chain;
76 static memclump_t masterclump;
78 static memclump_t *clumpchain = NULL;
82 cvar_t developer_memory = {0, "developer_memory", "0", "prints debugging information about memory allocations"};
83 cvar_t developer_memorydebug = {0, "developer_memorydebug", "0", "enables memory corruption checks (very slow)"};
85 static mempool_t *poolchain = NULL;
87 void Mem_PrintStats(void);
88 void Mem_PrintList(size_t minallocationsize);
91 // some platforms have a malloc that returns NULL but succeeds later
92 // (Windows growing its swapfile for example)
93 static void *attempt_malloc(size_t size)
96 // try for half a second or so
97 unsigned int attempts = 500;
100 base = (void *)malloc(size);
110 static memclump_t *Clump_NewClump(void)
112 memclump_t **clumpchainpointer;
117 clump = &masterclump;
119 clump = (memclump_t*)attempt_malloc(sizeof(memclump_t));
125 if (developer_memorydebug.integer)
126 memset(clump, 0xEF, sizeof(*clump));
127 clump->sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel1);
128 memset(clump->bits, 0, sizeof(clump->bits));
129 clump->sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel2);
130 clump->blocksinuse = 0;
131 clump->largestavailable = 0;
134 // link clump into chain
135 for (clumpchainpointer = &clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
137 *clumpchainpointer = clump;
143 // low level clumping functions, all other memory functions use these
144 static void *Clump_AllocBlock(size_t size)
148 if (size <= MEMCLUMPSIZE)
152 unsigned int needbits;
153 unsigned int startbit;
155 unsigned int needints;
161 memclump_t **clumpchainpointer;
163 needbits = (size + MEMUNIT - 1) / MEMUNIT;
164 needints = (needbits+31)>>5;
165 for (clumpchainpointer = &clumpchain;;clumpchainpointer = &(*clumpchainpointer)->chain)
167 clump = *clumpchainpointer;
170 clump = Clump_NewClump();
174 if (clump->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel1))
175 Sys_Error("Clump_AllocBlock: trashed sentinel1\n");
176 if (clump->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel2))
177 Sys_Error("Clump_AllocBlock: trashed sentinel2\n");
179 endbit = startbit + needbits;
181 // do as fast a search as possible, even if it means crude alignment
184 // large allocations are aligned to large boundaries
185 // furthermore, they are allocated downward from the top...
186 endindex = MEMBITINTS;
187 startindex = endindex - needints;
189 while (--index >= startindex)
194 startindex = endindex - needints;
199 startbit = startindex*32;
204 // search for a multi-bit gap in a single int
205 // (not dealing with the cases that cross two ints)
206 mask = (1<<needbits)-1;
207 endbit = 32-needbits;
209 for (index = 0;index < MEMBITINTS;index++)
211 value = array[index];
212 if (value != 0xFFFFFFFFu)
214 // there may be room in this one...
215 for (bit = 0;bit < endbit;bit++)
217 if (!(value & (mask<<bit)))
219 startbit = index*32+bit;
228 endbit = startbit + needbits;
229 // mark this range as used
231 for (bit = startbit;bit < endbit;bit++)
232 if (clump->bits[bit>>5] & (1<<(bit & 31)))
233 Sys_Error("Clump_AllocBlock: internal error (%i needbits)\n", needbits);
234 for (bit = startbit;bit < endbit;bit++)
235 clump->bits[bit>>5] |= (1<<(bit & 31));
236 clump->blocksinuse += needbits;
237 base = clump->block + startbit * MEMUNIT;
238 if (developer_memorydebug.integer)
239 memset(base, 0xBF, needbits * MEMUNIT);
247 // too big, allocate it directly
252 base = (unsigned char *)attempt_malloc(size);
253 if (base && developer_memorydebug.integer)
254 memset(base, 0xAF, size);
258 static void Clump_FreeBlock(void *base, size_t size)
261 unsigned int needbits;
262 unsigned int startbit;
265 memclump_t **clumpchainpointer;
267 unsigned char *start = (unsigned char *)base;
268 for (clumpchainpointer = &clumpchain;(clump = *clumpchainpointer);clumpchainpointer = &(*clumpchainpointer)->chain)
270 if (start >= clump->block && start < clump->block + MEMCLUMPSIZE)
272 if (clump->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel1))
273 Sys_Error("Clump_FreeBlock: trashed sentinel1\n");
274 if (clump->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel2))
275 Sys_Error("Clump_FreeBlock: trashed sentinel2\n");
276 if (start + size > clump->block + MEMCLUMPSIZE)
277 Sys_Error("Clump_FreeBlock: block overrun\n");
278 // the block belongs to this clump, clear the range
279 needbits = (size + MEMUNIT - 1) / MEMUNIT;
280 startbit = (start - clump->block) / MEMUNIT;
281 endbit = startbit + needbits;
282 // first verify all bits are set, otherwise this may be misaligned or a double free
283 for (bit = startbit;bit < endbit;bit++)
284 if ((clump->bits[bit>>5] & (1<<(bit & 31))) == 0)
285 Sys_Error("Clump_FreeBlock: double free\n");
286 for (bit = startbit;bit < endbit;bit++)
287 clump->bits[bit>>5] &= ~(1<<(bit & 31));
288 clump->blocksinuse -= needbits;
289 memset(base, 0xFF, needbits * MEMUNIT);
290 // if all has been freed, free the clump itself
291 if (clump->blocksinuse == 0)
293 *clumpchainpointer = clump->chain;
294 if (developer_memorydebug.integer)
295 memset(clump, 0xFF, sizeof(*clump));
303 // does not belong to any known chunk... assume it was a direct allocation
306 memset(base, 0xFF, size);
311 void *_Mem_Alloc(mempool_t *pool, void *olddata, size_t size, size_t alignment, const char *filename, int fileline)
313 unsigned int sentinel1;
314 unsigned int sentinel2;
325 _Mem_Free(olddata, filename, fileline);
329 Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
330 if (developer_memory.integer)
331 Con_DPrintf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, (int)size);
332 //if (developer.integer > 0 && developer_memorydebug.integer)
333 // _Mem_CheckSentinelsGlobal(filename, fileline);
334 pool->totalsize += size;
335 realsize = alignment + sizeof(memheader_t) + size + sizeof(sentinel2);
336 pool->realsize += realsize;
337 base = (unsigned char *)Clump_AllocBlock(realsize);
342 Mem_PrintList(1<<30);
344 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
346 // calculate address that aligns the end of the memheader_t to the specified alignment
347 mem = (memheader_t*)((((size_t)base + sizeof(memheader_t) + (alignment-1)) & ~(alignment-1)) - sizeof(memheader_t));
348 mem->baseaddress = (void*)base;
349 mem->filename = filename;
350 mem->fileline = fileline;
354 // calculate sentinels (detects buffer overruns, in a way that is hard to exploit)
355 sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&mem->sentinel);
356 sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS((unsigned char *) mem + sizeof(memheader_t) + mem->size);
357 mem->sentinel = sentinel1;
358 memcpy((unsigned char *) mem + sizeof(memheader_t) + mem->size, &sentinel2, sizeof(sentinel2));
360 // append to head of list
361 mem->next = pool->chain;
365 mem->next->prev = mem;
367 // copy the shared portion in the case of a realloc, then memset the rest
372 oldmem = (memheader_t*)olddata - 1;
373 sharedsize = min(oldmem->size, size);
374 memcpy((void *)((unsigned char *) mem + sizeof(memheader_t)), olddata, sharedsize);
375 remainsize -= sharedsize;
376 _Mem_Free(olddata, filename, fileline);
378 memset((void *)((unsigned char *) mem + sizeof(memheader_t) + sharedsize), 0, remainsize);
379 return (void *)((unsigned char *) mem + sizeof(memheader_t));
382 // only used by _Mem_Free and _Mem_FreePool
383 static void _Mem_FreeBlock(memheader_t *mem, const char *filename, int fileline)
388 unsigned int sentinel1;
389 unsigned int sentinel2;
391 // check sentinels (detects buffer overruns, in a way that is hard to exploit)
392 sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&mem->sentinel);
393 sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS((unsigned char *) mem + sizeof(memheader_t) + mem->size);
394 if (mem->sentinel != sentinel1)
395 Sys_Error("Mem_Free: trashed head sentinel (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
396 if (memcmp((unsigned char *) mem + sizeof(memheader_t) + mem->size, &sentinel2, sizeof(sentinel2)))
397 Sys_Error("Mem_Free: trashed tail sentinel (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
400 if (developer_memory.integer)
401 Con_DPrintf("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));
402 // unlink memheader from doubly linked list
403 if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
404 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
406 mem->prev->next = mem->next;
408 pool->chain = mem->next;
410 mem->next->prev = mem->prev;
411 // memheader has been unlinked, do the actual free now
413 realsize = sizeof(memheader_t) + size + sizeof(sentinel2);
414 pool->totalsize -= size;
415 pool->realsize -= realsize;
416 Clump_FreeBlock(mem->baseaddress, realsize);
419 void _Mem_Free(void *data, const char *filename, int fileline)
423 Con_DPrintf("Mem_Free: data == NULL (called at %s:%i)\n", filename, fileline);
427 if (developer_memorydebug.integer)
429 //_Mem_CheckSentinelsGlobal(filename, fileline);
430 if (!Mem_IsAllocated(NULL, data))
431 Sys_Error("Mem_Free: data is not allocated (called at %s:%i)", filename, fileline);
434 _Mem_FreeBlock((memheader_t *)((unsigned char *) data - sizeof(memheader_t)), filename, fileline);
437 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline)
440 if (developer_memorydebug.integer)
441 _Mem_CheckSentinelsGlobal(filename, fileline);
442 pool = (mempool_t *)Clump_AllocBlock(sizeof(mempool_t));
447 Mem_PrintList(1<<30);
449 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
451 memset(pool, 0, sizeof(mempool_t));
452 pool->sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1);
453 pool->sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2);
454 pool->filename = filename;
455 pool->fileline = fileline;
459 pool->realsize = sizeof(mempool_t);
460 strlcpy (pool->name, name, sizeof (pool->name));
461 pool->parent = parent;
462 pool->next = poolchain;
467 void _Mem_FreePool(mempool_t **poolpointer, const char *filename, int fileline)
469 mempool_t *pool = *poolpointer;
470 mempool_t **chainaddress, *iter, *temp;
472 if (developer_memorydebug.integer)
473 _Mem_CheckSentinelsGlobal(filename, fileline);
476 // unlink pool from chain
477 for (chainaddress = &poolchain;*chainaddress && *chainaddress != pool;chainaddress = &((*chainaddress)->next));
478 if (*chainaddress != pool)
479 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
480 if (pool->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1))
481 Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
482 if (pool->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2))
483 Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
484 *chainaddress = pool->next;
486 // free memory owned by the pool
488 _Mem_FreeBlock(pool->chain, filename, fileline);
490 // free child pools, too
491 for(iter = poolchain; iter; temp = iter = iter->next)
492 if(iter->parent == pool)
493 _Mem_FreePool(&temp, filename, fileline);
495 // free the pool itself
496 Clump_FreeBlock(pool, sizeof(*pool));
502 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
504 mempool_t *chainaddress;
506 if (developer_memorydebug.integer)
508 //_Mem_CheckSentinelsGlobal(filename, fileline);
509 // check if this pool is in the poolchain
510 for (chainaddress = poolchain;chainaddress;chainaddress = chainaddress->next)
511 if (chainaddress == pool)
514 Sys_Error("Mem_EmptyPool: pool is already free (emptypool at %s:%i)", filename, fileline);
517 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
518 if (pool->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1))
519 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
520 if (pool->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2))
521 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
523 // free memory owned by the pool
525 _Mem_FreeBlock(pool->chain, filename, fileline);
527 // empty child pools, too
528 for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
529 if(chainaddress->parent == pool)
530 _Mem_EmptyPool(chainaddress, filename, fileline);
534 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
537 unsigned int sentinel1;
538 unsigned int sentinel2;
541 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
543 mem = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
544 sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&mem->sentinel);
545 sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS((unsigned char *) mem + sizeof(memheader_t) + mem->size);
546 if (mem->sentinel != sentinel1)
547 Sys_Error("Mem_Free: trashed head sentinel (alloc at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
548 if (memcmp((unsigned char *) mem + sizeof(memheader_t) + mem->size, &sentinel2, sizeof(sentinel2)))
549 Sys_Error("Mem_Free: trashed tail sentinel (alloc at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
553 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
555 // this isn't really very useful
556 if (clump->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel1))
557 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
558 if (clump->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel2))
559 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
563 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
570 for (pool = poolchain;pool;pool = pool->next)
572 if (pool->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1))
573 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
574 if (pool->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2))
575 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
577 for (pool = poolchain;pool;pool = pool->next)
578 for (mem = pool->chain;mem;mem = mem->next)
579 _Mem_CheckSentinels((void *)((unsigned char *) mem + sizeof(memheader_t)), filename, fileline);
581 for (pool = poolchain;pool;pool = pool->next)
582 for (clump = clumpchain;clump;clump = clump->chain)
583 _Mem_CheckClumpSentinels(clump, filename, fileline);
587 qboolean Mem_IsAllocated(mempool_t *pool, void *data)
594 // search only one pool
595 target = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
596 for( header = pool->chain ; header ; header = header->next )
597 if( header == target )
603 for (pool = poolchain;pool;pool = pool->next)
604 if (Mem_IsAllocated(pool, data))
610 void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray)
612 memset(l, 0, sizeof(*l));
613 l->mempool = mempool;
614 l->recordsize = recordsize;
615 l->numrecordsperarray = numrecordsperarray;
618 void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l)
623 for (i = 0;i != l->numarrays;i++)
624 Mem_Free(l->arrays[i].data);
627 memset(l, 0, sizeof(*l));
630 // VorteX: hacked Mem_ExpandableArray_AllocRecord, it does allocate record at certain index
631 void *Mem_ExpandableArray_AllocRecordAtIndex(memexpandablearray_t *l, size_t index)
634 if (index == l->numarrays)
636 if (l->numarrays == l->maxarrays)
638 memexpandablearray_array_t *oldarrays = l->arrays;
639 l->maxarrays = max(l->maxarrays * 2, 128);
640 l->arrays = (memexpandablearray_array_t*) Mem_Alloc(l->mempool, l->maxarrays * sizeof(*l->arrays));
643 memcpy(l->arrays, oldarrays, l->numarrays * sizeof(*l->arrays));
647 l->arrays[index].numflaggedrecords = 0;
648 l->arrays[index].data = (unsigned char *) Mem_Alloc(l->mempool, (l->recordsize + 1) * l->numrecordsperarray);
649 l->arrays[index].allocflags = l->arrays[index].data + l->recordsize * l->numrecordsperarray;
652 if (l->arrays[index].numflaggedrecords < l->numrecordsperarray)
654 for (j = 0;j < l->numrecordsperarray;j++)
656 if (!l->arrays[index].allocflags[j])
658 l->arrays[index].allocflags[j] = true;
659 l->arrays[index].numflaggedrecords++;
660 memset(l->arrays[index].data + l->recordsize * j, 0, l->recordsize);
661 return (void *)(l->arrays[index].data + l->recordsize * j);
668 void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l)
673 if (i == l->numarrays)
675 if (l->numarrays == l->maxarrays)
677 memexpandablearray_array_t *oldarrays = l->arrays;
678 l->maxarrays = max(l->maxarrays * 2, 128);
679 l->arrays = (memexpandablearray_array_t*) Mem_Alloc(l->mempool, l->maxarrays * sizeof(*l->arrays));
682 memcpy(l->arrays, oldarrays, l->numarrays * sizeof(*l->arrays));
686 l->arrays[i].numflaggedrecords = 0;
687 l->arrays[i].data = (unsigned char *) Mem_Alloc(l->mempool, (l->recordsize + 1) * l->numrecordsperarray);
688 l->arrays[i].allocflags = l->arrays[i].data + l->recordsize * l->numrecordsperarray;
691 if (l->arrays[i].numflaggedrecords < l->numrecordsperarray)
693 for (j = 0;j < l->numrecordsperarray;j++)
695 if (!l->arrays[i].allocflags[j])
697 l->arrays[i].allocflags[j] = true;
698 l->arrays[i].numflaggedrecords++;
699 memset(l->arrays[i].data + l->recordsize * j, 0, l->recordsize);
700 return (void *)(l->arrays[i].data + l->recordsize * j);
707 /*****************************************************************************
709 * If this function was to change the size of the "expandable" array, you have
710 * to update r_shadow.c
711 * Just do a search for "range =", R_ShadowClearWorldLights would be the first
712 * function to look at. (And also seems like the only one?) You might have to
713 * move the call to Mem_ExpandableArray_IndexRange back into for(...) loop's
716 void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record) // const!
719 unsigned char *p = (unsigned char *)record;
720 for (i = 0;i != l->numarrays;i++)
722 if (p >= l->arrays[i].data && p < (l->arrays[i].data + l->recordsize * l->numrecordsperarray))
724 j = (p - l->arrays[i].data) / l->recordsize;
725 if (p != l->arrays[i].data + j * l->recordsize)
726 Sys_Error("Mem_ExpandableArray_FreeRecord: no such record %p\n", p);
727 if (!l->arrays[i].allocflags[j])
728 Sys_Error("Mem_ExpandableArray_FreeRecord: record %p is already free!\n", p);
729 l->arrays[i].allocflags[j] = false;
730 l->arrays[i].numflaggedrecords--;
736 size_t Mem_ExpandableArray_IndexRange(const memexpandablearray_t *l)
738 size_t i, j, k, end = 0;
739 for (i = 0;i < l->numarrays;i++)
741 for (j = 0, k = 0;k < l->arrays[i].numflaggedrecords;j++)
743 if (l->arrays[i].allocflags[j])
745 end = l->numrecordsperarray * i + j + 1;
753 void *Mem_ExpandableArray_RecordAtIndex(const memexpandablearray_t *l, size_t index)
756 i = index / l->numrecordsperarray;
757 j = index % l->numrecordsperarray;
758 if (i >= l->numarrays || !l->arrays[i].allocflags[j])
760 return (void *)(l->arrays[i].data + j * l->recordsize);
764 // used for temporary memory allocations around the engine, not for longterm
765 // storage, if anything in this pool stays allocated during gameplay, it is
767 mempool_t *tempmempool;
769 mempool_t *zonemempool;
771 void Mem_PrintStats(void)
773 size_t count = 0, size = 0, realsize = 0;
776 Mem_CheckSentinelsGlobal();
777 for (pool = poolchain;pool;pool = pool->next)
780 size += pool->totalsize;
781 realsize += pool->realsize;
783 Con_Printf("%lu memory pools, totalling %lu bytes (%.3fMB)\n", (unsigned long)count, (unsigned long)size, size / 1048576.0);
784 Con_Printf("total allocated size: %lu bytes (%.3fMB)\n", (unsigned long)realsize, realsize / 1048576.0);
785 for (pool = poolchain;pool;pool = pool->next)
787 if ((pool->flags & POOLFLAG_TEMP) && pool->chain)
789 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);
790 for (mem = pool->chain;mem;mem = mem->next)
791 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
796 void Mem_PrintList(size_t minallocationsize)
800 Mem_CheckSentinelsGlobal();
801 Con_Print("memory pool list:\n"
803 for (pool = poolchain;pool;pool = pool->next)
805 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" : "");
806 pool->lastchecksize = pool->totalsize;
807 for (mem = pool->chain;mem;mem = mem->next)
808 if (mem->size >= minallocationsize)
809 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
818 Mem_PrintList(1<<30);
822 Mem_PrintList(atoi(Cmd_Argv(1)) * 1024);
826 Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
831 extern void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal);
832 void MemStats_f(void)
834 Mem_CheckSentinelsGlobal();
835 R_TextureStats_Print(false, false, true);
836 GL_Mesh_ListVBOs(false);
841 char* Mem_strdup (mempool_t *pool, const char* s)
844 size_t sz = strlen (s) + 1;
845 if (s == NULL) return NULL;
846 p = (char*)Mem_Alloc (pool, sz);
852 ========================
854 ========================
856 void Memory_Init (void)
858 static union {unsigned short s;unsigned char b[2];} u;
860 mem_bigendian = u.b[0] != 0;
862 sentinel_seed = rand();
864 tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL);
865 zonemempool = Mem_AllocPool("Zone", 0, NULL);
868 void Memory_Shutdown (void)
870 // Mem_FreePool (&zonemempool);
871 // Mem_FreePool (&tempmempool);
874 void Memory_Init_Commands (void)
876 Cmd_AddCommand ("memstats", MemStats_f, "prints memory system statistics");
877 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)");
878 Cvar_RegisterVariable (&developer_memory);
879 Cvar_RegisterVariable (&developer_memorydebug);