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 // LordHavoc: this is pointless with a good C library
27 // div0: heap overflow detection paranoia
31 // give malloc padding so we can't waste most of a page at the end
32 #define MEMCLUMPSIZE (65536 - 1536)
33 // smallest unit we care about is this many bytes
35 #define MEMBITS (MEMCLUMPSIZE / MEMUNIT)
36 #define MEMBITINTS (MEMBITS / 32)
39 #define POOLNAMESIZE 128
40 // if set this pool will be printed in memlist reports
41 #define POOLFLAG_TEMP 1
43 typedef struct memheader_s
45 // next and previous memheaders in chain belonging to pool
46 struct memheader_s *next;
47 struct memheader_s *prev;
48 // pool this memheader belongs to
49 struct mempool_s *pool;
51 // clump this memheader lives in, NULL if not in a clump
52 struct memclump_s *clump;
54 // size of the memory after the header (excluding header and sentinel2)
56 // file name and line where Mem_Alloc was called
59 // should always be MEMHEADER_SENTINEL1
60 unsigned int sentinel1;
61 // immediately followed by data, which is followed by a MEMHEADER_SENTINEL2 byte
66 typedef struct memclump_s
68 // contents of the clump
69 unsigned char block[MEMCLUMPSIZE];
70 // should always be MEMCLUMP_SENTINEL
71 unsigned int sentinel1;
72 // if a bit is on, it means that the MEMUNIT bytes it represents are
73 // allocated, otherwise free
75 // should always be MEMCLUMP_SENTINEL
76 unsigned int sentinel2;
77 // if this drops to 0, the clump is freed
79 // largest block of memory available (this is reset to an optimistic
80 // number when anything is freed, and updated when alloc fails the clump)
81 size_t largestavailable;
82 // next clump in the chain
83 struct memclump_s *chain;
88 typedef struct mempool_s
90 // should always be MEMHEADER_SENTINEL1
91 unsigned int sentinel1;
92 // chain of individual memory allocations
93 struct memheader_s *chain;
95 // chain of clumps (if any)
96 struct memclump_s *clumpchain;
100 // total memory allocated in this pool (inside memheaders)
102 // total memory allocated in this pool (actual malloc total)
104 // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
105 size_t lastchecksize;
106 // linked into global mempool list
107 struct mempool_s *next;
108 // parent object (used for nested memory pools)
109 struct mempool_s *parent;
110 // file name and line where Mem_AllocPool was called
111 const char *filename;
114 char name[POOLNAMESIZE];
115 // should always be MEMHEADER_SENTINEL1
116 unsigned int sentinel2;
120 #define Mem_Alloc(pool,size) _Mem_Alloc(pool, size, __FILE__, __LINE__)
121 #define Mem_Free(mem) _Mem_Free(mem, __FILE__, __LINE__)
122 #define Mem_CheckSentinels(data) _Mem_CheckSentinels(data, __FILE__, __LINE__)
123 #define Mem_CheckSentinelsGlobal() _Mem_CheckSentinelsGlobal(__FILE__, __LINE__)
124 #define Mem_AllocPool(name, flags, parent) _Mem_AllocPool(name, flags, parent, __FILE__, __LINE__)
125 #define Mem_FreePool(pool) _Mem_FreePool(pool, __FILE__, __LINE__)
126 #define Mem_EmptyPool(pool) _Mem_EmptyPool(pool, __FILE__, __LINE__)
128 void *_Mem_Alloc(mempool_t *pool, size_t size, const char *filename, int fileline);
129 void _Mem_Free(void *data, const char *filename, int fileline);
130 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline);
131 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline);
132 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline);
133 void _Mem_CheckSentinels(void *data, const char *filename, int fileline);
134 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline);
135 // if pool is NULL this searches ALL pools for the allocation
136 qboolean Mem_IsAllocated(mempool_t *pool, void *data);
138 char* Mem_strdup (mempool_t *pool, const char* s);
140 typedef struct memexpandablearray_array_s
143 unsigned char *allocflags;
144 size_t numflaggedrecords;
146 memexpandablearray_array_t;
148 typedef struct memexpandablearray_s
152 size_t numrecordsperarray;
155 memexpandablearray_array_t *arrays;
157 memexpandablearray_t;
159 void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray);
160 void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l);
161 void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l);
162 void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record);
163 size_t Mem_ExpandableArray_IndexRange(const memexpandablearray_t *l) DP_FUNC_PURE;
164 void *Mem_ExpandableArray_RecordAtIndex(const memexpandablearray_t *l, size_t index) DP_FUNC_PURE;
166 // used for temporary allocations
167 extern mempool_t *tempmempool;
169 void Memory_Init (void);
170 void Memory_Shutdown (void);
171 void Memory_Init_Commands (void);
173 extern mempool_t *zonemempool;
174 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
175 #define Z_Free(data) Mem_Free(data)
177 extern struct cvar_s developer_memory;
178 extern struct cvar_s developer_memorydebug;