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));
206 pool->realsize = sizeof(mempool_t);
207 strcpy(pool->name, name);
208 pool->next = poolchain;
213 void _Mem_FreePool(mempool_t **pool, char *filename, int fileline)
215 mempool_t **chainaddress;
218 // unlink pool from chain
219 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
220 if (*chainaddress != *pool)
221 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
222 *chainaddress = (*pool)->next;
224 // free memory owned by the pool
225 while ((*pool)->chain)
226 Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
228 // free the pool itself
229 memset(*pool, 0xBF, sizeof(mempool_t));
235 void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline)
238 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
240 // free memory owned by the pool
242 Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
245 void _Mem_CheckSentinels(void *data, char *filename, int fileline)
250 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
252 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
253 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
254 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
255 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
256 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
260 static void _Mem_CheckClumpSentinels(memclump_t *clump, char *filename, int fileline)
262 // this isn't really very useful
263 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
264 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
265 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
266 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
270 void _Mem_CheckSentinelsGlobal(char *filename, int fileline)
277 for (pool = poolchain;pool;pool = pool->next)
281 for (mem = pool->chain;mem;mem = mem->chain)
282 _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline);
284 for (clump = pool->clumpchain;clump;clump = clump->chain)
285 _Mem_CheckClumpSentinels(clump, filename, fileline);
290 // used for temporary memory allocations around the engine, not for longterm
291 // storage, if anything in this pool stays allocated during gameplay, it is
293 mempool_t *tempmempool;
295 mempool_t *zonemempool;
297 void Mem_PrintStats(void)
299 int count = 0, size = 0;
302 Mem_CheckSentinelsGlobal();
303 for (pool = poolchain;pool;pool = pool->next)
306 size += pool->totalsize;
308 Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
309 if (tempmempool == NULL)
310 Con_Printf("Error: no tempmempool allocated\n");
311 else if (tempmempool->chain)
313 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
314 Con_Printf("listing temporary memory allocations:\n");
315 for (mem = tempmempool->chain;mem;mem = mem->chain)
316 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
320 void Mem_PrintList(int listallocations)
324 Mem_CheckSentinelsGlobal();
325 Con_Printf("memory pool list:\n"
327 for (pool = poolchain;pool;pool = pool->next)
329 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
330 Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
332 Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
333 pool->lastchecksize = pool->totalsize;
335 for (mem = pool->chain;mem;mem = mem->chain)
336 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
345 Mem_PrintList(false);
349 if (!strcmp(Cmd_Argv(1), "all"))
357 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
362 extern void R_TextureStats_PrintTotal(void);
363 void MemStats_f(void)
365 Mem_CheckSentinelsGlobal();
366 R_TextureStats_PrintTotal();
372 ========================
374 ========================
376 void Memory_Init (void)
378 tempmempool = Mem_AllocPool("Temporary Memory");
379 zonemempool = Mem_AllocPool("Zone");
382 void Memory_Init_Commands (void)
384 Cmd_AddCommand ("memstats", MemStats_f);
385 Cmd_AddCommand ("memlist", MemList_f);