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