]> icculus.org git repositories - divverent/darkplaces.git/blob - zone.c
reorganized shader rendering calls to process the whole surface chain multiple times...
[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 *)((long) 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_SENTINEL;
104         *((int *)((long) mem + sizeof(memheader_t) + mem->size)) = MEMHEADER_SENTINEL;
105         // append to head of list
106         mem->chain = pool->chain;
107         pool->chain = mem;
108         memset((void *)((long) mem + sizeof(memheader_t)), 0, mem->size);
109         return (void *)((long) mem + sizeof(memheader_t));
110 }
111
112 void _Mem_Free(void *data, char *filename, int fileline)
113 {
114         int i, firstblock, endblock;
115         memclump_t *clump, **clumpchainpointer;
116         memheader_t *mem, **memchainpointer;
117         mempool_t *pool;
118         if (data == NULL)
119                 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
120
121
122         mem = (memheader_t *)((long) data - sizeof(memheader_t));
123         if (mem->sentinel1 != MEMHEADER_SENTINEL)
124                 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
125         if (*((int *)((long) mem + sizeof(memheader_t) + mem->size)) != MEMHEADER_SENTINEL)
126                 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
127         pool = mem->pool;
128         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);
129         for (memchainpointer = &pool->chain;*memchainpointer;memchainpointer = &(*memchainpointer)->chain)
130         {
131                 if (*memchainpointer == mem)
132                 {
133                         *memchainpointer = mem->chain;
134                         pool->totalsize -= mem->size;
135                         if ((clump = mem->clump))
136                         {
137                                 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
138                                         Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
139                                 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
140                                         Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
141                                 firstblock = ((long) mem - (long) clump->block);
142                                 if (firstblock & (MEMUNIT - 1))
143                                         Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
144                                 firstblock /= MEMUNIT;
145                                 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
146                                 clump->blocksinuse -= endblock - firstblock;
147                                 // could use &, but we know the bit is set
148                                 for (i = firstblock;i < endblock;i++)
149                                         clump->bits[i >> 5] -= (1 << (i & 31));
150                                 if (clump->blocksinuse <= 0)
151                                 {
152                                         // unlink from chain
153                                         for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
154                                         {
155                                                 if (*clumpchainpointer == clump)
156                                                 {
157                                                         *clumpchainpointer = clump->chain;
158                                                         break;
159                                                 }
160                                         }
161                                         pool->realsize -= sizeof(memclump_t);
162                                         memset(clump, 0xBF, sizeof(memclump_t));
163                                         free(clump);
164                                 }
165                                 else
166                                 {
167                                         // clump still has some allocations
168                                         // force re-check of largest available space on next alloc
169                                         clump->largestavailable = MEMBITS - clump->blocksinuse;
170                                 }
171                         }
172                         else
173                         {
174                                 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
175                                 memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
176                                 free(mem);
177                         }
178                         return;
179                 }
180         }
181         Sys_Error("Mem_Free: not allocated (free at %s:%i)", filename, fileline);
182 }
183
184 mempool_t *_Mem_AllocPool(char *name, char *filename, int fileline)
185 {
186 //      int i;
187         mempool_t *pool;
188         pool = malloc(sizeof(mempool_t));
189         if (pool == NULL)
190                 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
191         memset(pool, 0, sizeof(mempool_t));
192         pool->chain = NULL;
193         pool->totalsize = 0;
194         pool->realsize = sizeof(mempool_t);
195         strcpy(pool->name, name);
196 //      for (i = 0;i < (POOLNAMESIZE - 1) && name[i];i++)
197 //              pool->name[i] = name[i];
198 //      for (i = 0;i < POOLNAMESIZE;i++)
199 //              pool->name[i] = 0;
200         pool->next = poolchain;
201         poolchain = pool;
202         return pool;
203 }
204
205 void _Mem_FreePool(mempool_t **pool, char *filename, int fileline)
206 {
207         mempool_t **chainaddress;
208         if (*pool)
209         {
210                 // unlink pool from chain
211                 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
212                 if (*chainaddress != *pool)
213                         Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
214                 *chainaddress = (*pool)->next;
215
216                 // free memory owned by the pool
217                 while ((*pool)->chain)
218                         Mem_Free((void *)((long) (*pool)->chain + sizeof(memheader_t)));
219
220                 // free the pool itself
221                 memset(*pool, 0xBF, sizeof(mempool_t));
222                 free(*pool);
223                 *pool = NULL;
224         }
225 }
226
227 void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline)
228 {
229         if (pool == NULL)
230                 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
231
232         // free memory owned by the pool
233         while (pool->chain)
234                 Mem_Free((void *)((long) pool->chain + sizeof(memheader_t)));
235 }
236
237 void _Mem_CheckSentinels(void *data, char *filename, int fileline)
238 {
239         memheader_t *mem;
240
241         if (data == NULL)
242                 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
243
244         mem = (memheader_t *)((long) data - sizeof(memheader_t));
245         if (mem->sentinel1 != MEMHEADER_SENTINEL)
246                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
247         if (*((int *)((long) mem + sizeof(memheader_t) + mem->size)) != MEMHEADER_SENTINEL)
248                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
249 }
250
251 static void _Mem_CheckClumpSentinels(memclump_t *clump, char *filename, int fileline)
252 {
253         // this isn't really very useful
254         if (clump->sentinel1 != MEMCLUMP_SENTINEL)
255                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
256         if (clump->sentinel2 != MEMCLUMP_SENTINEL)
257                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
258 }
259
260 void _Mem_CheckSentinelsGlobal(char *filename, int fileline)
261 {
262         memheader_t *mem;
263         memclump_t *clump;
264         mempool_t *pool;
265         for (pool = poolchain;pool;pool = pool->next)
266         {
267                 for (mem = pool->chain;mem;mem = mem->chain)
268                         _Mem_CheckSentinels((void *)((long) mem + sizeof(memheader_t)), filename, fileline);
269                 for (clump = pool->clumpchain;clump;clump = clump->chain)
270                         _Mem_CheckClumpSentinels(clump, filename, fileline);
271         }
272 }
273
274 // used for temporary memory allocations around the engine, not for longterm
275 // storage, if anything in this pool stays allocated during gameplay, it is
276 // considered a leak
277 mempool_t *tempmempool;
278 // only for zone
279 mempool_t *zonemempool;
280
281 void Mem_PrintStats(void)
282 {
283         int count = 0, size = 0;
284         mempool_t *pool;
285         memheader_t *mem;
286         Mem_CheckSentinelsGlobal();
287         for (pool = poolchain;pool;pool = pool->next)
288         {
289                 count++;
290                 size += pool->totalsize;
291         }
292         Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
293         if (tempmempool == NULL)
294                 Con_Printf("Error: no tempmempool allocated\n");
295         else if (tempmempool->chain)
296         {
297                 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
298                 Con_Printf("listing temporary memory allocations:\n");
299                 for (mem = tempmempool->chain;mem;mem = mem->chain)
300                         Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
301         }
302 }
303
304 void Mem_PrintList(int listallocations)
305 {
306         mempool_t *pool;
307         memheader_t *mem;
308         Mem_CheckSentinelsGlobal();
309         Con_Printf("memory pool list:\n"
310                    "size    name\n");
311         for (pool = poolchain;pool;pool = pool->next)
312         {
313                 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
314                         Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
315                 else
316                         Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
317                 pool->lastchecksize = pool->totalsize;
318                 if (listallocations)
319                         for (mem = pool->chain;mem;mem = mem->chain)
320                                 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
321         }
322 }
323
324 void MemList_f(void)
325 {
326         switch(Cmd_Argc())
327         {
328         case 1:
329                 Mem_PrintList(false);
330                 Mem_PrintStats();
331                 break;
332         case 2:
333                 if (!strcmp(Cmd_Argv(1), "all"))
334                 {
335                         Mem_PrintList(true);
336                         Mem_PrintStats();
337                         break;
338                 }
339                 // drop through
340         default:
341                 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
342                 break;
343         }
344 }
345
346 extern void R_TextureStats_PrintTotal(void);
347 void MemStats_f(void)
348 {
349         Mem_CheckSentinelsGlobal();
350         R_TextureStats_PrintTotal();
351         Mem_PrintStats();
352 }
353
354
355 /*
356 ========================
357 Memory_Init
358 ========================
359 */
360 void Memory_Init (void)
361 {
362         tempmempool = Mem_AllocPool("Temporary Memory");
363         zonemempool = Mem_AllocPool("Zone");
364 }
365
366 void Memory_Init_Commands (void)
367 {
368         Cmd_AddCommand ("memstats", MemStats_f);
369         Cmd_AddCommand ("memlist", MemList_f);
370 }
371