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
28 //#define MEMPARANOIA 1
31 // smallest unit we care about is this many bytes
33 #define MEMUNITSPERINT (MEMUNIT*32)
34 #define MEMUNITSPERBYTE (MEMUNIT*8)
36 #define MEMWANTCLUMPSIZE 1048576
37 // give malloc padding so we can't waste most of a page at the end
38 #define MEMCLUMPSIZE (MEMWANTCLUMPSIZE - MEMWANTCLUMPSIZE/MEMUNITSPERBYTE - 128)
39 #define MEMBITS (MEMCLUMPSIZE / MEMUNIT)
40 #define MEMBITINTS (MEMBITS / 32)
43 #define POOLNAMESIZE 128
44 // if set this pool will be printed in memlist reports
45 #define POOLFLAG_TEMP 1
47 typedef struct memheader_s
49 // next and previous memheaders in chain belonging to pool
50 struct memheader_s *next;
51 struct memheader_s *prev;
52 // pool this memheader belongs to
53 struct mempool_s *pool;
55 // clump this memheader lives in, NULL if not in a clump
56 struct memclump_s *clump;
58 // size of the memory after the header (excluding header and sentinel2)
60 // file name and line where Mem_Alloc was called
63 // should always be MEMHEADER_SENTINEL1
64 unsigned int sentinel1;
65 // immediately followed by data, which is followed by a MEMHEADER_SENTINEL2 byte
70 typedef struct memclump_s
72 // contents of the clump
73 unsigned char block[MEMCLUMPSIZE];
74 // should always be MEMCLUMP_SENTINEL
75 unsigned int sentinel1;
76 // if a bit is on, it means that the MEMUNIT bytes it represents are
77 // allocated, otherwise free
79 // should always be MEMCLUMP_SENTINEL
80 unsigned int sentinel2;
81 // if this drops to 0, the clump is freed
83 // largest block of memory available (this is reset to an optimistic
84 // number when anything is freed, and updated when alloc fails the clump)
85 size_t largestavailable;
86 // next clump in the chain
87 struct memclump_s *chain;
92 typedef struct mempool_s
94 // should always be MEMHEADER_SENTINEL1
95 unsigned int sentinel1;
96 // chain of individual memory allocations
97 struct memheader_s *chain;
99 // chain of clumps (if any)
100 struct memclump_s *clumpchain;
104 // total memory allocated in this pool (inside memheaders)
106 // total memory allocated in this pool (actual malloc total)
108 // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
109 size_t lastchecksize;
110 // linked into global mempool list
111 struct mempool_s *next;
112 // parent object (used for nested memory pools)
113 struct mempool_s *parent;
114 // file name and line where Mem_AllocPool was called
115 const char *filename;
118 char name[POOLNAMESIZE];
119 // should always be MEMHEADER_SENTINEL1
120 unsigned int sentinel2;
124 #define Mem_Alloc(pool,size) _Mem_Alloc(pool, size, __FILE__, __LINE__)
125 #define Mem_Free(mem) _Mem_Free(mem, __FILE__, __LINE__)
126 #define Mem_CheckSentinels(data) _Mem_CheckSentinels(data, __FILE__, __LINE__)
127 #define Mem_CheckSentinelsGlobal() _Mem_CheckSentinelsGlobal(__FILE__, __LINE__)
128 #define Mem_AllocPool(name, flags, parent) _Mem_AllocPool(name, flags, parent, __FILE__, __LINE__)
129 #define Mem_FreePool(pool) _Mem_FreePool(pool, __FILE__, __LINE__)
130 #define Mem_EmptyPool(pool) _Mem_EmptyPool(pool, __FILE__, __LINE__)
132 void *_Mem_Alloc(mempool_t *pool, size_t size, const char *filename, int fileline);
133 void _Mem_Free(void *data, const char *filename, int fileline);
134 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline);
135 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline);
136 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline);
137 void _Mem_CheckSentinels(void *data, const char *filename, int fileline);
138 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline);
139 // if pool is NULL this searches ALL pools for the allocation
140 qboolean Mem_IsAllocated(mempool_t *pool, void *data);
142 char* Mem_strdup (mempool_t *pool, const char* s);
144 typedef struct memexpandablearray_array_s
147 unsigned char *allocflags;
148 size_t numflaggedrecords;
150 memexpandablearray_array_t;
152 typedef struct memexpandablearray_s
156 size_t numrecordsperarray;
159 memexpandablearray_array_t *arrays;
161 memexpandablearray_t;
163 void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray);
164 void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l);
165 void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l);
166 void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record);
167 size_t Mem_ExpandableArray_IndexRange(const memexpandablearray_t *l) DP_FUNC_PURE;
168 void *Mem_ExpandableArray_RecordAtIndex(const memexpandablearray_t *l, size_t index) DP_FUNC_PURE;
170 // used for temporary allocations
171 extern mempool_t *tempmempool;
173 void Memory_Init (void);
174 void Memory_Shutdown (void);
175 void Memory_Init_Commands (void);
177 extern mempool_t *zonemempool;
178 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
179 #define Z_Free(data) Mem_Free(data)
181 extern struct cvar_s developer_memory;
182 extern struct cvar_s developer_memorydebug;