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"};
25 cvar_t developer_memorydebug = {0, "developer_memorydebug", "0"};
27 mempool_t *poolchain = NULL;
29 void *_Mem_Alloc(mempool_t *pool, int 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 *)((qbyte *) 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 = 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 *((qbyte *) 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 *)((qbyte *) mem + sizeof(memheader_t)), 0, mem->size);
125 return (void *)((qbyte *) 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);
139 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
140 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
141 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
142 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
143 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
145 if (developer.integer && developer_memory.integer)
146 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);
147 // unlink memheader from doubly linked list
148 if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
149 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
151 mem->prev->next = mem->next;
153 pool->chain = mem->next;
155 mem->next->prev = mem->prev;
156 // memheader has been unlinked, do the actual free now
157 pool->totalsize -= mem->size;
159 if ((clump = mem->clump))
161 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
162 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
163 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
164 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
165 firstblock = ((qbyte *) mem - (qbyte *) clump->block);
166 if (firstblock & (MEMUNIT - 1))
167 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
168 firstblock /= MEMUNIT;
169 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
170 clump->blocksinuse -= endblock - firstblock;
171 // could use &, but we know the bit is set
172 for (i = firstblock;i < endblock;i++)
173 clump->bits[i >> 5] -= (1 << (i & 31));
174 if (clump->blocksinuse <= 0)
177 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
179 if (*clumpchainpointer == clump)
181 *clumpchainpointer = clump->chain;
185 pool->realsize -= sizeof(memclump_t);
186 memset(clump, 0xBF, sizeof(memclump_t));
191 // clump still has some allocations
192 // force re-check of largest available space on next alloc
193 clump->largestavailable = MEMBITS - clump->blocksinuse;
199 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
200 if (developer.integer)
201 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
208 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline)
211 pool = malloc(sizeof(mempool_t));
213 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
214 memset(pool, 0, sizeof(mempool_t));
215 pool->sentinel1 = MEMHEADER_SENTINEL1;
216 pool->sentinel2 = MEMHEADER_SENTINEL1;
217 pool->filename = filename;
218 pool->fileline = fileline;
222 pool->realsize = sizeof(mempool_t);
223 strlcpy (pool->name, name, sizeof (pool->name));
224 pool->parent = parent;
225 pool->next = poolchain;
230 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline)
232 mempool_t **chainaddress, *iter, *temp;
236 if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1)
237 Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
238 if ((*pool)->sentinel2 != MEMHEADER_SENTINEL1)
239 Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
240 // unlink pool from chain
241 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
242 if (*chainaddress != *pool)
243 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
244 *chainaddress = (*pool)->next;
246 // free memory owned by the pool
247 while ((*pool)->chain)
248 Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
250 // free child pools, too
251 for(iter = poolchain; iter; temp = iter = iter->next)
252 if(iter->parent == *pool)
253 _Mem_FreePool(&temp, filename, fileline);
255 // free the pool itself
256 memset(*pool, 0xBF, sizeof(mempool_t));
262 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
264 mempool_t *chainaddress;
267 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
268 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
269 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
270 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
271 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
273 // free memory owned by the pool
275 Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
277 // empty child pools, too
278 for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
279 if(chainaddress->parent == pool)
280 _Mem_EmptyPool(chainaddress, filename, fileline);
284 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
289 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
291 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
292 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
293 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
294 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
295 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
299 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
301 // this isn't really very useful
302 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
303 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
304 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
305 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
309 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
316 for (pool = poolchain;pool;pool = pool->next)
318 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
319 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
320 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
321 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
323 for (pool = poolchain;pool;pool = pool->next)
324 for (mem = pool->chain;mem;mem = mem->next)
325 _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline);
327 for (pool = poolchain;pool;pool = pool->next)
328 for (clump = pool->clumpchain;clump;clump = clump->chain)
329 _Mem_CheckClumpSentinels(clump, filename, fileline);
333 qboolean Mem_IsAllocated(mempool_t *pool, void *data)
338 target = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
339 for( header = pool->chain ; header ; header = header->next )
340 if( header == target )
346 // used for temporary memory allocations around the engine, not for longterm
347 // storage, if anything in this pool stays allocated during gameplay, it is
349 mempool_t *tempmempool;
351 mempool_t *zonemempool;
353 void Mem_PrintStats(void)
355 int count = 0, size = 0;
358 Mem_CheckSentinelsGlobal();
359 for (pool = poolchain;pool;pool = pool->next)
362 size += pool->totalsize;
364 Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
365 for (pool = poolchain;pool;pool = pool->next)
367 if ((pool->flags & POOLFLAG_TEMP) && pool->chain)
369 Con_Printf("Memory pool %p has sprung a leak totalling %i bytes (%.3fMB)! Listing contents...\n", pool, pool->totalsize, pool->totalsize / 1048576.0);
370 for (mem = pool->chain;mem;mem = mem->next)
371 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
376 void Mem_PrintList(int listallocations)
380 Mem_CheckSentinelsGlobal();
381 Con_Print("memory pool list:\n"
383 for (pool = poolchain;pool;pool = pool->next)
385 Con_Printf("%10ik (%10ik actual) %s (%+i byte change) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize, (pool->flags & POOLFLAG_TEMP) ? "TEMP" : "");
386 pool->lastchecksize = pool->totalsize;
388 for (mem = pool->chain;mem;mem = mem->next)
389 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
398 Mem_PrintList(false);
402 if (!strcmp(Cmd_Argv(1), "all"))
410 Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
415 extern void R_TextureStats_PrintTotal(void);
416 void MemStats_f(void)
418 Mem_CheckSentinelsGlobal();
419 R_TextureStats_PrintTotal();
425 ========================
427 ========================
429 void Memory_Init (void)
431 tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL);
432 zonemempool = Mem_AllocPool("Zone", 0, NULL);
436 void Memory_Init_Commands (void)
438 Cmd_AddCommand ("memstats", MemStats_f);
439 Cmd_AddCommand ("memlist", MemList_f);
440 Cvar_RegisterVariable (&developer_memory);
441 Cvar_RegisterVariable (&developer_memorydebug);