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 *)((long) 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_SENTINEL;
104 *((int *)((long) mem + sizeof(memheader_t) + mem->size)) = MEMHEADER_SENTINEL;
105 // append to head of list
106 mem->chain = pool->chain;
108 memset((void *)((long) mem + sizeof(memheader_t)), 0, mem->size);
109 return (void *)((long) mem + sizeof(memheader_t));
112 void _Mem_Free(void *data, char *filename, int fileline)
114 int i, firstblock, endblock;
115 memclump_t *clump, **clumpchainpointer;
116 memheader_t *mem, **memchainpointer;
119 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
122 mem = (memheader_t *)((long) data - sizeof(memheader_t));
123 if (mem->sentinel1 != MEMHEADER_SENTINEL)
124 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
125 if (*((int *)((long) mem + sizeof(memheader_t) + mem->size)) != MEMHEADER_SENTINEL)
126 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
128 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);
129 for (memchainpointer = &pool->chain;*memchainpointer;memchainpointer = &(*memchainpointer)->chain)
131 if (*memchainpointer == mem)
133 *memchainpointer = mem->chain;
134 pool->totalsize -= mem->size;
135 if ((clump = mem->clump))
137 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
138 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
139 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
140 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
141 firstblock = ((long) mem - (long) clump->block);
142 if (firstblock & (MEMUNIT - 1))
143 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
144 firstblock /= MEMUNIT;
145 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
146 clump->blocksinuse -= endblock - firstblock;
147 // could use &, but we know the bit is set
148 for (i = firstblock;i < endblock;i++)
149 clump->bits[i >> 5] -= (1 << (i & 31));
150 if (clump->blocksinuse <= 0)
153 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
155 if (*clumpchainpointer == clump)
157 *clumpchainpointer = clump->chain;
161 pool->realsize -= sizeof(memclump_t);
162 memset(clump, 0xBF, sizeof(memclump_t));
167 // clump still has some allocations
168 // force re-check of largest available space on next alloc
169 clump->largestavailable = MEMBITS - clump->blocksinuse;
174 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
175 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
181 Sys_Error("Mem_Free: not allocated (free at %s:%i)", filename, fileline);
184 mempool_t *_Mem_AllocPool(char *name, char *filename, int fileline)
188 pool = malloc(sizeof(mempool_t));
190 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
191 memset(pool, 0, sizeof(mempool_t));
194 pool->realsize = sizeof(mempool_t);
195 strcpy(pool->name, name);
196 // for (i = 0;i < (POOLNAMESIZE - 1) && name[i];i++)
197 // pool->name[i] = name[i];
198 // for (i = 0;i < POOLNAMESIZE;i++)
199 // pool->name[i] = 0;
200 pool->next = poolchain;
205 void _Mem_FreePool(mempool_t **pool, char *filename, int fileline)
207 mempool_t **chainaddress;
210 // unlink pool from chain
211 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
212 if (*chainaddress != *pool)
213 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
214 *chainaddress = (*pool)->next;
216 // free memory owned by the pool
217 while ((*pool)->chain)
218 Mem_Free((void *)((long) (*pool)->chain + sizeof(memheader_t)));
220 // free the pool itself
221 memset(*pool, 0xBF, sizeof(mempool_t));
227 void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline)
230 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
232 // free memory owned by the pool
234 Mem_Free((void *)((long) pool->chain + sizeof(memheader_t)));
237 void _Mem_CheckSentinels(void *data, char *filename, int fileline)
242 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
244 mem = (memheader_t *)((long) data - sizeof(memheader_t));
245 if (mem->sentinel1 != MEMHEADER_SENTINEL)
246 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
247 if (*((int *)((long) mem + sizeof(memheader_t) + mem->size)) != MEMHEADER_SENTINEL)
248 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
251 static void _Mem_CheckClumpSentinels(memclump_t *clump, char *filename, int fileline)
253 // this isn't really very useful
254 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
255 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
256 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
257 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
260 void _Mem_CheckSentinelsGlobal(char *filename, int fileline)
265 for (pool = poolchain;pool;pool = pool->next)
267 for (mem = pool->chain;mem;mem = mem->chain)
268 _Mem_CheckSentinels((void *)((long) mem + sizeof(memheader_t)), filename, fileline);
269 for (clump = pool->clumpchain;clump;clump = clump->chain)
270 _Mem_CheckClumpSentinels(clump, filename, fileline);
274 // used for temporary memory allocations around the engine, not for longterm
275 // storage, if anything in this pool stays allocated during gameplay, it is
277 mempool_t *tempmempool;
279 mempool_t *zonemempool;
281 void Mem_PrintStats(void)
283 int count = 0, size = 0;
286 Mem_CheckSentinelsGlobal();
287 for (pool = poolchain;pool;pool = pool->next)
290 size += pool->totalsize;
292 Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
293 if (tempmempool == NULL)
294 Con_Printf("Error: no tempmempool allocated\n");
295 else if (tempmempool->chain)
297 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
298 Con_Printf("listing temporary memory allocations:\n");
299 for (mem = tempmempool->chain;mem;mem = mem->chain)
300 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
304 void Mem_PrintList(int listallocations)
308 Mem_CheckSentinelsGlobal();
309 Con_Printf("memory pool list:\n"
311 for (pool = poolchain;pool;pool = pool->next)
313 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
314 Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
316 Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
317 pool->lastchecksize = pool->totalsize;
319 for (mem = pool->chain;mem;mem = mem->chain)
320 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
329 Mem_PrintList(false);
333 if (!strcmp(Cmd_Argv(1), "all"))
341 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
346 extern void R_TextureStats_PrintTotal(void);
347 void MemStats_f(void)
349 Mem_CheckSentinelsGlobal();
350 R_TextureStats_PrintTotal();
356 ========================
358 ========================
360 void Memory_Init (void)
362 tempmempool = Mem_AllocPool("Temporary Memory");
363 zonemempool = Mem_AllocPool("Zone");
366 void Memory_Init_Commands (void)
368 Cmd_AddCommand ("memstats", MemStats_f);
369 Cmd_AddCommand ("memlist", MemList_f);