]> icculus.org git repositories - divverent/darkplaces.git/blob - zone.h
My Key_Event is disabled (yet).
[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 // LordHavoc: this is pointless with a good C library
25 //#define MEMCLUMPING
26
27 #define POOLNAMESIZE 128
28 // if set this pool will be printed in memlist reports
29 #define POOLFLAG_TEMP 1
30 #if MEMCLUMPING
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
34 #define MEMUNIT 8
35 #define MEMBITS (MEMCLUMPSIZE / MEMUNIT)
36 #define MEMBITINTS (MEMBITS / 32)
37 #define MEMCLUMP_SENTINEL 0xABADCAFE
38 #endif
39
40 #define MEMHEADER_SENTINEL1 0xDEADF00D
41 #define MEMHEADER_SENTINEL2 0xDF
42
43 typedef struct memheader_s
44 {
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;
50 #if MEMCLUMPING
51         // clump this memheader lives in, NULL if not in a clump
52         struct memclump_s *clump;
53 #endif
54         // size of the memory after the header (excluding header and sentinel2)
55         int size;
56         // file name and line where Mem_Alloc was called
57         const char *filename;
58         int fileline;
59         // should always be MEMHEADER_SENTINEL1
60         unsigned int sentinel1;
61         // immediately followed by data, which is followed by a MEMHEADER_SENTINEL2 byte
62 }
63 memheader_t;
64
65 #if MEMCLUMPING
66 typedef struct memclump_s
67 {
68         // contents of the clump
69         qbyte 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
74         int bits[MEMBITINTS];
75         // should always be MEMCLUMP_SENTINEL
76         unsigned int sentinel2;
77         // if this drops to 0, the clump is freed
78         int blocksinuse;
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         int largestavailable;
82         // next clump in the chain
83         struct memclump_s *chain;
84 }
85 memclump_t;
86 #endif
87
88 typedef struct mempool_s
89 {
90         // should always be MEMHEADER_SENTINEL1
91         unsigned int sentinel1;
92         // chain of individual memory allocations
93         struct memheader_s *chain;
94 #if MEMCLUMPING
95         // chain of clumps (if any)
96         struct memclump_s *clumpchain;
97 #endif
98         // POOLFLAG_*
99         int flags;
100         // total memory allocated in this pool (inside memheaders)
101         int totalsize;
102         // total memory allocated in this pool (actual malloc total)
103         int realsize;
104         // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
105         int lastchecksize;
106         // name of the pool
107         char name[POOLNAMESIZE];
108         // linked into global mempool list
109         struct mempool_s *next;
110         // parent object (used for nested memory pools)
111         struct mempool_s *parent;
112         // file name and line where Mem_AllocPool was called
113         const char *filename;
114         int fileline;
115         // should always be MEMHEADER_SENTINEL1
116         unsigned int sentinel2;
117 }
118 mempool_t;
119
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__)
127
128 void *_Mem_Alloc(mempool_t *pool, int 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 qboolean Mem_IsAllocated(mempool_t *pool, void *data);
136
137 // used for temporary allocations
138 mempool_t *tempmempool;
139
140 void Memory_Init (void);
141 void Memory_Shutdown (void);
142 void Memory_Init_Commands (void);
143
144 extern mempool_t *zonemempool;
145 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
146 #define Z_Free(data) Mem_Free(data)
147
148 extern struct cvar_s developer_memory;
149 extern struct cvar_s developer_memorydebug;
150
151 #endif
152