]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - zone.c
make other parts of DP able to retrieve the gamma table, and add a serial field so...
[xonotic/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", "prints debugging information about memory allocations"};
25 cvar_t developer_memorydebug = {0, "developer_memorydebug", "0", "enables memory corruption checks (very slow)"};
26
27 mempool_t *poolchain = NULL;
28
29 void *_Mem_Alloc(mempool_t *pool, size_t size, const char *filename, int fileline)
30 {
31 #if MEMCLUMPING
32         int i, j, k, needed, endbit, largest;
33         memclump_t *clump, **clumpchainpointer;
34 #endif
35         memheader_t *mem;
36         if (size <= 0)
37                 return NULL;
38         if (pool == NULL)
39                 Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline);
40         if (developer.integer && developer_memory.integer)
41                 Con_Printf("Mem_Alloc: pool %s, file %s:%i, size %i bytes\n", pool->name, filename, fileline, (int)size);
42         if (developer.integer && developer_memorydebug.integer)
43                 _Mem_CheckSentinelsGlobal(filename, fileline);
44         pool->totalsize += size;
45 #if MEMCLUMPING
46         if (size < 4096)
47         {
48                 // clumping
49                 needed = (sizeof(memheader_t) + size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT;
50                 endbit = MEMBITS - needed;
51                 for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
52                 {
53                         clump = *clumpchainpointer;
54                         if (clump->sentinel1 != MEMCLUMP_SENTINEL)
55                                 Sys_Error("Mem_Alloc: trashed clump sentinel 1 (alloc at %s:%d)", filename, fileline);
56                         if (clump->sentinel2 != MEMCLUMP_SENTINEL)
57                                 Sys_Error("Mem_Alloc: trashed clump sentinel 2 (alloc at %s:%d)", filename, fileline);
58                         if (clump->largestavailable >= needed)
59                         {
60                                 largest = 0;
61                                 for (i = 0;i < endbit;i++)
62                                 {
63                                         if (clump->bits[i >> 5] & (1 << (i & 31)))
64                                                 continue;
65                                         k = i + needed;
66                                         for (j = i;i < k;i++)
67                                                 if (clump->bits[i >> 5] & (1 << (i & 31)))
68                                                         goto loopcontinue;
69                                         goto choseclump;
70         loopcontinue:;
71                                         if (largest < j - i)
72                                                 largest = j - i;
73                                 }
74                                 // since clump falsely advertised enough space (nothing wrong
75                                 // with that), update largest count to avoid wasting time in
76                                 // later allocations
77                                 clump->largestavailable = largest;
78                         }
79                 }
80                 pool->realsize += sizeof(memclump_t);
81                 clump = malloc(sizeof(memclump_t));
82                 if (clump == NULL)
83                         Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
84                 memset(clump, 0, sizeof(memclump_t));
85                 *clumpchainpointer = clump;
86                 clump->sentinel1 = MEMCLUMP_SENTINEL;
87                 clump->sentinel2 = MEMCLUMP_SENTINEL;
88                 clump->chain = NULL;
89                 clump->blocksinuse = 0;
90                 clump->largestavailable = MEMBITS - needed;
91                 j = 0;
92 choseclump:
93                 mem = (memheader_t *)((unsigned char *) clump->block + j * MEMUNIT);
94                 mem->clump = clump;
95                 clump->blocksinuse += needed;
96                 for (i = j + needed;j < i;j++)
97                         clump->bits[j >> 5] |= (1 << (j & 31));
98         }
99         else
100         {
101                 // big allocations are not clumped
102 #endif
103                 pool->realsize += sizeof(memheader_t) + size + sizeof(int);
104                 mem = (memheader_t *)malloc(sizeof(memheader_t) + size + sizeof(int));
105                 if (mem == NULL)
106                         Sys_Error("Mem_Alloc: out of memory (alloc at %s:%i)", filename, fileline);
107 #if MEMCLUMPING
108                 mem->clump = NULL;
109         }
110 #endif
111         mem->filename = filename;
112         mem->fileline = fileline;
113         mem->size = size;
114         mem->pool = pool;
115         mem->sentinel1 = MEMHEADER_SENTINEL1;
116         // 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
117         *((unsigned char *) mem + sizeof(memheader_t) + mem->size) = MEMHEADER_SENTINEL2;
118         // append to head of list
119         mem->next = pool->chain;
120         mem->prev = NULL;
121         pool->chain = mem;
122         if (mem->next)
123                 mem->next->prev = mem;
124         memset((void *)((unsigned char *) mem + sizeof(memheader_t)), 0, mem->size);
125         return (void *)((unsigned char *) mem + sizeof(memheader_t));
126 }
127
128 // only used by _Mem_Free and _Mem_FreePool
129 static void _Mem_FreeBlock(memheader_t *mem, const char *filename, int fileline)
130 {
131 #if MEMCLUMPING
132         int i, firstblock, endblock;
133         memclump_t *clump, **clumpchainpointer;
134 #endif
135         mempool_t *pool;
136         if (mem->sentinel1 != MEMHEADER_SENTINEL1)
137                 Sys_Error("Mem_Free: trashed header sentinel 1 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
138         if (*((unsigned char *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
139                 Sys_Error("Mem_Free: trashed header sentinel 2 (alloc at %s:%i, free at %s:%i)", mem->filename, mem->fileline, filename, fileline);
140         pool = mem->pool;
141         if (developer.integer && developer_memory.integer)
142                 Con_Printf("Mem_Free: pool %s, alloc %s:%i, free %s:%i, size %i bytes\n", pool->name, mem->filename, mem->fileline, filename, fileline, (int)(mem->size));
143         // unlink memheader from doubly linked list
144         if ((mem->prev ? mem->prev->next != mem : pool->chain != mem) || (mem->next && mem->next->prev != mem))
145                 Sys_Error("Mem_Free: not allocated or double freed (free at %s:%i)", filename, fileline);
146         if (mem->prev)
147                 mem->prev->next = mem->next;
148         else
149                 pool->chain = mem->next;
150         if (mem->next)
151                 mem->next->prev = mem->prev;
152         // memheader has been unlinked, do the actual free now
153         pool->totalsize -= mem->size;
154 #if MEMCLUMPING
155         if ((clump = mem->clump))
156         {
157                 if (clump->sentinel1 != MEMCLUMP_SENTINEL)
158                         Sys_Error("Mem_Free: trashed clump sentinel 1 (free at %s:%i)", filename, fileline);
159                 if (clump->sentinel2 != MEMCLUMP_SENTINEL)
160                         Sys_Error("Mem_Free: trashed clump sentinel 2 (free at %s:%i)", filename, fileline);
161                 firstblock = ((unsigned char *) mem - (unsigned char *) clump->block);
162                 if (firstblock & (MEMUNIT - 1))
163                         Sys_Error("Mem_Free: address not valid in clump (free at %s:%i)", filename, fileline);
164                 firstblock /= MEMUNIT;
165                 endblock = firstblock + ((sizeof(memheader_t) + mem->size + sizeof(int) + (MEMUNIT - 1)) / MEMUNIT);
166                 clump->blocksinuse -= endblock - firstblock;
167                 // could use &, but we know the bit is set
168                 for (i = firstblock;i < endblock;i++)
169                         clump->bits[i >> 5] -= (1 << (i & 31));
170                 if (clump->blocksinuse <= 0)
171                 {
172                         // unlink from chain
173                         for (clumpchainpointer = &pool->clumpchain;*clumpchainpointer;clumpchainpointer = &(*clumpchainpointer)->chain)
174                         {
175                                 if (*clumpchainpointer == clump)
176                                 {
177                                         *clumpchainpointer = clump->chain;
178                                         break;
179                                 }
180                         }
181                         pool->realsize -= sizeof(memclump_t);
182                         memset(clump, 0xBF, sizeof(memclump_t));
183                         free(clump);
184                 }
185                 else
186                 {
187                         // clump still has some allocations
188                         // force re-check of largest available space on next alloc
189                         clump->largestavailable = MEMBITS - clump->blocksinuse;
190                 }
191         }
192         else
193         {
194 #endif
195                 pool->realsize -= sizeof(memheader_t) + mem->size + sizeof(int);
196                 if (developer_memorydebug.integer)
197                         memset(mem, 0xBF, sizeof(memheader_t) + mem->size + sizeof(int));
198                 free(mem);
199 #if MEMCLUMPING
200         }
201 #endif
202 }
203
204 void _Mem_Free(void *data, const char *filename, int fileline)
205 {
206         if (data == NULL)
207                 Sys_Error("Mem_Free: data == NULL (called at %s:%i)", filename, fileline);
208
209         if (developer.integer && developer_memorydebug.integer)
210         {
211                 _Mem_CheckSentinelsGlobal(filename, fileline);
212                 if (!Mem_IsAllocated(NULL, data))
213                         Sys_Error("Mem_Free: data is not allocated (called at %s:%i)", filename, fileline);
214         }
215
216         _Mem_FreeBlock((memheader_t *)((unsigned char *) data - sizeof(memheader_t)), filename, fileline);
217 }
218
219 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline)
220 {
221         mempool_t *pool;
222         if (developer.integer && developer_memorydebug.integer)
223                 _Mem_CheckSentinelsGlobal(filename, fileline);
224         pool = (mempool_t *)malloc(sizeof(mempool_t));
225         if (pool == NULL)
226                 Sys_Error("Mem_AllocPool: out of memory (allocpool at %s:%i)", filename, fileline);
227         memset(pool, 0, sizeof(mempool_t));
228         pool->sentinel1 = MEMHEADER_SENTINEL1;
229         pool->sentinel2 = MEMHEADER_SENTINEL1;
230         pool->filename = filename;
231         pool->fileline = fileline;
232         pool->flags = flags;
233         pool->chain = NULL;
234         pool->totalsize = 0;
235         pool->realsize = sizeof(mempool_t);
236         strlcpy (pool->name, name, sizeof (pool->name));
237         pool->parent = parent;
238         pool->next = poolchain;
239         poolchain = pool;
240         return pool;
241 }
242
243 void _Mem_FreePool(mempool_t **poolpointer, const char *filename, int fileline)
244 {
245         mempool_t *pool = *poolpointer;
246         mempool_t **chainaddress, *iter, *temp;
247
248         if (developer.integer && developer_memorydebug.integer)
249                 _Mem_CheckSentinelsGlobal(filename, fileline);
250         if (pool)
251         {
252                 // unlink pool from chain
253                 for (chainaddress = &poolchain;*chainaddress && *chainaddress != pool;chainaddress = &((*chainaddress)->next));
254                 if (*chainaddress != pool)
255                         Sys_Error("Mem_FreePool: pool already free (freepool at %s:%i)", filename, fileline);
256                 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
257                         Sys_Error("Mem_FreePool: trashed pool sentinel 1 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
258                 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
259                         Sys_Error("Mem_FreePool: trashed pool sentinel 2 (allocpool at %s:%i, freepool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
260                 *chainaddress = pool->next;
261
262                 // free memory owned by the pool
263                 while (pool->chain)
264                         _Mem_FreeBlock(pool->chain, filename, fileline);
265
266                 // free child pools, too
267                 for(iter = poolchain; iter; temp = iter = iter->next)
268                         if(iter->parent == pool)
269                                 _Mem_FreePool(&temp, filename, fileline);
270
271                 // free the pool itself
272                 memset(pool, 0xBF, sizeof(mempool_t));
273                 free(pool);
274
275                 *poolpointer = NULL;
276         }
277 }
278
279 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline)
280 {
281         mempool_t *chainaddress;
282
283         if (developer.integer && developer_memorydebug.integer)
284         {
285                 _Mem_CheckSentinelsGlobal(filename, fileline);
286                 // check if this pool is in the poolchain
287                 for (chainaddress = poolchain;chainaddress;chainaddress = chainaddress->next)
288                         if (chainaddress == pool)
289                                 break;
290                 if (!chainaddress)
291                         Sys_Error("Mem_EmptyPool: pool is already free (emptypool at %s:%i)", filename, fileline);
292         }
293         if (pool == NULL)
294                 Sys_Error("Mem_EmptyPool: pool == NULL (emptypool at %s:%i)", filename, fileline);
295         if (pool->sentinel1 != MEMHEADER_SENTINEL1)
296                 Sys_Error("Mem_EmptyPool: trashed pool sentinel 1 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
297         if (pool->sentinel2 != MEMHEADER_SENTINEL1)
298                 Sys_Error("Mem_EmptyPool: trashed pool sentinel 2 (allocpool at %s:%i, emptypool at %s:%i)", pool->filename, pool->fileline, filename, fileline);
299
300         // free memory owned by the pool
301         while (pool->chain)
302                 _Mem_FreeBlock(pool->chain, filename, fileline);
303
304         // empty child pools, too
305         for(chainaddress = poolchain; chainaddress; chainaddress = chainaddress->next)
306                 if(chainaddress->parent == pool)
307                         _Mem_EmptyPool(chainaddress, filename, fileline);
308
309 }
310
311 void _Mem_CheckSentinels(void *data, const char *filename, int fileline)
312 {
313         memheader_t *mem;
314
315         if (data == NULL)
316                 Sys_Error("Mem_CheckSentinels: data == NULL (sentinel check at %s:%i)", filename, fileline);
317
318         mem = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
319         if (mem->sentinel1 != MEMHEADER_SENTINEL1)
320                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 1 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
321         if (*((unsigned char *) mem + sizeof(memheader_t) + mem->size) != MEMHEADER_SENTINEL2)
322                 Sys_Error("Mem_CheckSentinels: trashed header sentinel 2 (block allocated at %s:%i, sentinel check at %s:%i)", mem->filename, mem->fileline, filename, fileline);
323 }
324
325 #if MEMCLUMPING
326 static void _Mem_CheckClumpSentinels(memclump_t *clump, const char *filename, int fileline)
327 {
328         // this isn't really very useful
329         if (clump->sentinel1 != MEMCLUMP_SENTINEL)
330                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 1 (sentinel check at %s:%i)", filename, fileline);
331         if (clump->sentinel2 != MEMCLUMP_SENTINEL)
332                 Sys_Error("Mem_CheckClumpSentinels: trashed sentinel 2 (sentinel check at %s:%i)", filename, fileline);
333 }
334 #endif
335
336 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline)
337 {
338         memheader_t *mem;
339 #if MEMCLUMPING
340         memclump_t *clump;
341 #endif
342         mempool_t *pool;
343         for (pool = poolchain;pool;pool = pool->next)
344         {
345                 if (pool->sentinel1 != MEMHEADER_SENTINEL1)
346                         Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 1 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
347                 if (pool->sentinel2 != MEMHEADER_SENTINEL1)
348                         Sys_Error("Mem_CheckSentinelsGlobal: trashed pool sentinel 2 (allocpool at %s:%i, sentinel check at %s:%i)", pool->filename, pool->fileline, filename, fileline);
349         }
350         for (pool = poolchain;pool;pool = pool->next)
351                 for (mem = pool->chain;mem;mem = mem->next)
352                         _Mem_CheckSentinels((void *)((unsigned char *) mem + sizeof(memheader_t)), filename, fileline);
353 #if MEMCLUMPING
354         for (pool = poolchain;pool;pool = pool->next)
355                 for (clump = pool->clumpchain;clump;clump = clump->chain)
356                         _Mem_CheckClumpSentinels(clump, filename, fileline);
357 #endif
358 }
359
360 qboolean Mem_IsAllocated(mempool_t *pool, void *data)
361 {
362         memheader_t *header;
363         memheader_t *target;
364
365         if (pool)
366         {
367                 // search only one pool
368                 target = (memheader_t *)((unsigned char *) data - sizeof(memheader_t));
369                 for( header = pool->chain ; header ; header = header->next )
370                         if( header == target )
371                                 return true;
372         }
373         else
374         {
375                 // search all pools
376                 for (pool = poolchain;pool;pool = pool->next)
377                         if (Mem_IsAllocated(pool, data))
378                                 return true;
379         }
380         return false;
381 }
382
383 void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray)
384 {
385         memset(l, 0, sizeof(*l));
386         l->mempool = mempool;
387         l->recordsize = recordsize;
388         l->numrecordsperarray = numrecordsperarray;
389 }
390
391 void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l)
392 {
393         size_t i;
394         if (l->maxarrays)
395         {
396                 for (i = 0;i != l->numarrays;i++)
397                         Mem_Free(l->arrays[i].data);
398                 Mem_Free(l->arrays);
399         }
400         memset(l, 0, sizeof(*l));
401 }
402
403 void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l)
404 {
405         size_t i, j;
406         for (i = 0;;i++)
407         {
408                 if (i == l->numarrays)
409                 {
410                         if (l->numarrays == l->maxarrays)
411                         {
412                                 memexpandablearray_array_t *oldarrays = l->arrays;
413                                 l->maxarrays = max(l->maxarrays * 2, 128);
414                                 l->arrays = Mem_Alloc(l->mempool, l->maxarrays * sizeof(*l->arrays));
415                                 if (oldarrays)
416                                 {
417                                         memcpy(l->arrays, oldarrays, l->numarrays * sizeof(*l->arrays));
418                                         Mem_Free(oldarrays);
419                                 }
420                         }
421                         l->arrays[i].numflaggedrecords = 0;
422                         l->arrays[i].data = Mem_Alloc(l->mempool, (l->recordsize + 1) * l->numrecordsperarray);
423                         l->arrays[i].allocflags = l->arrays[i].data + l->recordsize * l->numrecordsperarray;
424                         l->numarrays++;
425                 }
426                 if (l->arrays[i].numflaggedrecords < l->numrecordsperarray)
427                 {
428                         for (j = 0;j < l->numrecordsperarray;j++)
429                         {
430                                 if (!l->arrays[i].allocflags[j])
431                                 {
432                                         l->arrays[i].allocflags[j] = true;
433                                         l->arrays[i].numflaggedrecords++;
434                                         memset(l->arrays[i].data + l->recordsize * j, 0, l->recordsize);
435                                         return (void *)(l->arrays[i].data + l->recordsize * j);
436                                 }
437                         }
438                 }
439         }
440 }
441
442 void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record)
443 {
444         size_t i, j;
445         unsigned char *p = (unsigned char *)record;
446         for (i = 0;i != l->numarrays;i++)
447         {
448                 if (p >= l->arrays[i].data && p < (l->arrays[i].data + l->recordsize * l->numrecordsperarray))
449                 {
450                         j = (p - l->arrays[i].data) / l->recordsize;
451                         if (p != l->arrays[i].data + j * l->recordsize)
452                                 Sys_Error("Mem_ExpandableArray_FreeRecord: no such record %p\n", p);
453                         if (!l->arrays[i].allocflags[j])
454                                 Sys_Error("Mem_ExpandableArray_FreeRecord: record %p is already free!\n", p);
455                         l->arrays[i].allocflags[j] = false;
456                         l->arrays[i].numflaggedrecords--;
457                         return;
458                 }
459         }
460 }
461
462 size_t Mem_ExpandableArray_IndexRange(memexpandablearray_t *l)
463 {
464         size_t i, j, k;
465         if (!l->numarrays)
466                 return 0;
467         i = l->numarrays - 1;
468         for (j = 0, k = 0;k < l->arrays[i].numflaggedrecords;j++)
469                 if (l->arrays[i].allocflags[j])
470                         k++;
471         return l->numrecordsperarray * i + j;
472 }
473
474 void *Mem_ExpandableArray_RecordAtIndex(memexpandablearray_t *l, size_t index)
475 {
476         size_t i, j;
477         i = index / l->numrecordsperarray;
478         j = index % l->numrecordsperarray;
479         if (i >= l->numarrays || !l->arrays[i].allocflags[j])
480                 return NULL;
481         return (void *)(l->arrays[i].data + j * l->recordsize);
482 }
483
484
485 // used for temporary memory allocations around the engine, not for longterm
486 // storage, if anything in this pool stays allocated during gameplay, it is
487 // considered a leak
488 mempool_t *tempmempool;
489 // only for zone
490 mempool_t *zonemempool;
491
492 void Mem_PrintStats(void)
493 {
494         size_t count = 0, size = 0, realsize = 0;
495         mempool_t *pool;
496         memheader_t *mem;
497         Mem_CheckSentinelsGlobal();
498         for (pool = poolchain;pool;pool = pool->next)
499         {
500                 count++;
501                 size += pool->totalsize;
502                 realsize += pool->realsize;
503         }
504         Con_Printf("%lu memory pools, totalling %lu bytes (%.3fMB)\n", (unsigned long)count, (unsigned long)size, size / 1048576.0);
505         Con_Printf("total allocated size: %lu bytes (%.3fMB)\n", (unsigned long)realsize, realsize / 1048576.0);
506         for (pool = poolchain;pool;pool = pool->next)
507         {
508                 if ((pool->flags & POOLFLAG_TEMP) && pool->chain)
509                 {
510                         Con_Printf("Memory pool %p has sprung a leak totalling %lu bytes (%.3fMB)!  Listing contents...\n", pool, (unsigned long)pool->totalsize, pool->totalsize / 1048576.0);
511                         for (mem = pool->chain;mem;mem = mem->next)
512                                 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
513                 }
514         }
515 }
516
517 void Mem_PrintList(size_t minallocationsize)
518 {
519         mempool_t *pool;
520         memheader_t *mem;
521         Mem_CheckSentinelsGlobal();
522         Con_Print("memory pool list:\n"
523                    "size    name\n");
524         for (pool = poolchain;pool;pool = pool->next)
525         {
526                 Con_Printf("%10luk (%10luk actual) %s (%+li byte change) %s\n", (unsigned long) ((pool->totalsize + 1023) / 1024), (unsigned long)((pool->realsize + 1023) / 1024), pool->name, (long)pool->totalsize - pool->lastchecksize, (pool->flags & POOLFLAG_TEMP) ? "TEMP" : "");
527                 pool->lastchecksize = pool->totalsize;
528                 for (mem = pool->chain;mem;mem = mem->next)
529                         if (mem->size >= minallocationsize)
530                                 Con_Printf("%10lu bytes allocated at %s:%i\n", (unsigned long)mem->size, mem->filename, mem->fileline);
531         }
532 }
533
534 void MemList_f(void)
535 {
536         switch(Cmd_Argc())
537         {
538         case 1:
539                 Mem_PrintList(1<<30);
540                 Mem_PrintStats();
541                 break;
542         case 2:
543                 Mem_PrintList(atoi(Cmd_Argv(1)) * 1024);
544                 Mem_PrintStats();
545                 break;
546         default:
547                 Con_Print("MemList_f: unrecognized options\nusage: memlist [all]\n");
548                 break;
549         }
550 }
551
552 extern void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal);
553 void MemStats_f(void)
554 {
555         Mem_CheckSentinelsGlobal();
556         R_TextureStats_Print(false, false, true);
557         GL_Mesh_ListVBOs(false);
558         Mem_PrintStats();
559 }
560
561
562 char* Mem_strdup (mempool_t *pool, const char* s)
563 {
564         char* p;
565         size_t sz = strlen (s) + 1;
566         if (s == NULL) return NULL;
567         p = (char*)Mem_Alloc (pool, sz);
568         strlcpy (p, s, sz);
569         return p;
570 }
571
572 /*
573 ========================
574 Memory_Init
575 ========================
576 */
577 void Memory_Init (void)
578 {
579         poolchain = NULL;
580         tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL);
581         zonemempool = Mem_AllocPool("Zone", 0, NULL);
582 }
583
584 void Memory_Shutdown (void)
585 {
586 //      Mem_FreePool (&zonemempool);
587 //      Mem_FreePool (&tempmempool);
588 }
589
590 void Memory_Init_Commands (void)
591 {
592         Cmd_AddCommand ("memstats", MemStats_f, "prints memory system statistics");
593         Cmd_AddCommand ("memlist", MemList_f, "prints memory pool information (or if used as memlist 5 lists individual allocations of 5K or larger, 0 lists all allocations)");
594         Cvar_RegisterVariable (&developer_memory);
595         Cvar_RegisterVariable (&developer_memorydebug);
596 }
597