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 void _Mem_Free(void *data, const char *filename, int fileline)
131 int i, firstblock, endblock;
132 memclump_t *clump, **clumpchainpointer;
137 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
138 if (developer.integer && developer_memorydebug.integer)
140 _Mem_CheckSentinelsGlobal(filename, fileline);
141 if (!Mem_IsAllocated(NULL, data))
142 Sys_Error("Mem_Free: data is not allocated (called at %s:%i)", filename, fileline);
145 mem = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
146 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
147 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
148 if (*((unsigned char *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
149 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
151 if (developer.integer && developer_memory.integer)
152 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);
153 // unlink memheader from doubly linked list
154 if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
155 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
157 mem->prev->next = mem->next;
159 pool->chain = mem->next;
161 mem->next->prev = mem->prev;
162 // memheader has been unlinked, do the actual free now
163 pool->totalsize -= mem->size;
165 if ((clump = mem->clump))
167 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
168 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
169 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
170 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
171 firstblock = ((unsigned char *) mem - (unsigned char *) clump->block);
172 if (firstblock & (MEMUNIT - 1))
173 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
174 firstblock /= MEMUNIT;
175 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
176 clump->blocksinuse -= endblock - firstblock;
177 // could use &, but we know the bit is set
178 for (i = firstblock;i < endblock;i++)
179 clump->bits[i >> 5] -= (1 << (i & 31));
180 if (clump->blocksinuse <= 0)
183 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
185 if (*clumpchainpointer == clump)
187 *clumpchainpointer = clump->chain;
191 pool->realsize -= sizeof(memclump_t);
192 memset(clump, 0xBF, sizeof(memclump_t));
197 // clump still has some allocations
198 // force re-check of largest available space on next alloc
199 clump->largestavailable = MEMBITS - clump->blocksinuse;
205 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
206 if (developer.integer)
207 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
214 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline)
217 if (developer.integer && developer_memorydebug.integer)
218 _Mem_CheckSentinelsGlobal(filename, fileline);
219 pool = (mempool_t *)malloc(sizeof(mempool_t));
221 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
222 memset(pool, 0, sizeof(mempool_t));
223 pool->sentinel1 = MEMHEADER_SENTINEL1;
224 pool->sentinel2 = MEMHEADER_SENTINEL1;
225 pool->filename = filename;
226 pool->fileline = fileline;
230 pool->realsize = sizeof(mempool_t);
231 strlcpy (pool->name, name, sizeof (pool->name));
232 pool->parent = parent;
233 pool->next = poolchain;
238 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline)
240 mempool_t **chainaddress, *iter, *temp;
242 if (developer.integer && developer_memorydebug.integer)
243 _Mem_CheckSentinelsGlobal(filename, fileline);
246 // unlink pool from chain
247 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
248 if (*chainaddress != *pool)
249 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
250 if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1)
251 Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
252 if ((*pool)->sentinel2 != MEMHEADER_SENTINEL1)
253 Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
254 *chainaddress = (*pool)->next;
256 // free memory owned by the pool
257 while ((*pool)->chain)
258 _Mem_Free((void *)((unsigned char *) (*pool)->chain + sizeof(memheader_t)), filename, fileline);
260 // free child pools, too
261 for(iter = poolchain; iter; temp = iter = iter->next)
262 if(iter->parent == *pool)
263 _Mem_FreePool(&temp, filename, fileline);
265 // free the pool itself
266 memset(*pool, 0xBF, sizeof(mempool_t));
272 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
274 mempool_t *chainaddress;
276 if (developer.integer && developer_memorydebug.integer)
278 _Mem_CheckSentinelsGlobal(filename, fileline);
279 // check if this pool is in the poolchain
280 for (chainaddress = poolchain;chainaddress;chainaddress = chainaddress->next)
281 if (chainaddress == pool)
284 Sys_Error("Mem_EmptyPool: pool is already free (emptypool at %s:%i)", filename, fileline);
287 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
288 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
289 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
290 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
291 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
293 // free memory owned by the pool
295 _Mem_Free((void *)((unsigned char *) pool->chain + sizeof(memheader_t)), filename, fileline);
297 // empty child pools, too
298 for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
299 if(chainaddress->parent == pool)
300 _Mem_EmptyPool(chainaddress, filename, fileline);
304 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
309 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
311 mem = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
312 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
313 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
314 if (*((unsigned char *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
315 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
319 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
321 // this isn't really very useful
322 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
323 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
324 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
325 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
329 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
336 for (pool = poolchain;pool;pool = pool->next)
338 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
339 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
340 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
341 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
343 for (pool = poolchain;pool;pool = pool->next)
344 for (mem = pool->chain;mem;mem = mem->next)
345 _Mem_CheckSentinels((void *)((unsigned char *) mem + sizeof(memheader_t)), filename, fileline);
347 for (pool = poolchain;pool;pool = pool->next)
348 for (clump = pool->clumpchain;clump;clump = clump->chain)
349 _Mem_CheckClumpSentinels(clump, filename, fileline);
353 qboolean Mem_IsAllocated(mempool_t *pool, void *data)
360 // search only one pool
361 target = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
362 for( header = pool->chain ; header ; header = header->next )
363 if( header == target )
369 for (pool = poolchain;pool;pool = pool->next)
370 if (Mem_IsAllocated(pool, data))
377 // used for temporary memory allocations around the engine, not for longterm
378 // storage, if anything in this pool stays allocated during gameplay, it is
380 mempool_t *tempmempool;
382 mempool_t *zonemempool;
384 void Mem_PrintStats(void)
386 size_t count = 0, size = 0, realsize = 0;
389 Mem_CheckSentinelsGlobal();
390 for (pool = poolchain;pool;pool = pool->next)
393 size += pool->totalsize;
394 realsize += pool->realsize;
396 Con_Printf("%lu memory pools, totalling %lu bytes (%.3fMB)\n", (unsigned long)count, (unsigned long)size, size / 1048576.0);
397 Con_Printf("total allocated size: %lu bytes (%.3fMB)\n", (unsigned long)realsize, realsize / 1048576.0);
398 for (pool = poolchain;pool;pool = pool->next)
400 if ((pool->flags & POOLFLAG_TEMP) && pool->chain)
402 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);
403 for (mem = pool->chain;mem;mem = mem->next)
404 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
409 void Mem_PrintList(int listallocations)
413 Mem_CheckSentinelsGlobal();
414 Con_Print("memory pool list:\n"
416 for (pool = poolchain;pool;pool = pool->next)
418 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" : "");
419 pool->lastchecksize = pool->totalsize;
421 for (mem = pool->chain;mem;mem = mem->next)
422 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
431 Mem_PrintList(false);
435 if (!strcmp(Cmd_Argv(1), "all"))
443 Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
448 extern void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal);
449 void MemStats_f(void)
451 Mem_CheckSentinelsGlobal();
452 R_TextureStats_Print(false, false, true);
458 ========================
460 ========================
462 void Memory_Init (void)
464 tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL);
465 zonemempool = Mem_AllocPool("Zone", 0, NULL);
469 void Memory_Shutdown (void)
471 // Mem_FreePool (&zonemempool);
472 // Mem_FreePool (&tempmempool);
475 void Memory_Init_Commands (void)
477 Cmd_AddCommand ("memstats", MemStats_f, "prints memory system statistics");
478 Cmd_AddCommand ("memlist", MemList_f, "prints memory pool information (and individual allocations if used as memlist all)");
479 Cvar_RegisterVariable (&developer_memory);
480 Cvar_RegisterVariable (&developer_memorydebug);