]> icculus.org git repositories - divverent/darkplaces.git/blob - zone.c
The broken pushers are PARTLY fixed. They will now stop when in contact with an entit...
[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, size_t 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         if (developer.integer && developer_memorydebug.integer)
139         {
140                 _Mem_CheckSentinelsGlobal(filename, fileline);
141                 if (!Mem_IsAllocated(NULL, data))
142                         Sys_Error("Mem_Free: data is not allocated (called at %s:%i)", filename, fileline);
143         }
144
145         mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
146         if (mem->sentinel1 != MEMHEADER_SENTINEL1)
147                 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
148         if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
149                 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
150         pool = mem->pool;
151         if (developer.integer && developer_memory.integer)
152                 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);
153         // unlink memheader from doubly linked list
154         if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
155                 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
156         if (mem->prev)
157                 mem->prev->next = mem->next;
158         else
159                 pool->chain = mem->next;
160         if (mem->next)
161                 mem->next->prev = mem->prev;
162         // memheader has been unlinked, do the actual free now
163         pool->totalsize -= mem->size;
164 #if MEMCLUMPING
165         if ((clump = mem->clump))
166         {
167                 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
168                         Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
169                 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
170                         Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
171                 firstblock = ((qbyte *) mem - (qbyte *) clump->block);
172                 if (firstblock & (MEMUNIT - 1))
173                         Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
174                 firstblock /= MEMUNIT;
175                 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
176                 clump->blocksinuse -= endblock - firstblock;
177                 // could use &, but we know the bit is set
178                 for (i = firstblock;i < endblock;i++)
179                         clump->bits[i >> 5] -= (1 << (i & 31));
180                 if (clump->blocksinuse <= 0)
181                 {
182                         // unlink from chain
183                         for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
184                         {
185                                 if (*clumpchainpointer == clump)
186                                 {
187                                         *clumpchainpointer = clump->chain;
188                                         break;
189                                 }
190                         }
191                         pool->realsize -= sizeof(memclump_t);
192                         memset(clump, 0xBF, sizeof(memclump_t));
193                         free(clump);
194                 }
195                 else
196                 {
197                         // clump still has some allocations
198                         // force re-check of largest available space on next alloc
199                         clump->largestavailable = MEMBITS - clump->blocksinuse;
200                 }
201         }
202         else
203         {
204 #endif
205                 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
206                 if (developer.integer)
207                         memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
208                 free(mem);
209 #if MEMCLUMPING
210         }
211 #endif
212 }
213
214 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline)
215 {
216         mempool_t *pool;
217         if (developer.integer && developer_memorydebug.integer)
218                 _Mem_CheckSentinelsGlobal(filename, fileline);
219         pool = malloc(sizeof(mempool_t));
220         if (pool == NULL)
221                 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
222         memset(pool, 0, sizeof(mempool_t));
223         pool->sentinel1 = MEMHEADER_SENTINEL1;
224         pool->sentinel2 = MEMHEADER_SENTINEL1;
225         pool->filename = filename;
226         pool->fileline = fileline;
227         pool->flags = flags;
228         pool->chain = NULL;
229         pool->totalsize = 0;
230         pool->realsize = sizeof(mempool_t);
231         strlcpy (pool->name, name, sizeof (pool->name));
232         pool->parent = parent;
233         pool->next = poolchain;
234         poolchain = pool;
235         return pool;
236 }
237
238 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline)
239 {
240         mempool_t **chainaddress, *iter, *temp;
241
242         if (developer.integer && developer_memorydebug.integer)
243                 _Mem_CheckSentinelsGlobal(filename, fileline);
244         if (*pool)
245         {
246                 // unlink pool from chain
247                 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
248                 if (*chainaddress != *pool)
249                         Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
250                 if ((*pool)->sentinel1 != MEMHEADER_SENTINEL1)
251                         Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
252                 if ((*pool)->sentinel2 != MEMHEADER_SENTINEL1)
253                         Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", (*pool)->filename, (*pool)->fileline, filename, fileline);
254                 *chainaddress = (*pool)->next;
255
256                 // free memory owned by the pool
257                 while ((*pool)->chain)
258                         _Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)), filename, fileline);
259
260                 // free child pools, too
261                 for(iter = poolchain; iter; temp = iter = iter->next)
262                         if(iter->parent == *pool)
263                                 _Mem_FreePool(&temp, filename, fileline);
264
265                 // free the pool itself
266                 memset(*pool, 0xBF, sizeof(mempool_t));
267                 free(*pool);
268                 *pool = NULL;
269         }
270 }
271
272 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
273 {
274         mempool_t *chainaddress;
275
276         if (developer.integer && developer_memorydebug.integer)
277         {
278                 _Mem_CheckSentinelsGlobal(filename, fileline);
279                 // check if this pool is in the poolchain
280                 for (chainaddress = poolchain;chainaddress;chainaddress = chainaddress->next)
281                         if (chainaddress == pool)
282                                 break;
283                 if (!chainaddress)
284                         Sys_Error("Mem_EmptyPool: pool is already free (emptypool at %s:%i)", filename, fileline);
285         }
286         if (pool == NULL)
287                 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
288         if (pool->sentinel1 != MEMHEADER_SENTINEL1)
289                 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
290         if (pool->sentinel2 != MEMHEADER_SENTINEL1)
291                 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
292
293         // free memory owned by the pool
294         while (pool->chain)
295                 _Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)), filename, fileline);
296
297         // empty child pools, too
298         for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
299                 if(chainaddress->parent == pool)
300                         _Mem_EmptyPool(chainaddress, filename, fileline);
301
302 }
303
304 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
305 {
306         memheader_t *mem;
307
308         if (data == NULL)
309                 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
310
311         mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
312         if (mem->sentinel1 != MEMHEADER_SENTINEL1)
313                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
314         if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
315                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
316 }
317
318 #if MEMCLUMPING
319 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
320 {
321         // this isn't really very useful
322         if (clump->sentinel1 != MEMCLUMP_SENTINEL)
323                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
324         if (clump->sentinel2 != MEMCLUMP_SENTINEL)
325                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
326 }
327 #endif
328
329 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
330 {
331         memheader_t *mem;
332 #if MEMCLUMPING
333         memclump_t *clump;
334 #endif
335         mempool_t *pool;
336         for (pool = poolchain;pool;pool = pool->next)
337         {
338                 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
339                         Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
340                 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
341                         Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
342         }
343         for (pool = poolchain;pool;pool = pool->next)
344                 for (mem = pool->chain;mem;mem = mem->next)
345                         _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline);
346 #if MEMCLUMPING
347         for (pool = poolchain;pool;pool = pool->next)
348                 for (clump = pool->clumpchain;clump;clump = clump->chain)
349                         _Mem_CheckClumpSentinels(clump, filename, fileline);
350 #endif
351 }
352
353 qboolean Mem_IsAllocated(mempool_t *pool, void *data)
354 {
355         memheader_t *header;
356         memheader_t *target;
357
358         if (pool)
359         {
360                 // search only one pool
361                 target = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
362                 for( header = pool->chain ; header ; header = header->next )
363                         if( header == target )
364                                 return true;
365         }
366         else
367         {
368                 // search all pools
369                 for (pool = poolchain;pool;pool = pool->next)
370                         if (Mem_IsAllocated(pool, data))
371                                 return true;
372         }
373         return false;
374 }
375
376
377 // used for temporary memory allocations around the engine, not for longterm
378 // storage, if anything in this pool stays allocated during gameplay, it is
379 // considered a leak
380 mempool_t *tempmempool;
381 // only for zone
382 mempool_t *zonemempool;
383
384 void Mem_PrintStats(void)
385 {
386         size_t count = 0, size = 0;
387         mempool_t *pool;
388         memheader_t *mem;
389         Mem_CheckSentinelsGlobal();
390         for (pool = poolchain;pool;pool = pool->next)
391         {
392                 count++;
393                 size += pool->totalsize;
394         }
395         Con_Printf("%lu memory pools, totalling %lu bytes (%.3fMB)\n", (unsigned long)count, (unsigned long)size, size / 1048576.0);
396         for (pool = poolchain;pool;pool = pool->next)
397         {
398                 if ((pool->flags & POOLFLAG_TEMP) && pool->chain)
399                 {
400                         Con_Printf("Memory pool %p has sprung a leak totalling %lu bytes (%.3fMB)!  Listing contents...\n", pool, (unsigned long)pool->totalsize, pool->totalsize / 1048576.0);
401                         for (mem = pool->chain;mem;mem = mem->next)
402                                 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
403                 }
404         }
405 }
406
407 void Mem_PrintList(int listallocations)
408 {
409         mempool_t *pool;
410         memheader_t *mem;
411         Mem_CheckSentinelsGlobal();
412         Con_Print("memory pool list:\n"
413                    "size    name\n");
414         for (pool = poolchain;pool;pool = pool->next)
415         {
416                 Con_Printf("%10luk (%10luk actual) %s (%+li byte change) %s\n", (unsigned long) ((pool->totalsize + 1023) / 1024), (unsigned long)((pool->realsize + 1023) / 1024), pool->name, (long)pool->totalsize - pool->lastchecksize, (pool->flags & POOLFLAG_TEMP) ? "TEMP" : "");
417                 pool->lastchecksize = pool->totalsize;
418                 if (listallocations)
419                         for (mem = pool->chain;mem;mem = mem->next)
420                                 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
421         }
422 }
423
424 void MemList_f(void)
425 {
426         switch(Cmd_Argc())
427         {
428         case 1:
429                 Mem_PrintList(false);
430                 Mem_PrintStats();
431                 break;
432         case 2:
433                 if (!strcmp(Cmd_Argv(1), "all"))
434                 {
435                         Mem_PrintList(true);
436                         Mem_PrintStats();
437                         break;
438                 }
439                 // drop through
440         default:
441                 Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
442                 break;
443         }
444 }
445
446 extern void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal);
447 void MemStats_f(void)
448 {
449         Mem_CheckSentinelsGlobal();
450         R_TextureStats_Print(false, false, true);
451         Mem_PrintStats();
452 }
453
454
455 /*
456 ========================
457 Memory_Init
458 ========================
459 */
460 void Memory_Init (void)
461 {
462         tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL);
463         zonemempool = Mem_AllocPool("Zone", 0, NULL);
464         poolchain = NULL;
465 }
466
467 void Memory_Shutdown (void)
468 {
469 //      Mem_FreePool (&zonemempool);
470 //      Mem_FreePool (&tempmempool);
471 }
472
473 void Memory_Init_Commands (void)
474 {
475         Cmd_AddCommand ("memstats", MemStats_f);
476         Cmd_AddCommand ("memlist", MemList_f);
477         Cvar_RegisterVariable (&developer_memory);
478         Cvar_RegisterVariable (&developer_memorydebug);
479 }
480