]> icculus.org git repositories - divverent/darkplaces.git/blob - zone.c
Added the cmd prvm_global <program> <global> which displays the value of the
[divverent/darkplaces.git] / zone.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // Z_zone.c
21
22 #include "quakedef.h"
23
24 cvar_t developer_memory = {0, "developer_memory", "0"};
25 cvar_t developer_memorydebug = {0, "developer_memorydebug", "0"};
26
27 mempool_t *poolchain = NULL;
28
29 void *_Mem_Alloc(mempool_t *pool, int size, const char *filename, int fileline)
30 {
31 #if MEMCLUMPING
32         int i, j, k, needed, endbit, largest;
33         memclump_t *clump, **clumpchainpointer;
34 #endif
35         memheader_t *mem;
36         if (size <= 0)
37                 return NULL;
38         if (pool == NULL)
39                 Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
40         if (developer.integer && developer_memory.integer)
41                 Con_Printf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, size);
42         if (developer.integer && developer_memorydebug.integer)
43                 _Mem_CheckSentinelsGlobal(filename, fileline);
44         pool->totalsize += size;
45 #if MEMCLUMPING
46         if (size < 4096)
47         {
48                 // clumping
49                 needed = (sizeof(memheader_t) + size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT;
50                 endbit = MEMBITS - needed;
51                 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
52                 {
53                         clump = *clumpchainpointer;
54                         if (clump->sentinel1 != MEMCLUMP_SENTINEL)
55                                 Sys_Error("Mem_Alloc: trashed clump sentinel 1 (alloc at %s:%d)", filename, fileline);
56                         if (clump->sentinel2 != MEMCLUMP_SENTINEL)
57                                 Sys_Error("Mem_Alloc: trashed clump sentinel 2 (alloc at %s:%d)", filename, fileline);
58                         if (clump->largestavailable >= needed)
59                         {
60                                 largest = 0;
61                                 for (i = 0;i < endbit;i++)
62                                 {
63                                         if (clump->bits[i >> 5] & (1 << (i & 31)))
64                                                 continue;
65                                         k = i + needed;
66                                         for (j = i;i < k;i++)
67                                                 if (clump->bits[i >> 5] & (1 << (i & 31)))
68                                                         goto loopcontinue;
69                                         goto choseclump;
70         loopcontinue:;
71                                         if (largest < j - i)
72                                                 largest = j - i;
73                                 }
74                                 // since clump falsely advertised enough space (nothing wrong
75                                 // with that), update largest count to avoid wasting time in
76                                 // later allocations
77                                 clump->largestavailable = largest;
78                         }
79                 }
80                 pool->realsize += sizeof(memclump_t);
81                 clump = malloc(sizeof(memclump_t));
82                 if (clump == NULL)
83                         Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
84                 memset(clump, 0, sizeof(memclump_t));
85                 *clumpchainpointer = clump;
86                 clump->sentinel1 = MEMCLUMP_SENTINEL;
87                 clump->sentinel2 = MEMCLUMP_SENTINEL;
88                 clump->chain = NULL;
89                 clump->blocksinuse = 0;
90                 clump->largestavailable = MEMBITS - needed;
91                 j = 0;
92 choseclump:
93                 mem = (memheader_t *)((qbyte *) clump->block + j * MEMUNIT);
94                 mem->clump = clump;
95                 clump->blocksinuse += needed;
96                 for (i = j + needed;j < i;j++)
97                         clump->bits[j >> 5] |= (1 << (j & 31));
98         }
99         else
100         {
101                 // big allocations are not clumped
102 #endif
103                 pool->realsize += sizeof(memheader_t) + size + sizeof(int);
104                 mem = malloc(sizeof(memheader_t) + size + sizeof(int));
105                 if (mem == NULL)
106                         Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
107 #if MEMCLUMPING
108                 mem->clump = NULL;
109         }
110 #endif
111         mem->filename = filename;
112         mem->fileline = fileline;
113         mem->size = size;
114         mem->pool = pool;
115         mem->sentinel1 = MEMHEADER_SENTINEL1;
116         // 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
117         *((qbyte *) mem + sizeof(memheader_t) + mem->size) = MEMHEADER_SENTINEL2;
118         // append to head of list
119         mem->next = pool->chain;
120         mem->prev = NULL;
121         pool->chain = mem;
122         if (mem->next)
123                 mem->next->prev = mem;
124         memset((void *)((qbyte *) mem + sizeof(memheader_t)), 0, mem->size);
125         return (void *)((qbyte *) mem + sizeof(memheader_t));
126 }
127
128 void _Mem_Free(void *data, const char *filename, int fileline)
129 {
130 #if MEMCLUMPING
131         int i, firstblock, endblock;
132         memclump_t *clump, **clumpchainpointer;
133 #endif
134         memheader_t *mem;
135         mempool_t *pool;
136         if (data == NULL)
137                 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
138
139         mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
140         if (mem->sentinel1 != MEMHEADER_SENTINEL1)
141                 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
142         if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
143                 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
144         pool = mem->pool;
145         if (developer.integer && developer_memory.integer)
146                 Con_Printf("Mem_Free: pool %s, alloc %s:%i, free %s:%i, size %i bytes\n", pool->name, mem->filename, mem->fileline, filename, fileline, mem->size);
147         // unlink memheader from doubly linked list
148         if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
149                 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
150         if (mem->prev)
151                 mem->prev->next = mem->next;
152         else
153                 pool->chain = mem->next;
154         if (mem->next)
155                 mem->next->prev = mem->prev;
156         // memheader has been unlinked, do the actual free now
157         pool->totalsize -= mem->size;
158 #if MEMCLUMPING
159         if ((clump = mem->clump))
160         {
161                 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
162                         Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
163                 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
164                         Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
165                 firstblock = ((qbyte *) mem - (qbyte *) clump->block);
166                 if (firstblock & (MEMUNIT - 1))
167                         Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
168                 firstblock /= MEMUNIT;
169                 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
170                 clump->blocksinuse -= endblock - firstblock;
171                 // could use &, but we know the bit is set
172                 for (i = firstblock;i < endblock;i++)
173                         clump->bits[i >> 5] -= (1 << (i & 31));
174                 if (clump->blocksinuse <= 0)
175                 {
176                         // unlink from chain
177                         for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
178                         {
179                                 if (*clumpchainpointer == clump)
180                                 {
181                                         *clumpchainpointer = clump->chain;
182                                         break;
183                                 }
184                         }
185                         pool->realsize -= sizeof(memclump_t);
186                         memset(clump, 0xBF, sizeof(memclump_t));
187                         free(clump);
188                 }
189                 else
190                 {
191                         // clump still has some allocations
192                         // force re-check of largest available space on next alloc
193                         clump->largestavailable = MEMBITS - clump->blocksinuse;
194                 }
195         }
196         else
197         {
198 #endif
199                 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
200                 if (developer.integer)
201                         memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
202                 free(mem);
203 #if MEMCLUMPING
204         }
205 #endif
206 }
207
208 mempool_t *_Mem_AllocPool(const char *name, mempool_t *parent, const char *filename, int fileline)
209 {
210         mempool_t *pool;
211         pool = malloc(sizeof(mempool_t));
212         if (pool == NULL)
213                 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
214         memset(pool, 0, sizeof(mempool_t));
215         pool->sentinel1 = MEMHEADER_SENTINEL1;
216         pool->sentinel2 = MEMHEADER_SENTINEL1;
217         pool->filename = filename;
218         pool->fileline = fileline;
219         pool->chain = NULL;
220         pool->totalsize = 0;
221         pool->realsize = sizeof(mempool_t);
222         strlcpy (pool->name, name, sizeof (pool->name));
223         pool->parent = parent;
224         pool->next = poolchain;
225         poolchain = pool;
226         return pool;
227 }
228
229 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline)
230 {
231         mempool_t **chainaddress, *iter, *temp;
232         
233         if (*pool)
234         {
235                 if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1)
236                         Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
237                 if ((*pool)->sentinel2 != MEMHEADER_SENTINEL1)
238                         Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
239                 // unlink pool from chain
240                 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
241                 if (*chainaddress != *pool)
242                         Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
243                 *chainaddress = (*pool)->next;
244
245                 // free memory owned by the pool
246                 while ((*pool)->chain)
247                         Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
248
249                 // free child pools, too
250                 for(iter = poolchain; iter; temp = iter = iter->next)
251                         if(iter->parent == *pool)
252                                 _Mem_FreePool(&temp, filename, fileline);
253                 
254                 // free the pool itself
255                 memset(*pool, 0xBF, sizeof(mempool_t));
256                 free(*pool);
257                 *pool = NULL;
258         }
259 }
260
261 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
262 {
263         mempool_t *chainaddress;
264         
265         if (pool == NULL)
266                 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
267         if (pool->sentinel1 != MEMHEADER_SENTINEL1)
268                 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
269         if (pool->sentinel2 != MEMHEADER_SENTINEL1)
270                 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
271
272         // free memory owned by the pool
273         while (pool->chain)
274                 Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
275
276         // empty child pools, too
277         for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
278                 if(chainaddress->parent == pool)
279                         _Mem_EmptyPool(chainaddress, filename, fileline);
280
281 }
282
283 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
284 {
285         memheader_t *mem;
286
287         if (data == NULL)
288                 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
289
290         mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
291         if (mem->sentinel1 != MEMHEADER_SENTINEL1)
292                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
293         if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
294                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
295 }
296
297 #if MEMCLUMPING
298 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
299 {
300         // this isn't really very useful
301         if (clump->sentinel1 != MEMCLUMP_SENTINEL)
302                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
303         if (clump->sentinel2 != MEMCLUMP_SENTINEL)
304                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
305 }
306 #endif
307
308 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
309 {
310         memheader_t *mem;
311 #if MEMCLUMPING
312         memclump_t *clump;
313 #endif
314         mempool_t *pool;
315         for (pool = poolchain;pool;pool = pool->next)
316         {
317                 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
318                         Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
319                 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
320                         Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
321         }
322         for (pool = poolchain;pool;pool = pool->next)
323                 for (mem = pool->chain;mem;mem = mem->next)
324                         _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline);
325 #if MEMCLUMPING
326         for (pool = poolchain;pool;pool = pool->next)
327                 for (clump = pool->clumpchain;clump;clump = clump->chain)
328                         _Mem_CheckClumpSentinels(clump, filename, fileline);
329 #endif
330 }
331
332 // used for temporary memory allocations around the engine, not for longterm
333 // storage, if anything in this pool stays allocated during gameplay, it is
334 // considered a leak
335 mempool_t *tempmempool;
336 // only for zone
337 mempool_t *zonemempool;
338
339 void Mem_PrintStats(void)
340 {
341         int count = 0, size = 0;
342         mempool_t *pool;
343         memheader_t *mem;
344         Mem_CheckSentinelsGlobal();
345         for (pool = poolchain;pool;pool = pool->next)
346         {
347                 count++;
348                 size += pool->totalsize;
349         }
350         Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
351         if (tempmempool == NULL)
352                 Con_Print("Error: no tempmempool allocated\n");
353         else if (tempmempool->chain)
354         {
355                 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
356                 Con_Print("listing temporary memory allocations:\n");
357                 for (mem = tempmempool->chain;mem;mem = mem->next)
358                         Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
359         }
360 }
361
362 void Mem_PrintList(int listallocations)
363 {
364         mempool_t *pool;
365         memheader_t *mem;
366         Mem_CheckSentinelsGlobal();
367         Con_Print("memory pool list:\n"
368                    "size    name\n");
369         for (pool = poolchain;pool;pool = pool->next)
370         {
371                 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
372                         Con_Printf("%10ik (%10ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
373                 else
374                         Con_Printf("%10ik (%10ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
375                 pool->lastchecksize = pool->totalsize;
376                 if (listallocations)
377                         for (mem = pool->chain;mem;mem = mem->next)
378                                 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
379         }
380 }
381
382 void MemList_f(void)
383 {
384         switch(Cmd_Argc())
385         {
386         case 1:
387                 Mem_PrintList(false);
388                 Mem_PrintStats();
389                 break;
390         case 2:
391                 if (!strcmp(Cmd_Argv(1), "all"))
392                 {
393                         Mem_PrintList(true);
394                         Mem_PrintStats();
395                         break;
396                 }
397                 // drop through
398         default:
399                 Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
400                 break;
401         }
402 }
403
404 extern void R_TextureStats_PrintTotal(void);
405 void MemStats_f(void)
406 {
407         Mem_CheckSentinelsGlobal();
408         R_TextureStats_PrintTotal();
409         Mem_PrintStats();
410 }
411
412
413 /*
414 ========================
415 Memory_Init
416 ========================
417 */
418 void Memory_Init (void)
419 {
420         tempmempool = Mem_AllocPool("Temporary Memory");
421         zonemempool = Mem_AllocPool("Zone");
422         poolchain = NULL;
423 }
424
425 void Memory_Init_Commands (void)
426 {
427         Cmd_AddCommand ("memstats", MemStats_f);
428         Cmd_AddCommand ("memlist", MemList_f);
429         Cvar_RegisterVariable (&developer_memory);
430         Cvar_RegisterVariable (&developer_memorydebug);
431 }
432