]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - zone.h
engine mostly converted to use R_MeshQueue functions instead of true transparent...
[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 #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 memheader in chain belonging to pool
44         struct memheader_s *chain;
45         // pool this memheader belongs to
46         struct mempool_s *pool;
47 #if MEMCLUMPING
48         // clump this memheader lives in, NULL if not in a clump
49         struct memclump_s *clump;
50 #endif
51         // size of the memory after the header (excluding header and sentinel2)
52         int size;
53         // file name and line where Mem_Alloc was called
54         char *filename;
55         int fileline;
56         // should always be MEMHEADER_SENTINEL1
57         int sentinel1;
58         // immediately followed by data, which is followed by a MEMHEADER_SENTINEL2 byte
59 }
60 memheader_t;
61
62 #if MEMCLUMPING
63 typedef struct memclump_s
64 {
65         // contents of the clump
66         qbyte block[MEMCLUMPSIZE];
67         // should always be MEMCLUMP_SENTINEL
68         int sentinel1;
69         // if a bit is on, it means that the MEMUNIT bytes it represents are
70         // allocated, otherwise free
71         int bits[MEMBITINTS];
72         // should always be MEMCLUMP_SENTINEL
73         int sentinel2;
74         // if this drops to 0, the clump is freed
75         int blocksinuse;
76         // largest block of memory available (this is reset to an optimistic
77         // number when anything is freed, and updated when alloc fails the clump)
78         int largestavailable;
79         // next clump in the chain
80         struct memclump_s *chain;
81 }
82 memclump_t;
83 #endif
84
85 typedef struct mempool_s
86 {
87         // chain of individual memory allocations
88         struct memheader_s *chain;
89 #if MEMCLUMPING
90         // chain of clumps (if any)
91         struct memclump_s *clumpchain;
92 #endif
93         // total memory allocated in this pool (inside memheaders)
94         int totalsize;
95         // total memory allocated in this pool (actual malloc total)
96         int realsize;
97         // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
98         int lastchecksize;
99         // name of the pool
100         char name[POOLNAMESIZE];
101         // linked into global mempool list
102         struct mempool_s *next;
103 }
104 mempool_t;
105
106 #define Mem_Alloc(pool,size) _Mem_Alloc(pool, size, __FILE__, __LINE__)
107 #define Mem_Free(mem) _Mem_Free(mem, __FILE__, __LINE__)
108 #define Mem_CheckSentinels(data) _Mem_CheckSentinels(data, __FILE__, __LINE__)
109 #define Mem_CheckSentinelsGlobal() _Mem_CheckSentinelsGlobal(__FILE__, __LINE__)
110 #define Mem_AllocPool(name) _Mem_AllocPool(name, __FILE__, __LINE__)
111 #define Mem_FreePool(pool) _Mem_FreePool(pool, __FILE__, __LINE__)
112 #define Mem_EmptyPool(pool) _Mem_EmptyPool(pool, __FILE__, __LINE__)
113
114 void *_Mem_Alloc(mempool_t *pool, int size, char *filename, int fileline);
115 void _Mem_Free(void *data, char *filename, int fileline);
116 mempool_t *_Mem_AllocPool(char *name, char *filename, int fileline);
117 void _Mem_FreePool(mempool_t **pool, char *filename, int fileline);
118 void _Mem_EmptyPool(mempool_t *pool, char *filename, int fileline);
119 void _Mem_CheckSentinels(void *data, char *filename, int fileline);
120 void _Mem_CheckSentinelsGlobal(char *filename, int fileline);
121
122 // used for temporary allocations
123 mempool_t *tempmempool;
124
125 void Memory_Init (void);
126 void Memory_Init_Commands (void);
127
128 extern mempool_t *zonemempool;
129 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
130 #define Z_Free(data) Mem_Free(data)
131
132 #endif
133