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 mempool_t *poolchain = NULL;
26 void *_Mem_Alloc(mempool_t *pool, int size, char *filename, int fileline)
29 int i, j, k, needed, endbit, largest;
30 memclump_t *clump, **clumpchainpointer;
36 Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
37 Con_DPrintf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, size);
38 pool->totalsize += size;
43 needed = (sizeof(memheader_t) + size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT;
44 endbit = MEMBITS - needed;
45 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
47 clump = *clumpchainpointer;
48 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
49 Sys_Error("Mem_Alloc: trashed clump sentinel 1 (alloc at %s:%d)", filename, fileline);
50 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
51 Sys_Error("Mem_Alloc: trashed clump sentinel 2 (alloc at %s:%d)", filename, fileline);
52 if (clump->largestavailable >= needed)
55 for (i = 0;i < endbit;i++)
57 if (clump->bits[i >> 5] & (1 << (i & 31)))
61 if (clump->bits[i >> 5] & (1 << (i & 31)))
68 // since clump falsely advertised enough space (nothing wrong
69 // with that), update largest count to avoid wasting time in
71 clump->largestavailable = largest;
74 pool->realsize += sizeof(memclump_t);
75 clump = malloc(sizeof(memclump_t));
77 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
78 memset(clump, 0, sizeof(memclump_t));
79 *clumpchainpointer = clump;
80 clump->sentinel1 = MEMCLUMP_SENTINEL;
81 clump->sentinel2 = MEMCLUMP_SENTINEL;
83 clump->blocksinuse = 0;
84 clump->largestavailable = MEMBITS - needed;
87 mem = (memheader_t *)((qbyte *) clump->block + j * MEMUNIT);
89 clump->blocksinuse += needed;
90 for (i = j + needed;j < i;j++)
91 clump->bits[j >> 5] |= (1 << (j & 31));
95 // big allocations are not clumped
97 pool->realsize += sizeof(memheader_t) + size + sizeof(int);
98 mem = malloc(sizeof(memheader_t) + size + sizeof(int));
100 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
105 mem->filename = filename;
106 mem->fileline = fileline;
109 mem->sentinel1 = MEMHEADER_SENTINEL1;
110 // 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
111 *((qbyte *) mem + sizeof(memheader_t) + mem->size) = MEMHEADER_SENTINEL2;
112 // append to head of list
113 mem->chain = pool->chain;
115 memset((void *)((qbyte *) mem + sizeof(memheader_t)), 0, mem->size);
116 return (void *)((qbyte *) mem + sizeof(memheader_t));
119 void _Mem_Free(void *data, char *filename, int fileline)
122 int i, firstblock, endblock;
123 memclump_t *clump, **clumpchainpointer;
125 memheader_t *mem, **memchainpointer;
128 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
131 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
132 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
133 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
134 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
135 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
137 Con_DPrintf("Mem_Free: pool %s, alloc %s:%i, free %s:%i, size %i bytes\n", pool->name, mem->filename, mem->fileline, filename, fileline, mem->size);
138 for (memchainpointer = &pool->chain;*memchainpointer;memchainpointer = &(*memchainpointer)->chain)
140 if (*memchainpointer == mem)
142 *memchainpointer = mem->chain;
143 pool->totalsize -= mem->size;
145 if ((clump = mem->clump))
147 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
148 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
149 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
150 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
151 firstblock = ((qbyte *) mem - (qbyte *) clump->block);
152 if (firstblock & (MEMUNIT - 1))
153 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
154 firstblock /= MEMUNIT;
155 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
156 clump->blocksinuse -= endblock - firstblock;
157 // could use &, but we know the bit is set
158 for (i = firstblock;i < endblock;i++)
159 clump->bits[i >> 5] -= (1 << (i & 31));
160 if (clump->blocksinuse <= 0)
163 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
165 if (*clumpchainpointer == clump)
167 *clumpchainpointer = clump->chain;
171 pool->realsize -= sizeof(memclump_t);
172 memset(clump, 0xBF, sizeof(memclump_t));
177 // clump still has some allocations
178 // force re-check of largest available space on next alloc
179 clump->largestavailable = MEMBITS - clump->blocksinuse;
185 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
186 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
194 Sys_Error("Mem_Free: not allocated (free at %s:%i)", filename, fileline);
197 mempool_t *_Mem_AllocPool(char *name, char *filename, int fileline)
200 pool = malloc(sizeof(mempool_t));
202 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
203 memset(pool, 0, sizeof(mempool_t));
204 pool->sentinel1 = MEMHEADER_SENTINEL1;
205 pool->sentinel2 = MEMHEADER_SENTINEL1;
206 pool->filename = filename;
207 pool->fileline = fileline;
210 pool->realsize = sizeof(mempool_t);
211 strcpy(pool->name, name);
212 pool->next = poolchain;
217 void _Mem_FreePool(mempool_t **pool, char *filename, int fileline)
219 mempool_t **chainaddress;
222 if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1)
223 Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
224 if ((*pool)->sentinel2 != MEMHEADER_SENTINEL1)
225 Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
226 // unlink pool from chain
227 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
228 if (*chainaddress != *pool)
229 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
230 *chainaddress = (*pool)->next;
232 // free memory owned by the pool
233 while ((*pool)->chain)
234 Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
236 // free the pool itself
237 memset(*pool, 0xBF, sizeof(mempool_t));
243 void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline)
246 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
247 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
248 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
249 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
250 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
252 // free memory owned by the pool
254 Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
257 void _Mem_CheckSentinels(void *data, char *filename, int fileline)
262 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
264 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
265 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
266 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
267 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
268 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
272 static void _Mem_CheckClumpSentinels(memclump_t *clump, char *filename, int fileline)
274 // this isn't really very useful
275 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
276 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
277 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
278 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
282 void _Mem_CheckSentinelsGlobal(char *filename, int fileline)
289 for (pool = poolchain;pool;pool = pool->next)
291 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
292 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
293 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
294 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
296 for (pool = poolchain;pool;pool = pool->next)
297 for (mem = pool->chain;mem;mem = mem->chain)
298 _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline);
300 for (pool = poolchain;pool;pool = pool->next)
301 for (clump = pool->clumpchain;clump;clump = clump->chain)
302 _Mem_CheckClumpSentinels(clump, filename, fileline);
306 // used for temporary memory allocations around the engine, not for longterm
307 // storage, if anything in this pool stays allocated during gameplay, it is
309 mempool_t *tempmempool;
311 mempool_t *zonemempool;
313 void Mem_PrintStats(void)
315 int count = 0, size = 0;
318 Mem_CheckSentinelsGlobal();
319 for (pool = poolchain;pool;pool = pool->next)
322 size += pool->totalsize;
324 Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
325 if (tempmempool == NULL)
326 Con_Printf("Error: no tempmempool allocated\n");
327 else if (tempmempool->chain)
329 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
330 Con_Printf("listing temporary memory allocations:\n");
331 for (mem = tempmempool->chain;mem;mem = mem->chain)
332 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
336 void Mem_PrintList(int listallocations)
340 Mem_CheckSentinelsGlobal();
341 Con_Printf("memory pool list:\n"
343 for (pool = poolchain;pool;pool = pool->next)
345 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
346 Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
348 Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
349 pool->lastchecksize = pool->totalsize;
351 for (mem = pool->chain;mem;mem = mem->chain)
352 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
361 Mem_PrintList(false);
365 if (!strcmp(Cmd_Argv(1), "all"))
373 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
378 extern void R_TextureStats_PrintTotal(void);
379 void MemStats_f(void)
381 Mem_CheckSentinelsGlobal();
382 R_TextureStats_PrintTotal();
388 ========================
390 ========================
392 void Memory_Init (void)
394 tempmempool = Mem_AllocPool("Temporary Memory");
395 zonemempool = Mem_AllocPool("Zone");
398 void Memory_Init_Commands (void)
400 Cmd_AddCommand ("memstats", MemStats_f);
401 Cmd_AddCommand ("memlist", MemList_f);