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 // div0: heap overflow detection paranoia
27 #define POOLNAMESIZE 128
28 // if set this pool will be printed in memlist reports
29 #define POOLFLAG_TEMP 1
31 typedef struct memheader_s
33 // address returned by Chunk_Alloc (may be significantly before this header to satisify alignment)
35 // next and previous memheaders in chain belonging to pool
36 struct memheader_s *next;
37 struct memheader_s *prev;
38 // pool this memheader belongs to
39 struct mempool_s *pool;
40 // size of the memory after the header (excluding header and sentinel2)
42 // file name and line where Mem_Alloc was called
45 // should always be equal to MEMHEADER_SENTINEL_FOR_ADDRESS()
46 unsigned int sentinel;
47 // immediately followed by data, which is followed by another copy of mem_sentinel[]
51 typedef struct mempool_s
53 // should always be MEMPOOL_SENTINEL
54 unsigned int sentinel1;
55 // chain of individual memory allocations
56 struct memheader_s *chain;
59 // total memory allocated in this pool (inside memheaders)
61 // total memory allocated in this pool (actual malloc total)
63 // updated each time the pool is displayed by memlist, shows change from previous time (unless pool was freed)
65 // linked into global mempool list
66 struct mempool_s *next;
67 // parent object (used for nested memory pools)
68 struct mempool_s *parent;
69 // file name and line where Mem_AllocPool was called
73 char name[POOLNAMESIZE];
74 // should always be MEMPOOL_SENTINEL
75 unsigned int sentinel2;
79 #define Mem_Alloc(pool,size) _Mem_Alloc(pool, NULL, size, 16, __FILE__, __LINE__)
80 #define Mem_Memalign(pool,alignment,size) _Mem_Alloc(pool, NULL, size, alignment, __FILE__, __LINE__)
81 #define Mem_Realloc(pool,data,size) _Mem_Alloc(pool, data, size, 16, __FILE__, __LINE__)
82 #define Mem_Free(mem) _Mem_Free(mem, __FILE__, __LINE__)
83 #define Mem_CheckSentinels(data) _Mem_CheckSentinels(data, __FILE__, __LINE__)
84 #define Mem_CheckSentinelsGlobal() _Mem_CheckSentinelsGlobal(__FILE__, __LINE__)
85 #define Mem_AllocPool(name, flags, parent) _Mem_AllocPool(name, flags, parent, __FILE__, __LINE__)
86 #define Mem_FreePool(pool) _Mem_FreePool(pool, __FILE__, __LINE__)
87 #define Mem_EmptyPool(pool) _Mem_EmptyPool(pool, __FILE__, __LINE__)
89 void *_Mem_Alloc(mempool_t *pool, void *data, size_t size, size_t alignment, const char *filename, int fileline);
90 void _Mem_Free(void *data, const char *filename, int fileline);
91 mempool_t *_Mem_AllocPool(const char *name, int flags, mempool_t *parent, const char *filename, int fileline);
92 void _Mem_FreePool(mempool_t **pool, const char *filename, int fileline);
93 void _Mem_EmptyPool(mempool_t *pool, const char *filename, int fileline);
94 void _Mem_CheckSentinels(void *data, const char *filename, int fileline);
95 void _Mem_CheckSentinelsGlobal(const char *filename, int fileline);
96 // if pool is NULL this searches ALL pools for the allocation
97 qboolean Mem_IsAllocated(mempool_t *pool, void *data);
99 char* Mem_strdup (mempool_t *pool, const char* s);
101 typedef struct memexpandablearray_array_s
104 unsigned char *allocflags;
105 size_t numflaggedrecords;
107 memexpandablearray_array_t;
109 typedef struct memexpandablearray_s
113 size_t numrecordsperarray;
116 memexpandablearray_array_t *arrays;
118 memexpandablearray_t;
120 void Mem_ExpandableArray_NewArray(memexpandablearray_t *l, mempool_t *mempool, size_t recordsize, int numrecordsperarray);
121 void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l);
122 void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l);
123 void *Mem_ExpandableArray_AllocRecord_Id(memexpandablearray_t *l, size_t *out_id);
124 void Mem_ExpandableArray_FreeRecord(memexpandablearray_t *l, void *record);
125 size_t Mem_ExpandableArray_IndexRange(const memexpandablearray_t *l) DP_FUNC_PURE;
126 void *Mem_ExpandableArray_RecordAtIndex(const memexpandablearray_t *l, size_t index) DP_FUNC_PURE;
128 // used for temporary allocations
129 extern mempool_t *tempmempool;
131 void Memory_Init (void);
132 void Memory_Shutdown (void);
133 void Memory_Init_Commands (void);
135 extern mempool_t *zonemempool;
136 #define Z_Malloc(size) Mem_Alloc(zonemempool,size)
137 #define Z_Free(data) Mem_Free(data)
139 extern struct cvar_s developer_memory;
140 extern struct cvar_s developer_memorydebug;