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"};
26 mempool_t *poolchain = NULL;
28 void *_Mem_Alloc(mempool_t *pool, int size, const char *filename, int fileline)
31 int i, j, k, needed, endbit, largest;
32 memclump_t *clump, **clumpchainpointer;
38 Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
39 if (developer.integer && developer_memory.integer)
40 Con_Printf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, size);
41 pool->totalsize += size;
46 needed = (sizeof(memheader_t) + size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT;
47 endbit = MEMBITS - needed;
48 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
50 clump = *clumpchainpointer;
51 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
52 Sys_Error("Mem_Alloc: trashed clump sentinel 1 (alloc at %s:%d)", filename, fileline);
53 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
54 Sys_Error("Mem_Alloc: trashed clump sentinel 2 (alloc at %s:%d)", filename, fileline);
55 if (clump->largestavailable >= needed)
58 for (i = 0;i < endbit;i++)
60 if (clump->bits[i >> 5] & (1 << (i & 31)))
64 if (clump->bits[i >> 5] & (1 << (i & 31)))
71 // since clump falsely advertised enough space (nothing wrong
72 // with that), update largest count to avoid wasting time in
74 clump->largestavailable = largest;
77 pool->realsize += sizeof(memclump_t);
78 clump = malloc(sizeof(memclump_t));
80 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
81 memset(clump, 0, sizeof(memclump_t));
82 *clumpchainpointer = clump;
83 clump->sentinel1 = MEMCLUMP_SENTINEL;
84 clump->sentinel2 = MEMCLUMP_SENTINEL;
86 clump->blocksinuse = 0;
87 clump->largestavailable = MEMBITS - needed;
90 mem = (memheader_t *)((qbyte *) clump->block + j * MEMUNIT);
92 clump->blocksinuse += needed;
93 for (i = j + needed;j < i;j++)
94 clump->bits[j >> 5] |= (1 << (j & 31));
98 // big allocations are not clumped
100 pool->realsize += sizeof(memheader_t) + size + sizeof(int);
101 mem = malloc(sizeof(memheader_t) + size + sizeof(int));
103 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
108 mem->filename = filename;
109 mem->fileline = fileline;
112 mem->sentinel1 = MEMHEADER_SENTINEL1;
113 // 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
114 *((qbyte *) mem + sizeof(memheader_t) + mem->size) = MEMHEADER_SENTINEL2;
115 // append to head of list
116 mem->next = pool->chain;
120 mem->next->prev = mem;
121 memset((void *)((qbyte *) mem + sizeof(memheader_t)), 0, mem->size);
122 return (void *)((qbyte *) mem + sizeof(memheader_t));
125 void _Mem_Free(void *data, const char *filename, int fileline)
128 int i, firstblock, endblock;
129 memclump_t *clump, **clumpchainpointer;
134 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
136 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
137 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
138 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
139 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
140 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
142 if (developer.integer && developer_memory.integer)
143 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);
144 // unlink memheader from doubly linked list
145 if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
146 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
148 mem->prev->next = mem->next;
150 pool->chain = mem->next;
152 mem->next->prev = mem->prev;
153 // memheader has been unlinked, do the actual free now
154 pool->totalsize -= mem->size;
156 if ((clump = mem->clump))
158 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
159 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
160 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
161 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
162 firstblock = ((qbyte *) mem - (qbyte *) clump->block);
163 if (firstblock & (MEMUNIT - 1))
164 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
165 firstblock /= MEMUNIT;
166 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
167 clump->blocksinuse -= endblock - firstblock;
168 // could use &, but we know the bit is set
169 for (i = firstblock;i < endblock;i++)
170 clump->bits[i >> 5] -= (1 << (i & 31));
171 if (clump->blocksinuse <= 0)
174 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
176 if (*clumpchainpointer == clump)
178 *clumpchainpointer = clump->chain;
182 pool->realsize -= sizeof(memclump_t);
183 memset(clump, 0xBF, sizeof(memclump_t));
188 // clump still has some allocations
189 // force re-check of largest available space on next alloc
190 clump->largestavailable = MEMBITS - clump->blocksinuse;
196 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
197 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
204 mempool_t *_Mem_AllocPool(const char *name, const char *filename, int fileline)
207 pool = malloc(sizeof(mempool_t));
209 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
210 memset(pool, 0, sizeof(mempool_t));
211 pool->sentinel1 = MEMHEADER_SENTINEL1;
212 pool->sentinel2 = MEMHEADER_SENTINEL1;
213 pool->filename = filename;
214 pool->fileline = fileline;
217 pool->realsize = sizeof(mempool_t);
218 strcpy(pool->name, name);
219 pool->next = poolchain;
224 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline)
226 mempool_t **chainaddress;
229 if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1)
230 Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
231 if ((*pool)->sentinel2 != MEMHEADER_SENTINEL1)
232 Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
233 // unlink pool from chain
234 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
235 if (*chainaddress != *pool)
236 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
237 *chainaddress = (*pool)->next;
239 // free memory owned by the pool
240 while ((*pool)->chain)
241 Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
243 // free the pool itself
244 memset(*pool, 0xBF, sizeof(mempool_t));
250 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
253 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
254 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
255 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
256 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
257 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
259 // free memory owned by the pool
261 Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
264 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
269 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
271 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
272 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
273 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
274 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
275 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
279 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
281 // this isn't really very useful
282 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
283 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
284 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
285 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
289 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
296 for (pool = poolchain;pool;pool = pool->next)
298 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
299 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
300 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
301 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
303 for (pool = poolchain;pool;pool = pool->next)
304 for (mem = pool->chain;mem;mem = mem->next)
305 _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline);
307 for (pool = poolchain;pool;pool = pool->next)
308 for (clump = pool->clumpchain;clump;clump = clump->chain)
309 _Mem_CheckClumpSentinels(clump, filename, fileline);
313 // used for temporary memory allocations around the engine, not for longterm
314 // storage, if anything in this pool stays allocated during gameplay, it is
316 mempool_t *tempmempool;
318 mempool_t *zonemempool;
320 void Mem_PrintStats(void)
322 int count = 0, size = 0;
325 Mem_CheckSentinelsGlobal();
326 for (pool = poolchain;pool;pool = pool->next)
329 size += pool->totalsize;
331 Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
332 if (tempmempool == NULL)
333 Con_Printf("Error: no tempmempool allocated\n");
334 else if (tempmempool->chain)
336 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
337 Con_Printf("listing temporary memory allocations:\n");
338 for (mem = tempmempool->chain;mem;mem = mem->next)
339 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
343 void Mem_PrintList(int listallocations)
347 Mem_CheckSentinelsGlobal();
348 Con_Printf("memory pool list:\n"
350 for (pool = poolchain;pool;pool = pool->next)
352 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
353 Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
355 Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
356 pool->lastchecksize = pool->totalsize;
358 for (mem = pool->chain;mem;mem = mem->next)
359 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
368 Mem_PrintList(false);
372 if (!strcmp(Cmd_Argv(1), "all"))
380 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
385 extern void R_TextureStats_PrintTotal(void);
386 void MemStats_f(void)
388 Mem_CheckSentinelsGlobal();
389 R_TextureStats_PrintTotal();
395 ========================
397 ========================
399 void Memory_Init (void)
401 tempmempool = Mem_AllocPool("Temporary Memory");
402 zonemempool = Mem_AllocPool("Zone");
405 void Memory_Init_Commands (void)
407 Cmd_AddCommand ("memstats", MemStats_f);
408 Cmd_AddCommand ("memlist", MemList_f);
409 Cvar_RegisterVariable (&developer_memory);