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.
24 cvar_t developer_memory = {0, "developer_memory", "0", "prints debugging information about memory allocations"};
25 cvar_t developer_memorydebug = {0, "developer_memorydebug", "0", "enables memory corruption checks (very slow)"};
27 mempool_t *poolchain = NULL;
29 void *_Mem_Alloc(mempool_t *pool, size_t size, const char *filename, int fileline)
32 int i, j, k, needed, endbit, largest;
33 memclump_t *clump, **clumpchainpointer;
39 Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
40 if (developer.integer && developer_memory.integer)
41 Con_Printf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, size);
42 if (developer.integer && developer_memorydebug.integer)
43 _Mem_CheckSentinelsGlobal(filename, fileline);
44 pool->totalsize += size;
49 needed = (sizeof(memheader_t) + size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT;
50 endbit = MEMBITS - needed;
51 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
53 clump = *clumpchainpointer;
54 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
55 Sys_Error("Mem_Alloc: trashed clump sentinel 1 (alloc at %s:%d)", filename, fileline);
56 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
57 Sys_Error("Mem_Alloc: trashed clump sentinel 2 (alloc at %s:%d)", filename, fileline);
58 if (clump->largestavailable >= needed)
61 for (i = 0;i < endbit;i++)
63 if (clump->bits[i >> 5] & (1 << (i & 31)))
67 if (clump->bits[i >> 5] & (1 << (i & 31)))
74 // since clump falsely advertised enough space (nothing wrong
75 // with that), update largest count to avoid wasting time in
77 clump->largestavailable = largest;
80 pool->realsize += sizeof(memclump_t);
81 clump = malloc(sizeof(memclump_t));
83 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
84 memset(clump, 0, sizeof(memclump_t));
85 *clumpchainpointer = clump;
86 clump->sentinel1 = MEMCLUMP_SENTINEL;
87 clump->sentinel2 = MEMCLUMP_SENTINEL;
89 clump->blocksinuse = 0;
90 clump->largestavailable = MEMBITS - needed;
93 mem = (memheader_t *)((unsigned char *) clump->block + j * MEMUNIT);
95 clump->blocksinuse += needed;
96 for (i = j + needed;j < i;j++)
97 clump->bits[j >> 5] |= (1 << (j & 31));
101 // big allocations are not clumped
103 pool->realsize += sizeof(memheader_t) + size + sizeof(int);
104 mem = (memheader_t *)malloc(sizeof(memheader_t) + size + sizeof(int));
106 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
111 mem->filename = filename;
112 mem->fileline = fileline;
115 mem->sentinel1 = MEMHEADER_SENTINEL1;
116 // 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
117 *((unsigned char *) mem + sizeof(memheader_t) + mem->size) = MEMHEADER_SENTINEL2;
118 // append to head of list
119 mem->next = pool->chain;
123 mem->next->prev = mem;
124 memset((void *)((unsigned char *) mem + sizeof(memheader_t)), 0, mem->size);
125 return (void *)((unsigned char *) mem + sizeof(memheader_t));
128 // only used by _Mem_Free and _Mem_FreePool
129 static void _Mem_FreeBlock(memheader_t *mem, const char *filename, int fileline)
132 int i, firstblock, endblock;
133 memclump_t *clump, **clumpchainpointer;
136 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
137 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
138 if (*((unsigned char *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
139 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
141 if (developer.integer && developer_memory.integer)
142 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);
143 // unlink memheader from doubly linked list
144 if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
145 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
147 mem->prev->next = mem->next;
149 pool->chain = mem->next;
151 mem->next->prev = mem->prev;
152 // memheader has been unlinked, do the actual free now
153 pool->totalsize -= mem->size;
155 if ((clump = mem->clump))
157 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
158 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
159 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
160 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
161 firstblock = ((unsigned char *) mem - (unsigned char *) clump->block);
162 if (firstblock & (MEMUNIT - 1))
163 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
164 firstblock /= MEMUNIT;
165 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
166 clump->blocksinuse -= endblock - firstblock;
167 // could use &, but we know the bit is set
168 for (i = firstblock;i < endblock;i++)
169 clump->bits[i >> 5] -= (1 << (i & 31));
170 if (clump->blocksinuse <= 0)
173 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
175 if (*clumpchainpointer == clump)
177 *clumpchainpointer = clump->chain;
181 pool->realsize -= sizeof(memclump_t);
182 memset(clump, 0xBF, sizeof(memclump_t));
187 // clump still has some allocations
188 // force re-check of largest available space on next alloc
189 clump->largestavailable = MEMBITS - clump->blocksinuse;
195 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
196 if (developer.integer)
197 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
204 void _Mem_Free(void *data, const char *filename, int fileline)
207 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
209 if (developer.integer && developer_memorydebug.integer)
211 _Mem_CheckSentinelsGlobal(filename, fileline);
212 if (!Mem_IsAllocated(NULL, data))
213 Sys_Error("Mem_Free: data is not allocated (called at %s:%i)", filename, fileline);
216 _Mem_FreeBlock((memheader_t *)((unsigned char *) data - sizeof(memheader_t)), filename, fileline);
219 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline)
222 if (developer.integer && developer_memorydebug.integer)
223 _Mem_CheckSentinelsGlobal(filename, fileline);
224 pool = (mempool_t *)malloc(sizeof(mempool_t));
226 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
227 memset(pool, 0, sizeof(mempool_t));
228 pool->sentinel1 = MEMHEADER_SENTINEL1;
229 pool->sentinel2 = MEMHEADER_SENTINEL1;
230 pool->filename = filename;
231 pool->fileline = fileline;
235 pool->realsize = sizeof(mempool_t);
236 strlcpy (pool->name, name, sizeof (pool->name));
237 pool->parent = parent;
238 pool->next = poolchain;
243 void _Mem_FreePool(mempool_t **poolpointer, const char *filename, int fileline)
245 mempool_t *pool = *poolpointer;
246 mempool_t **chainaddress, *iter, *temp;
248 if (developer.integer && developer_memorydebug.integer)
249 _Mem_CheckSentinelsGlobal(filename, fileline);
252 // unlink pool from chain
253 for (chainaddress = &poolchain;*chainaddress && *chainaddress != pool;chainaddress = &((*chainaddress)->next));
254 if (*chainaddress != pool)
255 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
256 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
257 Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
258 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
259 Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
260 *chainaddress = pool->next;
262 // free memory owned by the pool
264 _Mem_FreeBlock(pool->chain, filename, fileline);
266 // free child pools, too
267 for(iter = poolchain; iter; temp = iter = iter->next)
268 if(iter->parent == pool)
269 _Mem_FreePool(&temp, filename, fileline);
271 // free the pool itself
272 memset(pool, 0xBF, sizeof(mempool_t));
279 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
281 mempool_t *chainaddress;
283 if (developer.integer && developer_memorydebug.integer)
285 _Mem_CheckSentinelsGlobal(filename, fileline);
286 // check if this pool is in the poolchain
287 for (chainaddress = poolchain;chainaddress;chainaddress = chainaddress->next)
288 if (chainaddress == pool)
291 Sys_Error("Mem_EmptyPool: pool is already free (emptypool at %s:%i)", filename, fileline);
294 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
295 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
296 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
297 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
298 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
300 // free memory owned by the pool
302 _Mem_FreeBlock(pool->chain, filename, fileline);
304 // empty child pools, too
305 for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
306 if(chainaddress->parent == pool)
307 _Mem_EmptyPool(chainaddress, filename, fileline);
311 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
316 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
318 mem = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
319 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
320 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
321 if (*((unsigned char *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
322 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
326 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
328 // this isn't really very useful
329 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
330 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
331 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
332 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
336 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
343 for (pool = poolchain;pool;pool = pool->next)
345 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
346 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
347 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
348 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
350 for (pool = poolchain;pool;pool = pool->next)
351 for (mem = pool->chain;mem;mem = mem->next)
352 _Mem_CheckSentinels((void *)((unsigned char *) mem + sizeof(memheader_t)), filename, fileline);
354 for (pool = poolchain;pool;pool = pool->next)
355 for (clump = pool->clumpchain;clump;clump = clump->chain)
356 _Mem_CheckClumpSentinels(clump, filename, fileline);
360 qboolean Mem_IsAllocated(mempool_t *pool, void *data)
367 // search only one pool
368 target = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
369 for( header = pool->chain ; header ; header = header->next )
370 if( header == target )
376 for (pool = poolchain;pool;pool = pool->next)
377 if (Mem_IsAllocated(pool, data))
384 // used for temporary memory allocations around the engine, not for longterm
385 // storage, if anything in this pool stays allocated during gameplay, it is
387 mempool_t *tempmempool;
389 mempool_t *zonemempool;
391 void Mem_PrintStats(void)
393 size_t count = 0, size = 0, realsize = 0;
396 Mem_CheckSentinelsGlobal();
397 for (pool = poolchain;pool;pool = pool->next)
400 size += pool->totalsize;
401 realsize += pool->realsize;
403 Con_Printf("%lu memory pools, totalling %lu bytes (%.3fMB)\n", (unsigned long)count, (unsigned long)size, size / 1048576.0);
404 Con_Printf("total allocated size: %lu bytes (%.3fMB)\n", (unsigned long)realsize, realsize / 1048576.0);
405 for (pool = poolchain;pool;pool = pool->next)
407 if ((pool->flags & POOLFLAG_TEMP) && pool->chain)
409 Con_Printf("Memory pool %p has sprung a leak totalling %lu bytes (%.3fMB)! Listing contents...\n", pool, (unsigned long)pool->totalsize, pool->totalsize / 1048576.0);
410 for (mem = pool->chain;mem;mem = mem->next)
411 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
416 void Mem_PrintList(int listallocations)
420 Mem_CheckSentinelsGlobal();
421 Con_Print("memory pool list:\n"
423 for (pool = poolchain;pool;pool = pool->next)
425 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" : "");
426 pool->lastchecksize = pool->totalsize;
428 for (mem = pool->chain;mem;mem = mem->next)
429 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
438 Mem_PrintList(false);
442 if (!strcmp(Cmd_Argv(1), "all"))
450 Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
455 extern void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal);
456 void MemStats_f(void)
458 Mem_CheckSentinelsGlobal();
459 R_TextureStats_Print(false, false, true);
465 ========================
467 ========================
469 void Memory_Init (void)
472 tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL);
473 zonemempool = Mem_AllocPool("Zone", 0, NULL);
476 void Memory_Shutdown (void)
478 // Mem_FreePool (&zonemempool);
479 // Mem_FreePool (&tempmempool);
482 void Memory_Init_Commands (void)
484 Cmd_AddCommand ("memstats", MemStats_f, "prints memory system statistics");
485 Cmd_AddCommand ("memlist", MemList_f, "prints memory pool information (and individual allocations if used as memlist all)");
486 Cvar_RegisterVariable (&developer_memory);
487 Cvar_RegisterVariable (&developer_memorydebug);