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