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
28 // if set this pool will be printed in memlist reports
29 #define POOLFLAG_TEMP 1
31 // give malloc padding so we can't waste most of a page at the end
32 #define MEMCLUMPSIZE (65536 - 1536)
33 // smallest unit we care about is this many bytes
35 #define MEMBITS (MEMCLUMPSIZE / MEMUNIT)
36 #define MEMBITINTS (MEMBITS / 32)
37 #define MEMCLUMP_SENTINEL 0xABADCAFE
40 #define MEMHEADER_SENTINEL1 0xDEADF00D
41 #define MEMHEADER_SENTINEL2 0xDF
43 typedef struct memheader_s
45 // next and previous memheaders in chain belonging to pool
46 struct memheader_s *next;
47 struct memheader_s *prev;
48 // pool this memheader belongs to
49 struct mempool_s *pool;
51 // clump this memheader lives in, NULL if not in a clump
52 struct memclump_s *clump;
54 // size of the memory after the header (excluding header and sentinel2)
56 // file name and line where Mem_Alloc was called
59 // should always be MEMHEADER_SENTINEL1
60 unsigned int sentinel1;
61 // immediately followed by data, which is followed by a MEMHEADER_SENTINEL2 byte
66 typedef struct memclump_s
68 // contents of the clump
69 qbyte block[MEMCLUMPSIZE];
70 // should always be MEMCLUMP_SENTINEL
71 unsigned int sentinel1;
72 // if a bit is on, it means that the MEMUNIT bytes it represents are
73 // allocated, otherwise free
75 // should always be MEMCLUMP_SENTINEL
76 unsigned int sentinel2;
77 // if this drops to 0, the clump is freed
79 // largest block of memory available (this is reset to an optimistic
80 // number when anything is freed, and updated when alloc fails the clump)
82 // next clump in the chain
83 struct memclump_s *chain;
88 typedef struct mempool_s
90 // should always be MEMHEADER_SENTINEL1
91 unsigned int sentinel1;
92 // chain of individual memory allocations
93 struct memheader_s *chain;
95 // chain of clumps (if any)
96 struct memclump_s *clumpchain;
100 // total memory allocated in this pool (inside memheaders)
102 // total memory allocated in this pool (actual malloc total)
104 // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
107 char name[POOLNAMESIZE];
108 // linked into global mempool list
109 struct mempool_s *next;
110 // parent object (used for nested memory pools)
111 struct mempool_s *parent;
112 // file name and line where Mem_AllocPool was called
113 const char *filename;
115 // should always be MEMHEADER_SENTINEL1
116 unsigned int sentinel2;
120 #define Mem_Alloc(pool,size) _Mem_Alloc(pool, size, __FILE__, __LINE__)
121 #define Mem_Free(mem) _Mem_Free(mem, __FILE__, __LINE__)
122 #define Mem_CheckSentinels(data) _Mem_CheckSentinels(data, __FILE__, __LINE__)
123 #define Mem_CheckSentinelsGlobal() _Mem_CheckSentinelsGlobal(__FILE__, __LINE__)
124 #define Mem_AllocPool(name, flags, parent) _Mem_AllocPool(name, flags, parent, __FILE__, __LINE__)
125 #define Mem_FreePool(pool) _Mem_FreePool(pool, __FILE__, __LINE__)
126 #define Mem_EmptyPool(pool) _Mem_EmptyPool(pool, __FILE__, __LINE__)
128 void *_Mem_Alloc(mempool_t *pool, int size, const char *filename, int fileline);
129 void _Mem_Free(void *data, const char *filename, int fileline);
130 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline);
131 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline);
132 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline);
133 void _Mem_CheckSentinels(void *data, const char *filename, int fileline);
134 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline);
135 qboolean Mem_IsAllocated(mempool_t *pool, void *data);
137 // used for temporary allocations
138 mempool_t *tempmempool;
140 void Memory_Init (void);
141 void Memory_Shutdown (void);
142 void Memory_Init_Commands (void);
144 extern mempool_t *zonemempool;
145 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
146 #define Z_Free(data) Mem_Free(data)
148 extern struct cvar_s developer_memory;
149 extern struct cvar_s developer_memorydebug;