]> icculus.org git repositories - divverent/darkplaces.git/blob - zone.h
fixed cl_decals_models 1 rendering
[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         // address returned by Chunk_Alloc (may be significantly before this header to satisify alignment)
34         void *baseaddress;
35         // next and previous memheaders in chain belonging to pool
36         struct memheader_s *next;
37         struct memheader_s *prev;
38         // pool this memheader belongs to
39         struct mempool_s *pool;
40         // size of the memory after the header (excluding header and sentinel2)
41         size_t size;
42         // file name and line where Mem_Alloc was called
43         const char *filename;
44         int fileline;
45         // should always be equal to MEMHEADER_SENTINEL_FOR_ADDRESS()
46         unsigned int sentinel;
47         // immediately followed by data, which is followed by another copy of mem_sentinel[]
48 }
49 memheader_t;
50
51 typedef struct mempool_s
52 {
53         // should always be MEMPOOL_SENTINEL
54         unsigned int sentinel1;
55         // chain of individual memory allocations
56         struct memheader_s *chain;
57         // POOLFLAG_*
58         int flags;
59         // total memory allocated in this pool (inside memheaders)
60         size_t totalsize;
61         // total memory allocated in this pool (actual malloc total)
62         size_t realsize;
63         // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
64         size_t lastchecksize;
65         // linked into global mempool list
66         struct mempool_s *next;
67         // parent object (used for nested memory pools)
68         struct mempool_s *parent;
69         // file name and line where Mem_AllocPool was called
70         const char *filename;
71         int fileline;
72         // name of the pool
73         char name[POOLNAMESIZE];
74         // should always be MEMPOOL_SENTINEL
75         unsigned int sentinel2;
76 }
77 mempool_t;
78
79 #define Mem_Alloc(pool,size) _Mem_Alloc(pool, NULL, size, 16, __FILE__, __LINE__)
80 #define Mem_Memalign(pool,alignment,size) _Mem_Alloc(pool, NULL, size, alignment, __FILE__, __LINE__)
81 #define Mem_Realloc(pool,data,size) _Mem_Alloc(pool, data, size, 16, __FILE__, __LINE__)
82 #define Mem_Free(mem) _Mem_Free(mem, __FILE__, __LINE__)
83 #define Mem_CheckSentinels(data) _Mem_CheckSentinels(data, __FILE__, __LINE__)
84 #define Mem_CheckSentinelsGlobal() _Mem_CheckSentinelsGlobal(__FILE__, __LINE__)
85 #define Mem_AllocPool(name, flags, parent) _Mem_AllocPool(name, flags, parent, __FILE__, __LINE__)
86 #define Mem_FreePool(pool) _Mem_FreePool(pool, __FILE__, __LINE__)
87 #define Mem_EmptyPool(pool) _Mem_EmptyPool(pool, __FILE__, __LINE__)
88
89 void *_Mem_Alloc(mempool_t *pool, void *data, size_t size, size_t alignment, const char *filename, int fileline);
90 void _Mem_Free(void *data, const char *filename, int fileline);
91 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline);
92 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline);
93 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline);
94 void _Mem_CheckSentinels(void *data, const char *filename, int fileline);
95 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline);
96 // if pool is NULL this searches ALL pools for the allocation
97 qboolean Mem_IsAllocated(mempool_t *pool, void *data);
98
99 char* Mem_strdup (mempool_t *pool, const char* s);
100
101 typedef struct memexpandablearray_array_s
102 {
103         unsigned char *data;
104         unsigned char *allocflags;
105         size_t numflaggedrecords;
106 }
107 memexpandablearray_array_t;
108
109 typedef struct memexpandablearray_s
110 {
111         mempool_t *mempool;
112         size_t recordsize;
113         size_t numrecordsperarray;
114         size_t numarrays;
115         size_t maxarrays;
116         memexpandablearray_array_t *arrays;
117 }
118 memexpandablearray_t;
119
120 void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray);
121 void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l);
122 void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l);
123 void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record);
124 size_t Mem_ExpandableArray_IndexRange(const memexpandablearray_t *l) DP_FUNC_PURE;
125 void *Mem_ExpandableArray_RecordAtIndex(const memexpandablearray_t *l, size_t index) DP_FUNC_PURE;
126
127 // used for temporary allocations
128 extern mempool_t *tempmempool;
129
130 void Memory_Init (void);
131 void Memory_Shutdown (void);
132 void Memory_Init_Commands (void);
133
134 extern mempool_t *zonemempool;
135 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
136 #define Z_Free(data) Mem_Free(data)
137
138 extern struct cvar_s developer_memory;
139 extern struct cvar_s developer_memorydebug;
140
141 #endif
142