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