1 /**
2  * @file
3  * @brief Memory handling with sentinel checking and pools with tags for grouped free'ing
4  */
5 
6 /*
7 Copyright (C) 1997-2001 Id Software, Inc.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 
24 */
25 
26 #pragma once
27 
28 struct memPool_t;
29 
30 /* constants */
31 #define Mem_CreatePool(name)							_Mem_CreatePool((name),__FILE__,__LINE__)
32 #define Mem_DeletePool(pool)							_Mem_DeletePool((pool),__FILE__,__LINE__)
33 
34 #define Mem_Free(ptr)									_Mem_Free((ptr),__FILE__,__LINE__)
35 #define Mem_FreeTag(pool,tagNum)						_Mem_FreeTag((pool),(tagNum),__FILE__,__LINE__)
36 #define Mem_FreePool(pool)								_Mem_FreePool((pool),__FILE__,__LINE__)
37 #define Mem_AllocTypeN(type, n)							static_cast<type*>(Mem_Alloc(sizeof(type) * (n)))
38 #define Mem_AllocType(type)								static_cast<type*>(Mem_Alloc(sizeof(type)))
39 #define Mem_Alloc(size)									Mem_PoolAlloc((size), com_genericPool, 0)
40 #define Mem_PoolAlloc(size,pool,tagNum)					_Mem_Alloc((size),true,(pool),(tagNum),__FILE__,__LINE__)
41 #define Mem_PoolAllocTypeN(type, n, pool)               static_cast<type*>(Mem_PoolAlloc(sizeof(type) * (n), (pool), 0))
42 #define Mem_PoolAllocType(type, pool)                   static_cast<type*>(Mem_PoolAllocTypeN(type, 1, (pool)))
43 #define Mem_ReAlloc(ptr,size)							_Mem_ReAlloc((ptr),(size),__FILE__,__LINE__)
44 #define Mem_SafeReAlloc(ptr, size)						((ptr) ? Mem_ReAlloc(ptr, size) : Mem_Alloc(size))
45 
46 #define Mem_Dup(type, in, n)							static_cast<type*>(_Mem_PoolDup(1 ? (in) : static_cast<type*>(0) /* type check */, sizeof(type) * (n), com_genericPool, 0, __FILE__, __LINE__))
47 #define Mem_StrDup(in)									_Mem_PoolStrDup((in),com_genericPool,0,__FILE__,__LINE__)
48 #define Mem_PoolStrDupTo(in,out,pool,tagNum)			_Mem_PoolStrDupTo((in),(out),(pool),(tagNum),__FILE__,__LINE__)
49 #define Mem_PoolStrDup(in,pool,tagNum)					_Mem_PoolStrDup((in),(pool),(tagNum),__FILE__,__LINE__)
50 #define Mem_PoolSize(pool)								_Mem_PoolSize((pool))
51 #define Mem_ChangeTag(pool,tagFrom,tagTo)				_Mem_ChangeTag((pool),(tagFrom),(tagTo))
52 
53 #define Mem_CheckGlobalIntegrity()						_Mem_CheckGlobalIntegrity(__FILE__,__LINE__)
54 
55 /* functions */
56 memPool_t* _Mem_CreatePool(const char* name, const char* fileName, const int fileLine) __attribute__ ((malloc));
57 void _Mem_DeletePool(memPool_t* pool, const char* fileName, const int fileLine);
58 
59 void _Mem_Free(void* ptr, const char* fileName, const int fileLine);
60 void _Mem_FreeTag(memPool_t* pool, const int tagNum, const char* fileName, const int fileLine);
61 void _Mem_FreePool(memPool_t* pool, const char* fileName, const int fileLine);
62 void* _Mem_Alloc(size_t size, bool zeroFill, memPool_t* pool, const int tagNum, const char* fileName, const int fileLine) __attribute__ ((malloc));
63 void* _Mem_ReAlloc(void* ptr, size_t size, const char* fileName, const int fileLine);
64 
65 char* _Mem_PoolStrDupTo(const char* in, char** out, memPool_t* pool, const int tagNum, const char* fileName, const int fileLine);
66 void* _Mem_PoolDup(const void* in, size_t size, memPool_t* pool, const int tagNum, const char* fileName, const int fileLine);
67 char* _Mem_PoolStrDup(const char* in, memPool_t* pool, const int tagNum, const char* fileName, const int fileLine) __attribute__ ((malloc));
68 uint32_t _Mem_PoolSize(memPool_t* pool);
69 uint32_t _Mem_ChangeTag(memPool_t* pool, const int tagFrom, const int tagTo);
70 
71 void _Mem_CheckGlobalIntegrity(const char* fileName, const int fileLine);
72 
73 bool _Mem_AllocatedInPool(memPool_t* pool, const void* pointer);
74 
75 void Mem_Init(void);
76 void Mem_Shutdown(void);
77