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