]> icculus.org git repositories - divverent/darkplaces.git/blob - zone.h
Fixed a bug in the Ogg Vorbis streaming code that can cause sound files to be unplaya...
[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 MEMCLUMPING
29 // give malloc padding so we can't waste most of a page at the end
30 #define MEMCLUMPSIZE (65536 - 1536)
31 // smallest unit we care about is this many bytes
32 #define MEMUNIT 8
33 #define MEMBITS (MEMCLUMPSIZE / MEMUNIT)
34 #define MEMBITINTS (MEMBITS / 32)
35 #define MEMCLUMP_SENTINEL 0xABADCAFE
36 #endif
37
38 #define MEMHEADER_SENTINEL1 0xDEADF00D
39 #define MEMHEADER_SENTINEL2 0xDF
40
41 typedef struct memheader_s
42 {
43         // next and previous memheaders in chain belonging to pool
44         struct memheader_s *next;
45         struct memheader_s *prev;
46         // pool this memheader belongs to
47         struct mempool_s *pool;
48 #if MEMCLUMPING
49         // clump this memheader lives in, NULL if not in a clump
50         struct memclump_s *clump;
51 #endif
52         // size of the memory after the header (excluding header and sentinel2)
53         int size;
54         // file name and line where Mem_Alloc was called
55         const char *filename;
56         int fileline;
57         // should always be MEMHEADER_SENTINEL1
58         unsigned int sentinel1;
59         // immediately followed by data, which is followed by a MEMHEADER_SENTINEL2 byte
60 }
61 memheader_t;
62
63 #if MEMCLUMPING
64 typedef struct memclump_s
65 {
66         // contents of the clump
67         qbyte block[MEMCLUMPSIZE];
68         // should always be MEMCLUMP_SENTINEL
69         unsigned int sentinel1;
70         // if a bit is on, it means that the MEMUNIT bytes it represents are
71         // allocated, otherwise free
72         int bits[MEMBITINTS];
73         // should always be MEMCLUMP_SENTINEL
74         unsigned int sentinel2;
75         // if this drops to 0, the clump is freed
76         int blocksinuse;
77         // largest block of memory available (this is reset to an optimistic
78         // number when anything is freed, and updated when alloc fails the clump)
79         int largestavailable;
80         // next clump in the chain
81         struct memclump_s *chain;
82 }
83 memclump_t;
84 #endif
85
86 typedef struct mempool_s
87 {
88         // should always be MEMHEADER_SENTINEL1
89         unsigned int sentinel1;
90         // chain of individual memory allocations
91         struct memheader_s *chain;
92 #if MEMCLUMPING
93         // chain of clumps (if any)
94         struct memclump_s *clumpchain;
95 #endif
96         // total memory allocated in this pool (inside memheaders)
97         int totalsize;
98         // total memory allocated in this pool (actual malloc total)
99         int realsize;
100         // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
101         int lastchecksize;
102         // name of the pool
103         char name[POOLNAMESIZE];
104         // linked into global mempool list
105         struct mempool_s *next;
106         // parent object (used for nested memory pools)
107         struct mempool_s *parent;
108         // file name and line where Mem_AllocPool was called
109         const char *filename;
110         int fileline;
111         // should always be MEMHEADER_SENTINEL1
112         unsigned int sentinel2;
113 }
114 mempool_t;
115
116 #define Mem_Alloc(pool,size) _Mem_Alloc(pool, size, __FILE__, __LINE__)
117 #define Mem_Free(mem) _Mem_Free(mem, __FILE__, __LINE__)
118 #define Mem_CheckSentinels(data) _Mem_CheckSentinels(data, __FILE__, __LINE__)
119 #define Mem_CheckSentinelsGlobal() _Mem_CheckSentinelsGlobal(__FILE__, __LINE__)
120 #define Mem_AllocPool(name) _Mem_AllocPool(name, NULL, __FILE__, __LINE__)
121 #define Mem_AllocNestedPool(name, parent) _Mem_AllocPool(name, (parent), __FILE__, __LINE__)
122 #define Mem_FreePool(pool) _Mem_FreePool(pool, __FILE__, __LINE__)
123 #define Mem_EmptyPool(pool) _Mem_EmptyPool(pool, __FILE__, __LINE__)
124
125 void *_Mem_Alloc(mempool_t *pool, int size, const char *filename, int fileline);
126 void _Mem_Free(void *data, const char *filename, int fileline);
127 mempool_t *_Mem_AllocPool(const char *name, mempool_t *parent, const char *filename, int fileline);
128 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline);
129 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline);
130 void _Mem_CheckSentinels(void *data, const char *filename, int fileline);
131 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline);
132
133 // used for temporary allocations
134 mempool_t *tempmempool;
135
136 void Memory_Init (void);
137 void Memory_Init_Commands (void);
138
139 extern mempool_t *zonemempool;
140 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
141 #define Z_Free(data) Mem_Free(data)
142
143 extern struct cvar_s developer_memory;
144 extern struct cvar_s developer_memorydebug;
145
146 #endif
147