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 pool->totalsize += size;
39 needed = (sizeof(memheader_t) + size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT;
40 endbit = MEMBITS - needed;
41 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
43 clump = *clumpchainpointer;
44 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
45 Sys_Error("Mem_Alloc: trashed clump sentinel 1 (alloc at %s:%d)", filename, fileline);
46 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
47 Sys_Error("Mem_Alloc: trashed clump sentinel 2 (alloc at %s:%d)", filename, fileline);
48 if (clump->largestavailable >= needed)
51 for (i = 0;i < endbit;i++)
53 if (clump->bits[i >> 5] & (1 << (i & 31)))
57 if (clump->bits[i >> 5] & (1 << (i & 31)))
64 // since clump falsely advertised enough space (nothing wrong
65 // with that), update largest count to avoid wasting time in
67 clump->largestavailable = largest;
70 pool->realsize += sizeof(memclump_t);
71 clump = malloc(sizeof(memclump_t));
73 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
74 memset(clump, 0, sizeof(memclump_t));
75 *clumpchainpointer = clump;
76 clump->sentinel1 = MEMCLUMP_SENTINEL;
77 clump->sentinel2 = MEMCLUMP_SENTINEL;
79 clump->blocksinuse = 0;
80 clump->largestavailable = MEMBITS - needed;
83 mem = (memheader_t *)((long) clump->block + j * MEMUNIT);
85 clump->blocksinuse += needed;
86 for (i = j + needed;j < i;j++)
87 clump->bits[j >> 5] |= (1 << (j & 31));
91 // big allocations are not clumped
92 pool->realsize += sizeof(memheader_t) + size + sizeof(int);
93 mem = malloc(sizeof(memheader_t) + size + sizeof(int));
95 Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
98 mem->filename = filename;
99 mem->fileline = fileline;
102 mem->sentinel1 = MEMHEADER_SENTINEL;
103 *((int *)((long) mem + sizeof(memheader_t) + mem->size)) = MEMHEADER_SENTINEL;
104 // append to head of list
105 mem->chain = pool->chain;
107 memset((void *)((long) mem + sizeof(memheader_t)), 0, mem->size);
108 return (void *)((long) mem + sizeof(memheader_t));
111 void _Mem_Free(void *data, char *filename, int fileline)
113 int i, firstblock, endblock;
114 memclump_t *clump, **clumpchainpointer;
115 memheader_t *mem, **memchainpointer;
118 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
121 mem = (memheader_t *)((long) data - sizeof(memheader_t));
122 if (mem->sentinel1 != MEMHEADER_SENTINEL)
123 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
124 if (*((int *)((long) mem + sizeof(memheader_t) + mem->size)) != MEMHEADER_SENTINEL)
125 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
127 for (memchainpointer = &pool->chain;*memchainpointer;memchainpointer = &(*memchainpointer)->chain)
129 if (*memchainpointer == mem)
131 *memchainpointer = mem->chain;
132 pool->totalsize -= mem->size;
133 if ((clump = mem->clump))
135 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
136 Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
137 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
138 Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
139 firstblock = ((long) mem - (long) clump->block);
140 if (firstblock & (MEMUNIT - 1))
141 Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
142 firstblock /= MEMUNIT;
143 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
144 clump->blocksinuse -= endblock - firstblock;
145 // could use &, but we know the bit is set
146 for (i = firstblock;i < endblock;i++)
147 clump->bits[i >> 5] -= (1 << (i & 31));
148 if (clump->blocksinuse <= 0)
151 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
153 if (*clumpchainpointer == clump)
155 *clumpchainpointer = clump->chain;
159 pool->realsize -= sizeof(memclump_t);
160 memset(clump, 0xBF, sizeof(memclump_t));
165 // clump still has some allocations
166 // force re-check of largest available space on next alloc
167 clump->largestavailable = MEMBITS - clump->blocksinuse;
172 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
173 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
179 Sys_Error("Mem_Free: not allocated (free at %s:%i)", filename, fileline);
182 mempool_t *_Mem_AllocPool(char *name, char *filename, int fileline)
186 pool = malloc(sizeof(mempool_t));
188 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
189 memset(pool, 0, sizeof(mempool_t));
192 pool->realsize = sizeof(mempool_t);
193 strcpy(pool->name, name);
194 // for (i = 0;i < (POOLNAMESIZE - 1) && name[i];i++)
195 // pool->name[i] = name[i];
196 // for (i = 0;i < POOLNAMESIZE;i++)
197 // pool->name[i] = 0;
198 pool->next = poolchain;
203 void _Mem_FreePool(mempool_t **pool, char *filename, int fileline)
205 mempool_t **chainaddress;
208 // unlink pool from chain
209 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
210 if (*chainaddress != *pool)
211 Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
212 *chainaddress = (*pool)->next;
214 // free memory owned by the pool
215 while ((*pool)->chain)
216 Mem_Free((void *)((long) (*pool)->chain + sizeof(memheader_t)));
218 // free the pool itself
219 memset(*pool, 0xBF, sizeof(mempool_t));
225 void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline)
228 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
230 // free memory owned by the pool
232 Mem_Free((void *)((long) pool->chain + sizeof(memheader_t)));
235 void _Mem_CheckSentinels(void *data, char *filename, int fileline)
240 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
242 mem = (memheader_t *)((long) data - sizeof(memheader_t));
243 if (mem->sentinel1 != MEMHEADER_SENTINEL)
244 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
245 if (*((int *)((long) mem + sizeof(memheader_t) + mem->size)) != MEMHEADER_SENTINEL)
246 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
249 static void _Mem_CheckClumpSentinels(memclump_t *clump, char *filename, int fileline)
251 // this isn't really very useful
252 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
253 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
254 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
255 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
258 void _Mem_CheckSentinelsGlobal(char *filename, int fileline)
263 for (pool = poolchain;pool;pool = pool->next)
265 for (mem = pool->chain;mem;mem = mem->chain)
266 _Mem_CheckSentinels((void *)((long) mem + sizeof(memheader_t)), filename, fileline);
267 for (clump = pool->clumpchain;clump;clump = clump->chain)
268 _Mem_CheckClumpSentinels(clump, filename, fileline);
272 // used for temporary memory allocations around the engine, not for longterm
273 // storage, if anything in this pool stays allocated during gameplay, it is
275 mempool_t *tempmempool;
277 mempool_t *zonemempool;
279 void Mem_PrintStats(void)
281 int count = 0, size = 0;
284 Mem_CheckSentinelsGlobal();
285 for (pool = poolchain;pool;pool = pool->next)
288 size += pool->totalsize;
290 Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
291 if (tempmempool == NULL)
292 Con_Printf("Error: no tempmempool allocated\n");
293 else if (tempmempool->chain)
295 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
296 Con_Printf("listing temporary memory allocations:\n");
297 for (mem = tempmempool->chain;mem;mem = mem->chain)
298 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
302 void Mem_PrintList(int listallocations)
306 Mem_CheckSentinelsGlobal();
307 Con_Printf("memory pool list:\n"
309 for (pool = poolchain;pool;pool = pool->next)
311 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
312 Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
314 Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
315 pool->lastchecksize = pool->totalsize;
317 for (mem = pool->chain;mem;mem = mem->chain)
318 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
327 Mem_PrintList(false);
331 if (!strcmp(Cmd_Argv(1), "all"))
339 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
344 extern void R_TextureStats_PrintTotal(void);
345 void MemStats_f(void)
347 Mem_CheckSentinelsGlobal();
348 R_TextureStats_PrintTotal();
354 ========================
356 ========================
358 void Memory_Init (void)
360 tempmempool = Mem_AllocPool("Temporary Memory");
361 zonemempool = Mem_AllocPool("Zone");
364 void Memory_Init_Commands (void)
366 Cmd_AddCommand ("memstats", MemStats_f);
367 Cmd_AddCommand ("memlist", MemList_f);