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, const 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->next = pool->chain;
117 mem->next->prev = mem;
118 memset((void *)((qbyte *) mem + sizeof(memheader_t)), 0, mem->size);
119 return (void *)((qbyte *) mem + sizeof(memheader_t));
122 void _Mem_Free(void *data, const char *filename, int fileline)
125 int i, firstblock, endblock;
126 memclump_t *clump, **clumpchainpointer;
131 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
133 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
134 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
135 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
136 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
137 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
139 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);
140 // unlink memheader from doubly linked list
141 if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
142 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
144 mem->prev->next = mem->next;
146 pool->chain = mem->next;
148 mem->next->prev = mem->prev;
149 // memheader has been unlinked, do the actual free now
150 pool->totalsize -= mem->size;
152 if ((clump = mem->clump))
154 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
155 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
156 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
157 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
158 firstblock = ((qbyte *) mem - (qbyte *) clump->block);
159 if (firstblock & (MEMUNIT - 1))
160 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
161 firstblock /= MEMUNIT;
162 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
163 clump->blocksinuse -= endblock - firstblock;
164 // could use &, but we know the bit is set
165 for (i = firstblock;i < endblock;i++)
166 clump->bits[i >> 5] -= (1 << (i & 31));
167 if (clump->blocksinuse <= 0)
170 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
172 if (*clumpchainpointer == clump)
174 *clumpchainpointer = clump->chain;
178 pool->realsize -= sizeof(memclump_t);
179 memset(clump, 0xBF, sizeof(memclump_t));
184 // clump still has some allocations
185 // force re-check of largest available space on next alloc
186 clump->largestavailable = MEMBITS - clump->blocksinuse;
192 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
193 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
200 mempool_t *_Mem_AllocPool(const char *name, const char *filename, int fileline)
203 pool = malloc(sizeof(mempool_t));
205 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
206 memset(pool, 0, sizeof(mempool_t));
207 pool->sentinel1 = MEMHEADER_SENTINEL1;
208 pool->sentinel2 = MEMHEADER_SENTINEL1;
209 pool->filename = filename;
210 pool->fileline = fileline;
213 pool->realsize = sizeof(mempool_t);
214 strcpy(pool->name, name);
215 pool->next = poolchain;
220 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline)
222 mempool_t **chainaddress;
225 if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1)
226 Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
227 if ((*pool)->sentinel2 != MEMHEADER_SENTINEL1)
228 Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
229 // unlink pool from chain
230 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
231 if (*chainaddress != *pool)
232 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
233 *chainaddress = (*pool)->next;
235 // free memory owned by the pool
236 while ((*pool)->chain)
237 Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
239 // free the pool itself
240 memset(*pool, 0xBF, sizeof(mempool_t));
246 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
249 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
250 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
251 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
252 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
253 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
255 // free memory owned by the pool
257 Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
260 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
265 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
267 mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
268 if (mem->sentinel1 != MEMHEADER_SENTINEL1)
269 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
270 if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
271 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
275 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
277 // this isn't really very useful
278 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
279 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
280 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
281 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
285 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
292 for (pool = poolchain;pool;pool = pool->next)
294 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
295 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
296 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
297 Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
299 for (pool = poolchain;pool;pool = pool->next)
300 for (mem = pool->chain;mem;mem = mem->next)
301 _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline);
303 for (pool = poolchain;pool;pool = pool->next)
304 for (clump = pool->clumpchain;clump;clump = clump->chain)
305 _Mem_CheckClumpSentinels(clump, filename, fileline);
309 // used for temporary memory allocations around the engine, not for longterm
310 // storage, if anything in this pool stays allocated during gameplay, it is
312 mempool_t *tempmempool;
314 mempool_t *zonemempool;
316 void Mem_PrintStats(void)
318 int count = 0, size = 0;
321 Mem_CheckSentinelsGlobal();
322 for (pool = poolchain;pool;pool = pool->next)
325 size += pool->totalsize;
327 Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
328 if (tempmempool == NULL)
329 Con_Printf("Error: no tempmempool allocated\n");
330 else if (tempmempool->chain)
332 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
333 Con_Printf("listing temporary memory allocations:\n");
334 for (mem = tempmempool->chain;mem;mem = mem->next)
335 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
339 void Mem_PrintList(int listallocations)
343 Mem_CheckSentinelsGlobal();
344 Con_Printf("memory pool list:\n"
346 for (pool = poolchain;pool;pool = pool->next)
348 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
349 Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
351 Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
352 pool->lastchecksize = pool->totalsize;
354 for (mem = pool->chain;mem;mem = mem->next)
355 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
364 Mem_PrintList(false);
368 if (!strcmp(Cmd_Argv(1), "all"))
376 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
381 extern void R_TextureStats_PrintTotal(void);
382 void MemStats_f(void)
384 Mem_CheckSentinelsGlobal();
385 R_TextureStats_PrintTotal();
391 ========================
393 ========================
395 void Memory_Init (void)
397 tempmempool = Mem_AllocPool("Temporary Memory");
398 zonemempool = Mem_AllocPool("Zone");
401 void Memory_Init_Commands (void)
403 Cmd_AddCommand ("memstats", MemStats_f);
404 Cmd_AddCommand ("memlist", MemList_f);