2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
24 // LordHavoc: this is pointless with a good C library
27 #define POOLNAMESIZE 128
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
33 #define MEMBITS (MEMCLUMPSIZE / MEMUNIT)
34 #define MEMBITINTS (MEMBITS / 32)
35 #define MEMCLUMP_SENTINEL 0xABADCAFE
38 #define MEMHEADER_SENTINEL1 0xDEADF00D
39 #define MEMHEADER_SENTINEL2 0xDF
41 typedef struct memheader_s
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;
49 // clump this memheader lives in, NULL if not in a clump
50 struct memclump_s *clump;
52 // size of the memory after the header (excluding header and sentinel2)
54 // file name and line where Mem_Alloc was called
57 // should always be MEMHEADER_SENTINEL1
58 unsigned int sentinel1;
59 // immediately followed by data, which is followed by a MEMHEADER_SENTINEL2 byte
64 typedef struct memclump_s
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
73 // should always be MEMCLUMP_SENTINEL
74 unsigned int sentinel2;
75 // if this drops to 0, the clump is freed
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)
80 // next clump in the chain
81 struct memclump_s *chain;
86 typedef struct mempool_s
88 // should always be MEMHEADER_SENTINEL1
89 unsigned int sentinel1;
90 // chain of individual memory allocations
91 struct memheader_s *chain;
93 // chain of clumps (if any)
94 struct memclump_s *clumpchain;
96 // total memory allocated in this pool (inside memheaders)
98 // total memory allocated in this pool (actual malloc total)
100 // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
103 char name[POOLNAMESIZE];
104 // linked into global mempool list
105 struct mempool_s *next;
106 // file name and line where Mem_AllocPool was called
107 const char *filename;
109 // should always be MEMHEADER_SENTINEL1
110 unsigned int sentinel2;
114 #define Mem_Alloc(pool,size) _Mem_Alloc(pool, size, __FILE__, __LINE__)
115 #define Mem_Free(mem) _Mem_Free(mem, __FILE__, __LINE__)
116 #define Mem_CheckSentinels(data) _Mem_CheckSentinels(data, __FILE__, __LINE__)
117 #define Mem_CheckSentinelsGlobal() _Mem_CheckSentinelsGlobal(__FILE__, __LINE__)
118 #define Mem_AllocPool(name) _Mem_AllocPool(name, __FILE__, __LINE__)
119 #define Mem_FreePool(pool) _Mem_FreePool(pool, __FILE__, __LINE__)
120 #define Mem_EmptyPool(pool) _Mem_EmptyPool(pool, __FILE__, __LINE__)
122 void *_Mem_Alloc(mempool_t *pool, int size, const char *filename, int fileline);
123 void _Mem_Free(void *data, const char *filename, int fileline);
124 mempool_t *_Mem_AllocPool(const char *name, const char *filename, int fileline);
125 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline);
126 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline);
127 void _Mem_CheckSentinels(void *data, const char *filename, int fileline);
128 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline);
130 // used for temporary allocations
131 mempool_t *tempmempool;
133 void Memory_Init (void);
134 void Memory_Init_Commands (void);
136 extern mempool_t *zonemempool;
137 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
138 #define Z_Free(data) Mem_Free(data)
140 extern struct cvar_s developer_memory;
141 extern struct cvar_s developer_memorydebug;