]> icculus.org git repositories - divverent/darkplaces.git/blob - zone.c
get rid of velocity jittering on rain and snow, except for horizontal jitter on snow...
[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         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         pool->next = poolchain;
197         poolchain = pool;
198         return pool;
199 }
200
201 void _Mem_FreePool(mempool_t **pool, char *filename, int fileline)
202 {
203         mempool_t **chainaddress;
204         if (*pool)
205         {
206                 // unlink pool from chain
207                 for (chainaddress = &poolchain;*chainaddress && *chainaddress != *pool;chainaddress = &((*chainaddress)->next));
208                 if (*chainaddress != *pool)
209                         Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
210                 *chainaddress = (*pool)->next;
211
212                 // free memory owned by the pool
213                 while ((*pool)->chain)
214                         Mem_Free((void *)((qbyte *) (*pool)->chain + sizeof(memheader_t)));
215
216                 // free the pool itself
217                 memset(*pool, 0xBF, sizeof(mempool_t));
218                 free(*pool);
219                 *pool = NULL;
220         }
221 }
222
223 void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline)
224 {
225         if (pool == NULL)
226                 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
227
228         // free memory owned by the pool
229         while (pool->chain)
230                 Mem_Free((void *)((qbyte *) pool->chain + sizeof(memheader_t)));
231 }
232
233 void _Mem_CheckSentinels(void *data, char *filename, int fileline)
234 {
235         memheader_t *mem;
236
237         if (data == NULL)
238                 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
239
240         mem = (memheader_t *)((qbyte *) data - sizeof(memheader_t));
241         if (mem->sentinel1 != MEMHEADER_SENTINEL1)
242                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
243         if (*((qbyte *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
244                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
245 }
246
247 static void _Mem_CheckClumpSentinels(memclump_t *clump, char *filename, int fileline)
248 {
249         // this isn't really very useful
250         if (clump->sentinel1 != MEMCLUMP_SENTINEL)
251                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
252         if (clump->sentinel2 != MEMCLUMP_SENTINEL)
253                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
254 }
255
256 void _Mem_CheckSentinelsGlobal(char *filename, int fileline)
257 {
258         memheader_t *mem;
259         memclump_t *clump;
260         mempool_t *pool;
261         for (pool = poolchain;pool;pool = pool->next)
262         {
263                 for (mem = pool->chain;mem;mem = mem->chain)
264                         _Mem_CheckSentinels((void *)((qbyte *) mem + sizeof(memheader_t)), filename, fileline);
265                 for (clump = pool->clumpchain;clump;clump = clump->chain)
266                         _Mem_CheckClumpSentinels(clump, filename, fileline);
267         }
268 }
269
270 // used for temporary memory allocations around the engine, not for longterm
271 // storage, if anything in this pool stays allocated during gameplay, it is
272 // considered a leak
273 mempool_t *tempmempool;
274 // only for zone
275 mempool_t *zonemempool;
276
277 void Mem_PrintStats(void)
278 {
279         int count = 0, size = 0;
280         mempool_t *pool;
281         memheader_t *mem;
282         Mem_CheckSentinelsGlobal();
283         for (pool = poolchain;pool;pool = pool->next)
284         {
285                 count++;
286                 size += pool->totalsize;
287         }
288         Con_Printf("%i memory pools, totalling %i bytes (%.3fMB)\n", count, size, size / 1048576.0);
289         if (tempmempool == NULL)
290                 Con_Printf("Error: no tempmempool allocated\n");
291         else if (tempmempool->chain)
292         {
293                 Con_Printf("%i bytes (%.3fMB) of temporary memory still allocated (Leak!)\n", tempmempool->totalsize, tempmempool->totalsize / 1048576.0);
294                 Con_Printf("listing temporary memory allocations:\n");
295                 for (mem = tempmempool->chain;mem;mem = mem->chain)
296                         Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
297         }
298 }
299
300 void Mem_PrintList(int listallocations)
301 {
302         mempool_t *pool;
303         memheader_t *mem;
304         Mem_CheckSentinelsGlobal();
305         Con_Printf("memory pool list:\n"
306                    "size    name\n");
307         for (pool = poolchain;pool;pool = pool->next)
308         {
309                 if (pool->lastchecksize != 0 && pool->totalsize != pool->lastchecksize)
310                         Con_Printf("%6ik (%6ik actual) %s (%i byte change)\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name, pool->totalsize - pool->lastchecksize);
311                 else
312                         Con_Printf("%6ik (%6ik actual) %s\n", (pool->totalsize + 1023) / 1024, (pool->realsize + 1023) / 1024, pool->name);
313                 pool->lastchecksize = pool->totalsize;
314                 if (listallocations)
315                         for (mem = pool->chain;mem;mem = mem->chain)
316                                 Con_Printf("%10i bytes allocated at %s:%i\n", mem->size, mem->filename, mem->fileline);
317         }
318 }
319
320 void MemList_f(void)
321 {
322         switch(Cmd_Argc())
323         {
324         case 1:
325                 Mem_PrintList(false);
326                 Mem_PrintStats();
327                 break;
328         case 2:
329                 if (!strcmp(Cmd_Argv(1), "all"))
330                 {
331                         Mem_PrintList(true);
332                         Mem_PrintStats();
333                         break;
334                 }
335                 // drop through
336         default:
337                 Con_Printf("MemList_f: unrecognized options\nusage: memlist [all]\n");
338                 break;
339         }
340 }
341
342 extern void R_TextureStats_PrintTotal(void);
343 void MemStats_f(void)
344 {
345         Mem_CheckSentinelsGlobal();
346         R_TextureStats_PrintTotal();
347         Mem_PrintStats();
348 }
349
350
351 /*
352 ========================
353 Memory_Init
354 ========================
355 */
356 void Memory_Init (void)
357 {
358         tempmempool = Mem_AllocPool("Temporary Memory");
359         zonemempool = Mem_AllocPool("Zone");
360 }
361
362 void Memory_Init_Commands (void)
363 {
364         Cmd_AddCommand ("memstats", MemStats_f);
365         Cmd_AddCommand ("memlist", MemList_f);
366 }
367