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.
22 #include "darkplaces.h"
31 #define MEMHEADER_SENTINEL_FOR_ADDRESS(p) ((sentinel_seed ^ (unsigned int) (uintptr_t) (p)) + sentinel_seed)
32 unsigned int sentinel_seed;
34 qbool mem_bigendian = false;
35 void *mem_mutex = NULL;
37 // divVerent: enables file backed malloc using mmap to conserve swap space (instead of malloc)
38 #ifndef FILE_BACKED_MALLOC
39 # define FILE_BACKED_MALLOC 0
42 // LadyHavoc: enables our own low-level allocator (instead of malloc)
44 # define MEMCLUMPING 0
46 #ifndef MEMCLUMPING_FREECLUMPS
47 # define MEMCLUMPING_FREECLUMPS 0
51 // smallest unit we care about is this many bytes
53 // try to do 32MB clumps, but overhead eats into this
54 #ifndef MEMWANTCLUMPSIZE
55 # define MEMWANTCLUMPSIZE (1<<27)
57 // give malloc padding so we can't waste most of a page at the end
58 #define MEMCLUMPSIZE (MEMWANTCLUMPSIZE - MEMWANTCLUMPSIZE/MEMUNIT/32 - 128)
59 #define MEMBITS (MEMCLUMPSIZE / MEMUNIT)
60 #define MEMBITINTS (MEMBITS / 32)
62 typedef struct memclump_s
64 // contents of the clump
65 unsigned char block[MEMCLUMPSIZE];
66 // should always be MEMCLUMP_SENTINEL
67 unsigned int sentinel1;
68 // if a bit is on, it means that the MEMUNIT bytes it represents are
69 // allocated, otherwise free
70 unsigned int bits[MEMBITINTS];
71 // should always be MEMCLUMP_SENTINEL
72 unsigned int sentinel2;
73 // if this drops to 0, the clump is freed
75 // largest block of memory available (this is reset to an optimistic
76 // number when anything is freed, and updated when alloc fails the clump)
77 size_t largestavailable;
78 // next clump in the chain
79 struct memclump_s *chain;
84 static memclump_t masterclump;
86 static memclump_t *clumpchain = NULL;
90 cvar_t developer_memory = {CF_CLIENT | CF_SERVER, "developer_memory", "0", "prints debugging information about memory allocations"};
91 cvar_t developer_memorydebug = {CF_CLIENT | CF_SERVER, "developer_memorydebug", "0", "enables memory corruption checks (very slow)"};
92 cvar_t developer_memoryreportlargerthanmb = {CF_CLIENT | CF_SERVER, "developer_memorylargerthanmb", "16", "prints debugging information about memory allocations over this size"};
93 cvar_t sys_memsize_physical = {CF_CLIENT | CF_SERVER | CF_READONLY, "sys_memsize_physical", "", "physical memory size in MB (or empty if unknown)"};
94 cvar_t sys_memsize_virtual = {CF_CLIENT | CF_SERVER | CF_READONLY, "sys_memsize_virtual", "", "virtual memory size in MB (or empty if unknown)"};
96 static mempool_t *poolchain = NULL;
98 void Mem_PrintStats(void);
99 void Mem_PrintList(size_t minallocationsize);
101 #if FILE_BACKED_MALLOC
103 #include <sys/mman.h>
104 #ifndef MAP_NORESERVE
105 #define MAP_NORESERVE 0
107 typedef struct mmap_data_s
112 static void *mmap_malloc(size_t size)
114 char vabuf[MAX_OSPATH + 1];
115 char *tmpdir = getenv("TEMP");
118 size += sizeof(mmap_data_t); // waste block
119 dpsnprintf(vabuf, sizeof(vabuf), "%s/darkplaces.XXXXXX", tmpdir ? tmpdir : "/tmp");
124 data = (unsigned char *) mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, fd, 0);
127 if(!data || data == (void *)-1)
130 return (void *) (data + 1);
132 static void mmap_free(void *mem)
137 data = ((mmap_data_t *) mem) - 1;
138 munmap(data, data->len);
140 #define malloc mmap_malloc
141 #define free mmap_free
145 // some platforms have a malloc that returns NULL but succeeds later
146 // (Windows growing its swapfile for example)
147 static void *attempt_malloc(size_t size)
153 // try for half a second or so
154 unsigned int attempts = 500;
157 base = (void *)malloc(size);
168 static memclump_t *Clump_NewClump(void)
170 memclump_t **clumpchainpointer;
175 clump = &masterclump;
177 clump = (memclump_t*)attempt_malloc(sizeof(memclump_t));
183 if (developer_memorydebug.integer)
184 memset(clump, 0xEF, sizeof(*clump));
185 clump->sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel1);
186 memset(clump->bits, 0, sizeof(clump->bits));
187 clump->sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel2);
188 clump->blocksinuse = 0;
189 clump->largestavailable = 0;
192 // link clump into chain
193 for (clumpchainpointer = &clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
195 *clumpchainpointer = clump;
201 // low level clumping functions, all other memory functions use these
202 static void *Clump_AllocBlock(size_t size)
206 if (size <= MEMCLUMPSIZE)
219 memclump_t **clumpchainpointer;
221 needbits = (size + MEMUNIT - 1) / MEMUNIT;
222 needints = (needbits+31)>>5;
223 for (clumpchainpointer = &clumpchain;;clumpchainpointer = &(*clumpchainpointer)->chain)
225 clump = *clumpchainpointer;
228 clump = Clump_NewClump();
232 if (clump->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel1))
233 Sys_Abort("Clump_AllocBlock: trashed sentinel1\n");
234 if (clump->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel2))
235 Sys_Abort("Clump_AllocBlock: trashed sentinel2\n");
237 endbit = startbit + needbits;
239 // do as fast a search as possible, even if it means crude alignment
242 // large allocations are aligned to large boundaries
243 // furthermore, they are allocated downward from the top...
244 endindex = MEMBITINTS;
245 startindex = endindex - needints;
247 while (--index >= startindex)
252 startindex = endindex - needints;
257 startbit = startindex*32;
262 // search for a multi-bit gap in a single int
263 // (not dealing with the cases that cross two ints)
264 mask = (1<<needbits)-1;
265 endbit = 32-needbits;
267 for (index = 0;index < MEMBITINTS;index++)
269 value = array[index];
270 if (value != 0xFFFFFFFFu)
272 // there may be room in this one...
273 for (bit = 0;bit < endbit;bit++)
275 if (!(value & (mask<<bit)))
277 startbit = index*32+bit;
286 endbit = startbit + needbits;
287 // mark this range as used
289 for (bit = startbit;bit < endbit;bit++)
290 if (clump->bits[bit>>5] & (1<<(bit & 31)))
291 Sys_Abort("Clump_AllocBlock: internal error (%i needbits)\n", needbits);
292 for (bit = startbit;bit < endbit;bit++)
293 clump->bits[bit>>5] |= (1<<(bit & 31));
294 clump->blocksinuse += needbits;
295 base = clump->block + startbit * MEMUNIT;
296 if (developer_memorydebug.integer)
297 memset(base, 0xBF, needbits * MEMUNIT);
305 // too big, allocate it directly
310 base = (unsigned char *)attempt_malloc(size);
311 if (base && developer_memorydebug.integer)
312 memset(base, 0xAF, size);
316 static void Clump_FreeBlock(void *base, size_t size)
323 memclump_t **clumpchainpointer;
325 unsigned char *start = (unsigned char *)base;
326 for (clumpchainpointer = &clumpchain;(clump = *clumpchainpointer);clumpchainpointer = &(*clumpchainpointer)->chain)
328 if (start >= clump->block && start < clump->block + MEMCLUMPSIZE)
330 if (clump->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel1))
331 Sys_Abort("Clump_FreeBlock: trashed sentinel1\n");
332 if (clump->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel2))
333 Sys_Abort("Clump_FreeBlock: trashed sentinel2\n");
334 if (start + size > clump->block + MEMCLUMPSIZE)
335 Sys_Abort("Clump_FreeBlock: block overrun\n");
336 // the block belongs to this clump, clear the range
337 needbits = (size + MEMUNIT - 1) / MEMUNIT;
338 startbit = (start - clump->block) / MEMUNIT;
339 endbit = startbit + needbits;
340 // first verify all bits are set, otherwise this may be misaligned or a double free
341 for (bit = startbit;bit < endbit;bit++)
342 if ((clump->bits[bit>>5] & (1<<(bit & 31))) == 0)
343 Sys_Abort("Clump_FreeBlock: double free\n");
344 for (bit = startbit;bit < endbit;bit++)
345 clump->bits[bit>>5] &= ~(1<<(bit & 31));
346 clump->blocksinuse -= needbits;
347 memset(base, 0xFF, needbits * MEMUNIT);
348 // if all has been freed, free the clump itself
349 if (clump->blocksinuse == 0)
351 *clumpchainpointer = clump->chain;
352 if (developer_memorydebug.integer)
353 memset(clump, 0xFF, sizeof(*clump));
361 // does not belong to any known chunk... assume it was a direct allocation
364 memset(base, 0xFF, size);
369 void *_Mem_Alloc(mempool_t *pool, void *olddata, size_t size, size_t alignment, const char *filename, int fileline)
371 unsigned int sentinel1;
372 unsigned int sentinel2;
383 _Mem_Free(olddata, filename, fileline);
389 pool = ((memheader_t *)((unsigned char *) olddata - sizeof(memheader_t)))->pool;
391 Sys_Abort("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
394 Thread_LockMutex(mem_mutex);
395 if (developer_memory.integer || size >= developer_memoryreportlargerthanmb.value * 1048576)
396 Con_DPrintf("Mem_Alloc: pool %s, file %s:%i, size %f bytes (%f MB)\n", pool->name, filename, fileline, (double)size, (double)size / 1048576.0f);
397 //if (developer.integer > 0 && developer_memorydebug.integer)
398 // _Mem_CheckSentinelsGlobal(filename, fileline);
399 pool->totalsize += size;
400 // calculate the smallest realsize that is a multiple of alignment
401 realsize = (sizeof(memheader_t) + size + sizeof(sentinel2) + (alignment-1)) & ~(alignment-1);
402 pool->realsize += realsize;
403 base = (unsigned char *)Clump_AllocBlock(realsize);
408 Mem_PrintList(1<<30);
410 Sys_Abort("Mem_Alloc: out of memory (alloc of size %f (%.3fMB) at %s:%i)", (double)realsize, (double)realsize / (1 << 20), filename, fileline);
412 // calculate address that aligns the end of the memheader_t to the specified alignment
413 mem = (memheader_t*)((((size_t)base + sizeof(memheader_t) + (alignment-1)) & ~(alignment-1)) - sizeof(memheader_t));
414 mem->baseaddress = (void*)base;
415 mem->filename = filename;
416 mem->fileline = fileline;
420 // calculate sentinels (detects buffer overruns, in a way that is hard to exploit)
421 sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&mem->sentinel);
422 sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS((unsigned char *) mem + sizeof(memheader_t) + mem->size);
423 mem->sentinel = sentinel1;
424 memcpy((unsigned char *) mem + sizeof(memheader_t) + mem->size, &sentinel2, sizeof(sentinel2));
426 // append to head of list
427 List_Add(&mem->list, &pool->chain);
430 Thread_UnlockMutex(mem_mutex);
432 // copy the shared portion in the case of a realloc, then memset the rest
437 oldmem = (memheader_t*)olddata - 1;
438 sharedsize = min(oldmem->size, size);
439 memcpy((void *)((unsigned char *) mem + sizeof(memheader_t)), olddata, sharedsize);
440 remainsize -= sharedsize;
441 _Mem_Free(olddata, filename, fileline);
443 memset((void *)((unsigned char *) mem + sizeof(memheader_t) + sharedsize), 0, remainsize);
444 return (void *)((unsigned char *) mem + sizeof(memheader_t));
447 // only used by _Mem_Free and _Mem_FreePool
448 static void _Mem_FreeBlock(memheader_t *mem, const char *filename, int fileline)
453 unsigned int sentinel1;
454 unsigned int sentinel2;
456 // check sentinels (detects buffer overruns, in a way that is hard to exploit)
457 sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&mem->sentinel);
458 sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS((unsigned char *) mem + sizeof(memheader_t) + mem->size);
459 if (mem->sentinel != sentinel1)
460 Sys_Abort("Mem_Free: trashed head sentinel (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
461 if (memcmp((unsigned char *) mem + sizeof(memheader_t) + mem->size, &sentinel2, sizeof(sentinel2)))
462 Sys_Abort("Mem_Free: trashed tail sentinel (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
465 if (developer_memory.integer)
466 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));
467 // unlink memheader from doubly linked list
468 if (mem->list.prev->next != &mem->list || mem->list.next->prev != &mem->list)
469 Sys_Abort("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
471 Thread_LockMutex(mem_mutex);
472 List_Delete(&mem->list);
473 // memheader has been unlinked, do the actual free now
475 realsize = sizeof(memheader_t) + size + sizeof(sentinel2);
476 pool->totalsize -= size;
477 pool->realsize -= realsize;
478 Clump_FreeBlock(mem->baseaddress, realsize);
480 Thread_UnlockMutex(mem_mutex);
483 void _Mem_Free(void *data, const char *filename, int fileline)
487 Con_DPrintf("Mem_Free: data == NULL (called at %s:%i)\n", filename, fileline);
491 if (developer_memorydebug.integer)
493 //_Mem_CheckSentinelsGlobal(filename, fileline);
494 if (!Mem_IsAllocated(NULL, data))
495 Sys_Abort("Mem_Free: data is not allocated (called at %s:%i)", filename, fileline);
498 _Mem_FreeBlock((memheader_t *)((unsigned char *) data - sizeof(memheader_t)), filename, fileline);
501 mempool_t *_Mem_AllocPool(const char *name, unsigned flags, mempool_t *parent, const char *filename, int fileline)
504 if (developer_memorydebug.integer)
505 _Mem_CheckSentinelsGlobal(filename, fileline);
506 pool = (mempool_t *)Clump_AllocBlock(sizeof(mempool_t));
511 Mem_PrintList(1<<30);
513 Sys_Abort("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
515 memset(pool, 0, sizeof(mempool_t));
516 pool->sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1);
517 pool->sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2);
518 pool->filename = filename;
519 pool->fileline = fileline;
521 List_Create(&pool->chain);
523 pool->realsize = sizeof(mempool_t);
524 dp_strlcpy (pool->name, name, sizeof (pool->name));
525 pool->parent = parent;
526 pool->next = poolchain;
531 void _Mem_FreePool(mempool_t **poolpointer, const char *filename, int fileline)
533 mempool_t *pool = *poolpointer;
534 mempool_t **chainaddress, *iter, *temp;
536 if (developer_memorydebug.integer)
537 _Mem_CheckSentinelsGlobal(filename, fileline);
540 // unlink pool from chain
541 for (chainaddress = &poolchain;*chainaddress && *chainaddress != pool;chainaddress = &((*chainaddress)->next));
542 if (*chainaddress != pool)
543 Sys_Abort("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
544 if (pool->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1))
545 Sys_Abort("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
546 if (pool->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2))
547 Sys_Abort("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
548 *chainaddress = pool->next;
550 // free memory owned by the pool
551 while (!List_Is_Empty(&pool->chain))
552 _Mem_FreeBlock(List_First_Entry(&pool->chain, memheader_t, list), filename, fileline);
554 // free child pools, too
555 for(iter = poolchain; iter; iter = temp) {
557 if(iter->parent == pool)
558 _Mem_FreePool(&temp, filename, fileline);
561 // free the pool itself
562 Clump_FreeBlock(pool, sizeof(*pool));
568 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
570 mempool_t *chainaddress;
572 if (developer_memorydebug.integer)
574 //_Mem_CheckSentinelsGlobal(filename, fileline);
575 // check if this pool is in the poolchain
576 for (chainaddress = poolchain;chainaddress;chainaddress = chainaddress->next)
577 if (chainaddress == pool)
580 Sys_Abort("Mem_EmptyPool: pool is already free (emptypool at %s:%i)", filename, fileline);
583 Sys_Abort("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
584 if (pool->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1))
585 Sys_Abort("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
586 if (pool->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2))
587 Sys_Abort("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
589 // free memory owned by the pool
590 while (!List_Is_Empty(&pool->chain))
591 _Mem_FreeBlock(List_First_Entry(&pool->chain, memheader_t, list), filename, fileline);
593 // empty child pools, too
594 for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
595 if(chainaddress->parent == pool)
596 _Mem_EmptyPool(chainaddress, filename, fileline);
600 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
603 unsigned int sentinel1;
604 unsigned int sentinel2;
607 Sys_Abort("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
609 mem = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
610 sentinel1 = MEMHEADER_SENTINEL_FOR_ADDRESS(&mem->sentinel);
611 sentinel2 = MEMHEADER_SENTINEL_FOR_ADDRESS((unsigned char *) mem + sizeof(memheader_t) + mem->size);
612 if (mem->sentinel != sentinel1)
613 Sys_Abort("Mem_Free: trashed head sentinel (alloc at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
614 if (memcmp((unsigned char *) mem + sizeof(memheader_t) + mem->size, &sentinel2, sizeof(sentinel2)))
615 Sys_Abort("Mem_Free: trashed tail sentinel (alloc at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
619 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
621 // this isn't really very useful
622 if (clump->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel1))
623 Sys_Abort("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
624 if (clump->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&clump->sentinel2))
625 Sys_Abort("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
629 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
636 for (pool = poolchain;pool;pool = pool->next)
638 if (pool->sentinel1 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel1))
639 Sys_Abort("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
640 if (pool->sentinel2 != MEMHEADER_SENTINEL_FOR_ADDRESS(&pool->sentinel2))
641 Sys_Abort("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
643 for (pool = poolchain;pool;pool = pool->next)
644 List_For_Each_Entry(mem, &pool->chain, memheader_t, list)
645 _Mem_CheckSentinels((void *)((unsigned char *) mem + sizeof(memheader_t)), filename, fileline);
647 for (pool = poolchain;pool;pool = pool->next)
648 for (clump = clumpchain;clump;clump = clump->chain)
649 _Mem_CheckClumpSentinels(clump, filename, fileline);
653 qbool Mem_IsAllocated(mempool_t *pool, const void *data)
660 // search only one pool
661 target = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
662 List_For_Each_Entry(header, &pool->chain, memheader_t, list)
663 if( header == target )
669 for (pool = poolchain;pool;pool = pool->next)
670 if (Mem_IsAllocated(pool, data))
676 void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray)
678 memset(l, 0, sizeof(*l));
679 l->mempool = mempool;
680 l->recordsize = recordsize;
681 l->numrecordsperarray = numrecordsperarray;
684 void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l)
689 for (i = 0;i != l->numarrays;i++)
690 Mem_Free(l->arrays[i].data);
693 memset(l, 0, sizeof(*l));
696 void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l)
701 if (i == l->numarrays)
703 if (l->numarrays == l->maxarrays)
705 memexpandablearray_array_t *oldarrays = l->arrays;
706 l->maxarrays = max(l->maxarrays * 2, 128);
707 l->arrays = (memexpandablearray_array_t*) Mem_Alloc(l->mempool, l->maxarrays * sizeof(*l->arrays));
710 memcpy(l->arrays, oldarrays, l->numarrays * sizeof(*l->arrays));
714 l->arrays[i].numflaggedrecords = 0;
715 l->arrays[i].data = (unsigned char *) Mem_Alloc(l->mempool, (l->recordsize + 1) * l->numrecordsperarray);
716 l->arrays[i].allocflags = l->arrays[i].data + l->recordsize * l->numrecordsperarray;
719 if (l->arrays[i].numflaggedrecords < l->numrecordsperarray)
721 for (j = 0;j < l->numrecordsperarray;j++)
723 if (!l->arrays[i].allocflags[j])
725 l->arrays[i].allocflags[j] = true;
726 l->arrays[i].numflaggedrecords++;
727 memset(l->arrays[i].data + l->recordsize * j, 0, l->recordsize);
728 return (void *)(l->arrays[i].data + l->recordsize * j);
735 /*****************************************************************************
737 * If this function was to change the size of the "expandable" array, you have
738 * to update r_shadow.c
739 * Just do a search for "range =", R_ShadowClearWorldLights would be the first
740 * function to look at. (And also seems like the only one?) You might have to
741 * move the call to Mem_ExpandableArray_IndexRange back into for(...) loop's
744 void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record) // const!
747 unsigned char *p = (unsigned char *)record;
748 for (i = 0;i != l->numarrays;i++)
750 if (p >= l->arrays[i].data && p < (l->arrays[i].data + l->recordsize * l->numrecordsperarray))
752 j = (p - l->arrays[i].data) / l->recordsize;
753 if (p != l->arrays[i].data + j * l->recordsize)
754 Sys_Abort("Mem_ExpandableArray_FreeRecord: no such record %p\n", (void *)p);
755 if (!l->arrays[i].allocflags[j])
756 Sys_Abort("Mem_ExpandableArray_FreeRecord: record %p is already free!\n", (void *)p);
757 l->arrays[i].allocflags[j] = false;
758 l->arrays[i].numflaggedrecords--;
764 size_t Mem_ExpandableArray_IndexRange(const memexpandablearray_t *l)
766 size_t i, j, k, end = 0;
767 for (i = 0;i < l->numarrays;i++)
769 for (j = 0, k = 0;k < l->arrays[i].numflaggedrecords;j++)
771 if (l->arrays[i].allocflags[j])
773 end = l->numrecordsperarray * i + j + 1;
781 void *Mem_ExpandableArray_RecordAtIndex(const memexpandablearray_t *l, size_t index)
784 i = index / l->numrecordsperarray;
785 j = index % l->numrecordsperarray;
786 if (i >= l->numarrays || !l->arrays[i].allocflags[j])
788 return (void *)(l->arrays[i].data + j * l->recordsize);
792 // used for temporary memory allocations around the engine, not for longterm
793 // storage, if anything in this pool stays allocated during gameplay, it is
795 mempool_t *tempmempool;
797 mempool_t *zonemempool;
799 void Mem_PrintStats(void)
801 size_t count = 0, size = 0, realsize = 0;
804 Mem_CheckSentinelsGlobal();
805 for (pool = poolchain;pool;pool = pool->next)
808 size += pool->totalsize;
809 realsize += pool->realsize;
811 Con_Printf("%lu memory pools, totalling %lu bytes (%.3fMB)\n", (unsigned long)count, (unsigned long)size, size / 1048576.0);
812 Con_Printf("total allocated size: %lu bytes (%.3fMB)\n", (unsigned long)realsize, realsize / 1048576.0);
813 for (pool = poolchain;pool;pool = pool->next)
815 if ((pool->flags & POOLFLAG_TEMP) && !List_Is_Empty(&pool->chain))
817 Con_Printf(CON_WARN "Memory pool %p has sprung a leak totalling %lu bytes (%.3fMB)! Listing contents...\n", (void *)pool, (unsigned long)pool->totalsize, pool->totalsize / 1048576.0);
818 List_For_Each_Entry(mem, &pool->chain, memheader_t, list)
819 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
824 void Mem_PrintList(size_t minallocationsize)
828 Mem_CheckSentinelsGlobal();
829 Con_Print("memory pool list:\n"
831 for (pool = poolchain;pool;pool = pool->next)
833 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" : "");
834 pool->lastchecksize = pool->totalsize;
835 List_For_Each_Entry(mem, &pool->chain, memheader_t, list)
836 if (mem->size >= minallocationsize)
837 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
841 static void MemList_f(cmd_state_t *cmd)
843 switch(Cmd_Argc(cmd))
846 Mem_PrintList(1<<30);
850 Mem_PrintList(atoi(Cmd_Argv(cmd, 1)) * 1024);
854 Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
859 static void MemStats_f(cmd_state_t *cmd)
861 Mem_CheckSentinelsGlobal();
866 char *_Mem_strdup(mempool_t *pool, const char *s, const char *filename, int fileline)
873 p = (char*)_Mem_Alloc (pool, NULL, sz, alignof(char), filename, fileline);
874 dp_strlcpy (p, s, sz);
879 ========================
881 ========================
883 void Memory_Init (void)
885 static union {unsigned short s;unsigned char b[2];} u;
887 mem_bigendian = u.b[0] != 0;
889 sentinel_seed = rand();
891 tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL);
892 zonemempool = Mem_AllocPool("Zone", 0, NULL);
894 if (Thread_HasThreads())
895 mem_mutex = Thread_CreateMutex();
898 void Memory_Shutdown (void)
900 // Mem_FreePool (&zonemempool);
901 // Mem_FreePool (&tempmempool);
904 Thread_DestroyMutex(mem_mutex);
908 void Memory_Init_Commands (void)
910 Cmd_AddCommand(CF_SHARED, "memstats", MemStats_f, "prints memory system statistics");
911 Cmd_AddCommand(CF_SHARED, "memlist", MemList_f, "prints memory pool information (or if used as memlist 5 lists individual allocations of 5K or larger, 0 lists all allocations)");
913 Cvar_RegisterVariable (&developer_memory);
914 Cvar_RegisterVariable (&developer_memorydebug);
915 Cvar_RegisterVariable (&developer_memoryreportlargerthanmb);
916 Cvar_RegisterVariable (&sys_memsize_physical);
917 Cvar_RegisterVariable (&sys_memsize_virtual);
922 MEMORYSTATUSEX status;
924 Cvar_SetValueQuick(&sys_memsize_virtual, 8388608);
926 status.dwLength = sizeof(status);
927 if(GlobalMemoryStatusEx(&status))
929 Cvar_SetValueQuick(&sys_memsize_physical, status.ullTotalPhys / 1048576.0);
930 Cvar_SetValueQuick(&sys_memsize_virtual, min(sys_memsize_virtual.value, status.ullTotalVirtual / 1048576.0));
937 Cvar_SetValueQuick(&sys_memsize_virtual, 2048);
939 status.dwLength = sizeof(status);
940 GlobalMemoryStatus(&status);
941 Cvar_SetValueQuick(&sys_memsize_physical, status.dwTotalPhys / 1048576.0);
942 Cvar_SetValueQuick(&sys_memsize_virtual, min(sys_memsize_virtual.value, status.dwTotalVirtual / 1048576.0));
948 Cvar_SetValueQuick(&sys_memsize_virtual, (sizeof(void*) == 4) ? 2048 : 268435456);
951 // Linux, and BSD with linprocfs mounted
952 FILE *f = fopen("/proc/meminfo", "r");
955 static char buf[1024];
956 while(fgets(buf, sizeof(buf), f))
959 if(!COM_ParseToken_Console(&p))
961 if(!strcmp(com_token, "MemTotal:"))
963 if(!COM_ParseToken_Console(&p))
965 Cvar_SetValueQuick(&sys_memsize_physical, atof(com_token) / 1024.0);
967 if(!strcmp(com_token, "SwapTotal:"))
969 if(!COM_ParseToken_Console(&p))
971 Cvar_SetValueQuick(&sys_memsize_virtual, min(sys_memsize_virtual.value , atof(com_token) / 1024.0 + sys_memsize_physical.value));