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)
28 int i, j, k, needed, endbit, largest;
29 memclump_t *clump, **clumpchainpointer;
34 Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
35 Con_DPrintf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, size);
36 pool->totalsize += size;
40 needed = (sizeof(memheader_t) + size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT;
41 endbit = MEMBITS - needed;
42 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
44 clump = *clumpchainpointer;
45 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
46 Sys_Error("Mem_Alloc: trashed clump sentinel 1 (alloc at %s:%d)", filename, fileline);
47 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
48 Sys_Error("Mem_Alloc: trashed clump sentinel 2 (alloc at %s:%d)", filename, fileline);
49 if (clump->largestavailable >= needed)
52 for (i = 0;i < endbit;i++)
54 if (clump->bits[i >> 5] & (1 << (i & 31)))
58 if (clump->bits[i >> 5] & (1 << (i & 31)))
65 // since clump falsely advertised enough space (nothing wrong
66 // with that), update largest count to avoid wasting time in
68 clump->largestavailable = largest;
71 pool->realsize += sizeof(memclump_t);
72 clump = malloc(sizeof(memclump_t));
74 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
75 memset(clump, 0, sizeof(memclump_t));
76 *clumpchainpointer = clump;
77 clump->sentinel1 = MEMCLUMP_SENTINEL;
78 clump->sentinel2 = MEMCLUMP_SENTINEL;
80 clump->blocksinuse = 0;
81 clump->largestavailable = MEMBITS - needed;
84 mem = (memheader_t *)((qbyte *) clump->block + j * MEMUNIT);
86 clump->blocksinuse += needed;
87 for (i = j + needed;j < i;j++)
88 clump->bits[j >> 5] |= (1 << (j & 31));
92 // big allocations are not clumped
93 pool->realsize += sizeof(memheader_t) + size + sizeof(int);
94 mem = malloc(sizeof(memheader_t) + size + sizeof(int));
96 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
99 mem->filename = filename;
100 mem->fileline = fileline;
103 mem->sentinel1 = MEMHEADER_SENTINEL1;
104 // 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
105 *((qbyte *) mem + sizeof(memheader_t) + mem->size) = MEMHEADER_SENTINEL2;
106 // append to head of list
107 mem->chain = pool->chain;
109 memset((void *)((qbyte *) mem + sizeof(memheader_t)), 0, mem->size);
110 return (void *)((qbyte *) mem + sizeof(memheader_t));
113 void _Mem_Free(void *data, char *filename, int fileline)
115 int i, firstblock, endblock;
116 memclump_t *clump, **clumpchainpointer;
117 memheader_t *mem, **memchainpointer;
120 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
123 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
124 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
125 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
126 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
127 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
129 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);
130 for (memchainpointer = &pool->chain;*memchainpointer;memchainpointer = &(*memchainpointer)->chain)
132 if (*memchainpointer == mem)
134 *memchainpointer = mem->chain;
135 pool->totalsize -= mem->size;
136 if ((clump = mem->clump))
138 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
139 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
140 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
141 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
142 firstblock = ((qbyte *) mem - (qbyte *) clump->block);
143 if (firstblock & (MEMUNIT - 1))
144 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
145 firstblock /= MEMUNIT;
146 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
147 clump->blocksinuse -= endblock - firstblock;
148 // could use &, but we know the bit is set
149 for (i = firstblock;i < endblock;i++)
150 clump->bits[i >> 5] -= (1 << (i & 31));
151 if (clump->blocksinuse <= 0)
154 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
156 if (*clumpchainpointer == clump)
158 *clumpchainpointer = clump->chain;
162 pool->realsize -= sizeof(memclump_t);
163 memset(clump, 0xBF, sizeof(memclump_t));
168 // clump still has some allocations
169 // force re-check of largest available space on next alloc
170 clump->largestavailable = MEMBITS - clump->blocksinuse;
175 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
176 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
182 Sys_Error("Mem_Free: not allocated (free at %s:%i)", filename, fileline);
185 mempool_t *_Mem_AllocPool(char *name, char *filename, int fileline)
189 pool = malloc(sizeof(mempool_t));
191 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
192 memset(pool, 0, sizeof(mempool_t));
195 pool->realsize = sizeof(mempool_t);
196 strcpy(pool->name, name);
197 // for (i = 0;i < (POOLNAMESIZE - 1) && name[i];i++)
198 // pool->name[i] = name[i];
199 // for (i = 0;i < POOLNAMESIZE;i++)
200 // pool->name[i] = 0;
201 pool->next = poolchain;
206 void _Mem_FreePool(mempool_t **pool, char *filename, int fileline)
208 mempool_t **chainaddress;
211 // unlink pool from chain
212 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
213 if (*chainaddress != *pool)
214 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
215 *chainaddress = (*pool)->next;
217 // free memory owned by the pool
218 while ((*pool)->chain)
219 Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
221 // free the pool itself
222 memset(*pool, 0xBF, sizeof(mempool_t));
228 void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline)
231 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
233 // free memory owned by the pool
235 Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
238 void _Mem_CheckSentinels(void *data, char *filename, int fileline)
243 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
245 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
246 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
247 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
248 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
249 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
252 static void _Mem_CheckClumpSentinels(memclump_t *clump, char *filename, int fileline)
254 // this isn't really very useful
255 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
256 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
257 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
258 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
261 void _Mem_CheckSentinelsGlobal(char *filename, int fileline)
266 for (pool = poolchain;pool;pool = pool->next)
268 for (mem = pool->chain;mem;mem = mem->chain)
269 _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline);
270 for (clump = pool->clumpchain;clump;clump = clump->chain)
271 _Mem_CheckClumpSentinels(clump, filename, fileline);
275 // used for temporary memory allocations around the engine, not for longterm
276 // storage, if anything in this pool stays allocated during gameplay, it is
278 mempool_t *tempmempool;
280 mempool_t *zonemempool;
282 void Mem_PrintStats(void)
284 int count = 0, size = 0;
287 Mem_CheckSentinelsGlobal();
288 for (pool = poolchain;pool;pool = pool->next)
291 size += pool->totalsize;
293 Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
294 if (tempmempool == NULL)
295 Con_Printf("Error: no tempmempool allocated\n");
296 else if (tempmempool->chain)
298 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
299 Con_Printf("listing temporary memory allocations:\n");
300 for (mem = tempmempool->chain;mem;mem = mem->chain)
301 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
305 void Mem_PrintList(int listallocations)
309 Mem_CheckSentinelsGlobal();
310 Con_Printf("memory pool list:\n"
312 for (pool = poolchain;pool;pool = pool->next)
314 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
315 Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
317 Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
318 pool->lastchecksize = pool->totalsize;
320 for (mem = pool->chain;mem;mem = mem->chain)
321 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
330 Mem_PrintList(false);
334 if (!strcmp(Cmd_Argv(1), "all"))
342 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
347 extern void R_TextureStats_PrintTotal(void);
348 void MemStats_f(void)
350 Mem_CheckSentinelsGlobal();
351 R_TextureStats_PrintTotal();
357 ========================
359 ========================
361 void Memory_Init (void)
363 tempmempool = Mem_AllocPool("Temporary Memory");
364 zonemempool = Mem_AllocPool("Zone");
367 void Memory_Init_Commands (void)
369 Cmd_AddCommand ("memstats", MemStats_f);
370 Cmd_AddCommand ("memlist", MemList_f);