]> icculus.org git repositories - divverent/darkplaces.git/blob - zone.c
fix crashs on maps with missing textures
[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 mempool_t *poolchain = NULL;
25
26 void *_Mem_Alloc(mempool_t *pool, int size, const char *filename, int fileline)
27 {
28 #if MEMCLUMPING
29         int i, j, k, needed, endbit, largest;
30         memclump_t *clump, **clumpchainpointer;
31 #endif
32         memheader_t *mem;
33         if (size <= 0)
34                 return NULL;
35         if (pool == NULL)
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;
39 #if MEMCLUMPING
40         if (size < 4096)
41         {
42                 // clumping
43                 needed = (sizeof(memheader_t) + size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT;
44                 endbit = MEMBITS - needed;
45                 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
46                 {
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)
53                         {
54                                 largest = 0;
55                                 for (i = 0;i < endbit;i++)
56                                 {
57                                         if (clump->bits[i >> 5] & (1 << (i & 31)))
58                                                 continue;
59                                         k = i + needed;
60                                         for (j = i;i < k;i++)
61                                                 if (clump->bits[i >> 5] & (1 << (i & 31)))
62                                                         goto loopcontinue;
63                                         goto choseclump;
64         loopcontinue:;
65                                         if (largest < j - i)
66                                                 largest = j - i;
67                                 }
68                                 // since clump falsely advertised enough space (nothing wrong
69                                 // with that), update largest count to avoid wasting time in
70                                 // later allocations
71                                 clump->largestavailable = largest;
72                         }
73                 }
74                 pool->realsize += sizeof(memclump_t);
75                 clump = malloc(sizeof(memclump_t));
76                 if (clump == NULL)
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;
82                 clump->chain = NULL;
83                 clump->blocksinuse = 0;
84                 clump->largestavailable = MEMBITS - needed;
85                 j = 0;
86 choseclump:
87                 mem = (memheader_t *)((qbyte *) clump->block + j * MEMUNIT);
88                 mem->clump = clump;
89                 clump->blocksinuse += needed;
90                 for (i = j + needed;j < i;j++)
91                         clump->bits[j >> 5] |= (1 << (j & 31));
92         }
93         else
94         {
95                 // big allocations are not clumped
96 #endif
97                 pool->realsize += sizeof(memheader_t) + size + sizeof(int);
98                 mem = malloc(sizeof(memheader_t) + size + sizeof(int));
99                 if (mem == NULL)
100                         Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
101 #if MEMCLUMPING
102                 mem->clump = NULL;
103         }
104 #endif
105         mem->filename = filename;
106         mem->fileline = fileline;
107         mem->size = size;
108         mem->pool = pool;
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;
114         mem->prev = NULL;
115         pool->chain = mem;
116         if (mem->next)
117                 mem->next->prev = mem;
118         memset((void *)((qbyte *) mem + sizeof(memheader_t)), 0, mem->size);
119         return (void *)((qbyte *) mem + sizeof(memheader_t));
120 }
121
122 void _Mem_Free(void *data, const char *filename, int fileline)
123 {
124 #if MEMCLUMPING
125         int i, firstblock, endblock;
126         memclump_t *clump, **clumpchainpointer;
127 #endif
128         memheader_t *mem;
129         mempool_t *pool;
130         if (data == NULL)
131                 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
132
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);
138         pool = mem->pool;
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);
143         if (mem->prev)
144                 mem->prev->next = mem->next;
145         else
146                 pool->chain = mem->next;
147         if (mem->next)
148                 mem->next->prev = mem->prev;
149         // memheader has been unlinked, do the actual free now
150         pool->totalsize -= mem->size;
151 #if MEMCLUMPING
152         if ((clump = mem->clump))
153         {
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)
168                 {
169                         // unlink from chain
170                         for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
171                         {
172                                 if (*clumpchainpointer == clump)
173                                 {
174                                         *clumpchainpointer = clump->chain;
175                                         break;
176                                 }
177                         }
178                         pool->realsize -= sizeof(memclump_t);
179                         memset(clump, 0xBF, sizeof(memclump_t));
180                         free(clump);
181                 }
182                 else
183                 {
184                         // clump still has some allocations
185                         // force re-check of largest available space on next alloc
186                         clump->largestavailable = MEMBITS - clump->blocksinuse;
187                 }
188         }
189         else
190         {
191 #endif
192                 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
193                 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
194                 free(mem);
195 #if MEMCLUMPING
196         }
197 #endif
198 }
199
200 mempool_t *_Mem_AllocPool(const char *name, const char *filename, int fileline)
201 {
202         mempool_t *pool;
203         pool = malloc(sizeof(mempool_t));
204         if (pool == NULL)
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;
211         pool->chain = NULL;
212         pool->totalsize = 0;
213         pool->realsize = sizeof(mempool_t);
214         strcpy(pool->name, name);
215         pool->next = poolchain;
216         poolchain = pool;
217         return pool;
218 }
219
220 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline)
221 {
222         mempool_t **chainaddress;
223         if (*pool)
224         {
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;
234
235                 // free memory owned by the pool
236                 while ((*pool)->chain)
237                         Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
238
239                 // free the pool itself
240                 memset(*pool, 0xBF, sizeof(mempool_t));
241                 free(*pool);
242                 *pool = NULL;
243         }
244 }
245
246 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
247 {
248         if (pool == NULL)
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);
254
255         // free memory owned by the pool
256         while (pool->chain)
257                 Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
258 }
259
260 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
261 {
262         memheader_t *mem;
263
264         if (data == NULL)
265                 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
266
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);
272 }
273
274 #if MEMCLUMPING
275 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
276 {
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);
282 }
283 #endif
284
285 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
286 {
287         memheader_t *mem;
288 #if MEMCLUMPING
289         memclump_t *clump;
290 #endif
291         mempool_t *pool;
292         for (pool = poolchain;pool;pool = pool->next)
293         {
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);
298         }
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);
302 #if MEMCLUMPING
303         for (pool = poolchain;pool;pool = pool->next)
304                 for (clump = pool->clumpchain;clump;clump = clump->chain)
305                         _Mem_CheckClumpSentinels(clump, filename, fileline);
306 #endif
307 }
308
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
311 // considered a leak
312 mempool_t *tempmempool;
313 // only for zone
314 mempool_t *zonemempool;
315
316 void Mem_PrintStats(void)
317 {
318         int count = 0, size = 0;
319         mempool_t *pool;
320         memheader_t *mem;
321         Mem_CheckSentinelsGlobal();
322         for (pool = poolchain;pool;pool = pool->next)
323         {
324                 count++;
325                 size += pool->totalsize;
326         }
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)
331         {
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);
336         }
337 }
338
339 void Mem_PrintList(int listallocations)
340 {
341         mempool_t *pool;
342         memheader_t *mem;
343         Mem_CheckSentinelsGlobal();
344         Con_Printf("memory pool list:\n"
345                    "size    name\n");
346         for (pool = poolchain;pool;pool = pool->next)
347         {
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);
350                 else
351                         Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
352                 pool->lastchecksize = pool->totalsize;
353                 if (listallocations)
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);
356         }
357 }
358
359 void MemList_f(void)
360 {
361         switch(Cmd_Argc())
362         {
363         case 1:
364                 Mem_PrintList(false);
365                 Mem_PrintStats();
366                 break;
367         case 2:
368                 if (!strcmp(Cmd_Argv(1), "all"))
369                 {
370                         Mem_PrintList(true);
371                         Mem_PrintStats();
372                         break;
373                 }
374                 // drop through
375         default:
376                 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
377                 break;
378         }
379 }
380
381 extern void R_TextureStats_PrintTotal(void);
382 void MemStats_f(void)
383 {
384         Mem_CheckSentinelsGlobal();
385         R_TextureStats_PrintTotal();
386         Mem_PrintStats();
387 }
388
389
390 /*
391 ========================
392 Memory_Init
393 ========================
394 */
395 void Memory_Init (void)
396 {
397         tempmempool = Mem_AllocPool("Temporary Memory");
398         zonemempool = Mem_AllocPool("Zone");
399 }
400
401 void Memory_Init_Commands (void)
402 {
403         Cmd_AddCommand ("memstats", MemStats_f);
404         Cmd_AddCommand ("memlist", MemList_f);
405 }
406