]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - zone.h
improving MEMCLUMP handling
[xonotic/darkplaces.git] / zone.h
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
21 #ifndef ZONE_H
22 #define ZONE_H
23
24 // LordHavoc: this is pointless with a good C library
25 //#define MEMCLUMPING
26
27 // div0: heap overflow detection paranoia
28 //#define MEMPARANOIA 1
29
30 #if MEMCLUMPING
31 // smallest unit we care about is this many bytes
32 #define MEMUNIT 64
33 #define MEMUNITSPERINT (MEMUNIT*32)
34 #define MEMUNITSPERBYTE (MEMUNIT*8)
35 // 1MB clumps
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)
41 #endif
42
43 #define POOLNAMESIZE 128
44 // if set this pool will be printed in memlist reports
45 #define POOLFLAG_TEMP 1
46
47 typedef struct memheader_s
48 {
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;
54 #if MEMCLUMPING
55         // clump this memheader lives in, NULL if not in a clump
56         struct memclump_s *clump;
57 #endif
58         // size of the memory after the header (excluding header and sentinel2)
59         size_t size;
60         // file name and line where Mem_Alloc was called
61         const char *filename;
62         int fileline;
63         // should always be MEMHEADER_SENTINEL1
64         unsigned int sentinel1;
65         // immediately followed by data, which is followed by a MEMHEADER_SENTINEL2 byte
66 }
67 memheader_t;
68
69 #if MEMCLUMPING
70 typedef struct memclump_s
71 {
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
78         int bits[MEMBITINTS];
79         // should always be MEMCLUMP_SENTINEL
80         unsigned int sentinel2;
81         // if this drops to 0, the clump is freed
82         size_t blocksinuse;
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;
88 }
89 memclump_t;
90 #endif
91
92 typedef struct mempool_s
93 {
94         // should always be MEMHEADER_SENTINEL1
95         unsigned int sentinel1;
96         // chain of individual memory allocations
97         struct memheader_s *chain;
98 #if MEMCLUMPING
99         // chain of clumps (if any)
100         struct memclump_s *clumpchain;
101 #endif
102         // POOLFLAG_*
103         int flags;
104         // total memory allocated in this pool (inside memheaders)
105         size_t totalsize;
106         // total memory allocated in this pool (actual malloc total)
107         size_t realsize;
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;
116         int fileline;
117         // name of the pool
118         char name[POOLNAMESIZE];
119         // should always be MEMHEADER_SENTINEL1
120         unsigned int sentinel2;
121 }
122 mempool_t;
123
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__)
131
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);
141
142 char* Mem_strdup (mempool_t *pool, const char* s);
143
144 typedef struct memexpandablearray_array_s
145 {
146         unsigned char *data;
147         unsigned char *allocflags;
148         size_t numflaggedrecords;
149 }
150 memexpandablearray_array_t;
151
152 typedef struct memexpandablearray_s
153 {
154         mempool_t *mempool;
155         size_t recordsize;
156         size_t numrecordsperarray;
157         size_t numarrays;
158         size_t maxarrays;
159         memexpandablearray_array_t *arrays;
160 }
161 memexpandablearray_t;
162
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;
169
170 // used for temporary allocations
171 extern mempool_t *tempmempool;
172
173 void Memory_Init (void);
174 void Memory_Shutdown (void);
175 void Memory_Init_Commands (void);
176
177 extern mempool_t *zonemempool;
178 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
179 #define Z_Free(data) Mem_Free(data)
180
181 extern struct cvar_s developer_memory;
182 extern struct cvar_s developer_memorydebug;
183
184 #endif
185