2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
24 // div0: heap overflow detection paranoia
27 #define POOLNAMESIZE 128
28 // if set this pool will be printed in memlist reports
29 #define POOLFLAG_TEMP 1
31 typedef struct memheader_s
33 // next and previous memheaders in chain belonging to pool
34 struct memheader_s *next;
35 struct memheader_s *prev;
36 // pool this memheader belongs to
37 struct mempool_s *pool;
38 // size of the memory after the header (excluding header and sentinel2)
40 // file name and line where Mem_Alloc was called
43 // should always be equal to MEMHEADER_SENTINEL_FOR_ADDRESS()
44 unsigned int sentinel;
45 // immediately followed by data, which is followed by another copy of mem_sentinel[]
49 typedef struct mempool_s
51 // should always be MEMPOOL_SENTINEL
52 unsigned int sentinel1;
53 // chain of individual memory allocations
54 struct memheader_s *chain;
57 // total memory allocated in this pool (inside memheaders)
59 // total memory allocated in this pool (actual malloc total)
61 // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
63 // linked into global mempool list
64 struct mempool_s *next;
65 // parent object (used for nested memory pools)
66 struct mempool_s *parent;
67 // file name and line where Mem_AllocPool was called
71 char name[POOLNAMESIZE];
72 // should always be MEMPOOL_SENTINEL
73 unsigned int sentinel2;
77 #define Mem_Alloc(pool,size) _Mem_Alloc(pool, size, __FILE__, __LINE__)
78 #define Mem_Free(mem) _Mem_Free(mem, __FILE__, __LINE__)
79 #define Mem_CheckSentinels(data) _Mem_CheckSentinels(data, __FILE__, __LINE__)
80 #define Mem_CheckSentinelsGlobal() _Mem_CheckSentinelsGlobal(__FILE__, __LINE__)
81 #define Mem_AllocPool(name, flags, parent) _Mem_AllocPool(name, flags, parent, __FILE__, __LINE__)
82 #define Mem_FreePool(pool) _Mem_FreePool(pool, __FILE__, __LINE__)
83 #define Mem_EmptyPool(pool) _Mem_EmptyPool(pool, __FILE__, __LINE__)
85 void *_Mem_Alloc(mempool_t *pool, size_t size, const char *filename, int fileline);
86 void _Mem_Free(void *data, const char *filename, int fileline);
87 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline);
88 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline);
89 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline);
90 void _Mem_CheckSentinels(void *data, const char *filename, int fileline);
91 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline);
92 // if pool is NULL this searches ALL pools for the allocation
93 qboolean Mem_IsAllocated(mempool_t *pool, void *data);
95 char* Mem_strdup (mempool_t *pool, const char* s);
97 typedef struct memexpandablearray_array_s
100 unsigned char *allocflags;
101 size_t numflaggedrecords;
103 memexpandablearray_array_t;
105 typedef struct memexpandablearray_s
109 size_t numrecordsperarray;
112 memexpandablearray_array_t *arrays;
114 memexpandablearray_t;
116 void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray);
117 void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l);
118 void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l);
119 void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record);
120 size_t Mem_ExpandableArray_IndexRange(const memexpandablearray_t *l) DP_FUNC_PURE;
121 void *Mem_ExpandableArray_RecordAtIndex(const memexpandablearray_t *l, size_t index) DP_FUNC_PURE;
123 // used for temporary allocations
124 extern mempool_t *tempmempool;
126 void Memory_Init (void);
127 void Memory_Shutdown (void);
128 void Memory_Init_Commands (void);
130 extern mempool_t *zonemempool;
131 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
132 #define Z_Free(data) Mem_Free(data)
134 extern struct cvar_s developer_memory;
135 extern struct cvar_s developer_memorydebug;