]> icculus.org git repositories - divverent/darkplaces.git/blob - zone.h
rewrote clump allocation system, added an experimental global clump
[divverent/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 // div0: heap overflow detection paranoia
25 #define MEMPARANOIA 0
26
27 #define POOLNAMESIZE 128
28 // if set this pool will be printed in memlist reports
29 #define POOLFLAG_TEMP 1
30
31 typedef struct memheader_s
32 {
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)
39         size_t size;
40         // file name and line where Mem_Alloc was called
41         const char *filename;
42         int fileline;
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[]
46 }
47 memheader_t;
48
49 typedef struct mempool_s
50 {
51         // should always be MEMPOOL_SENTINEL
52         unsigned int sentinel1;
53         // chain of individual memory allocations
54         struct memheader_s *chain;
55         // POOLFLAG_*
56         int flags;
57         // total memory allocated in this pool (inside memheaders)
58         size_t totalsize;
59         // total memory allocated in this pool (actual malloc total)
60         size_t realsize;
61         // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
62         size_t lastchecksize;
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
68         const char *filename;
69         int fileline;
70         // name of the pool
71         char name[POOLNAMESIZE];
72         // should always be MEMPOOL_SENTINEL
73         unsigned int sentinel2;
74 }
75 mempool_t;
76
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__)
84
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);
94
95 char* Mem_strdup (mempool_t *pool, const char* s);
96
97 typedef struct memexpandablearray_array_s
98 {
99         unsigned char *data;
100         unsigned char *allocflags;
101         size_t numflaggedrecords;
102 }
103 memexpandablearray_array_t;
104
105 typedef struct memexpandablearray_s
106 {
107         mempool_t *mempool;
108         size_t recordsize;
109         size_t numrecordsperarray;
110         size_t numarrays;
111         size_t maxarrays;
112         memexpandablearray_array_t *arrays;
113 }
114 memexpandablearray_t;
115
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;
122
123 // used for temporary allocations
124 extern mempool_t *tempmempool;
125
126 void Memory_Init (void);
127 void Memory_Shutdown (void);
128 void Memory_Init_Commands (void);
129
130 extern mempool_t *zonemempool;
131 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
132 #define Z_Free(data) Mem_Free(data)
133
134 extern struct cvar_s developer_memory;
135 extern struct cvar_s developer_memorydebug;
136
137 #endif
138