00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _COMMON_MEM_H
00027 #define _COMMON_MEM_H
00028
00029 #define MEM_MAX_POOLNAME 64
00030 #define MEM_HASH 11
00031
00032 typedef struct memBlockFoot_s {
00033 uint32_t sentinel;
00034 } memBlockFoot_t;
00035
00036 typedef struct memBlock_s {
00037 struct memBlock_s *next;
00038
00039 uint32_t topSentinel;
00041 struct memPool_s *pool;
00042 int tagNum;
00043 size_t size;
00045 const char *allocFile;
00046 int allocLine;
00048 void *memPointer;
00049 size_t memSize;
00051 memBlockFoot_t *footer;
00053 uint32_t botSentinel;
00054 } memBlock_t;
00055
00056 typedef struct memPool_s {
00057 char name[MEM_MAX_POOLNAME];
00058 qboolean inUse;
00060 memBlock_t *blocks[MEM_HASH];
00062 uint32_t blockCount;
00063 uint32_t byteCount;
00065 const char *createFile;
00066 int createLine;
00067 } memPool_t;
00068
00069
00070 #define Mem_CreatePool(name) _Mem_CreatePool((name),__FILE__,__LINE__)
00071 #define Mem_DeletePool(pool) _Mem_DeletePool((pool),__FILE__,__LINE__)
00072
00073 #define Mem_Free(ptr) _Mem_Free((ptr),__FILE__,__LINE__)
00074 #define Mem_FreeTag(pool,tagNum) _Mem_FreeTag((pool),(tagNum),__FILE__,__LINE__)
00075 #define Mem_FreePool(pool) _Mem_FreePool((pool),__FILE__,__LINE__)
00076 #define Mem_Alloc(size) _Mem_Alloc((size),qtrue,com_genericPool,0,__FILE__,__LINE__)
00077 #define Mem_AllocExt(size,zeroFill) _Mem_Alloc((size),(zeroFill),com_genericPool,0,__FILE__,__LINE__)
00078 #define Mem_PoolAlloc(size,pool,tagNum) _Mem_Alloc((size),qtrue,(pool),(tagNum),__FILE__,__LINE__)
00079 #define Mem_PoolAllocExt(size,zeroFill,pool,tagNum) _Mem_Alloc((size),(zeroFill),(pool),(tagNum),__FILE__,__LINE__)
00080 #define Mem_ReAlloc(ptr,size) _Mem_ReAlloc((ptr),(size),__FILE__,__LINE__)
00081
00082 #define Mem_Dup(in,size) _Mem_PoolDup((in),(size),com_genericPool,0,__FILE__,__LINE__)
00083 #define Mem_StrDup(in) _Mem_PoolStrDup((in),com_genericPool,0,__FILE__,__LINE__)
00084 #define Mem_PoolStrDupTo(in,out,pool,tagNum) _Mem_PoolStrDupTo((in),(out),(pool),(tagNum),__FILE__,__LINE__)
00085 #define Mem_PoolStrDup(in,pool,tagNum) _Mem_PoolStrDup((in),(pool),(tagNum),__FILE__,__LINE__)
00086 #define Mem_PoolSize(pool) _Mem_PoolSize((pool))
00087 #define Mem_TagSize(pool,tagNum) _Mem_TagSize((pool),(tagNum))
00088 #define Mem_ChangeTag(pool,tagFrom,tagTo) _Mem_ChangeTag((pool),(tagFrom),(tagTo))
00089
00090 #define Mem_CheckPoolIntegrity(pool) _Mem_CheckPoolIntegrity((pool),__FILE__,__LINE__)
00091 #define Mem_CheckGlobalIntegrity() _Mem_CheckGlobalIntegrity(__FILE__,__LINE__)
00092
00093 #define Mem_TouchPool(pool) _Mem_TouchPool((pool),__FILE__,__LINE__)
00094 #define Mem_TouchGlobal() _Mem_TouchGlobal(__FILE__,__LINE__)
00095
00096
00097 memPool_t *_Mem_CreatePool(const char *name, const char *fileName, const int fileLine) __attribute__ ((malloc));
00098 uint32_t _Mem_DeletePool(memPool_t *pool, const char *fileName, const int fileLine);
00099
00100 uint32_t _Mem_Free(void *ptr, const char *fileName, const int fileLine);
00101 uint32_t _Mem_FreeTag(memPool_t *pool, const int tagNum, const char *fileName, const int fileLine);
00102 uint32_t _Mem_FreePool(memPool_t *pool, const char *fileName, const int fileLine);
00103 void* _Mem_Alloc(size_t size, qboolean zeroFill, memPool_t *pool, const int tagNum, const char *fileName, const int fileLine) __attribute__ ((malloc));
00104 void* _Mem_ReAlloc(void *ptr, size_t size, const char *fileName, const int fileLine);
00105
00106 size_t Mem_Size(const void *ptr);
00107 char* _Mem_PoolStrDupTo(const char *in, char **out, memPool_t *pool, const int tagNum, const char *fileName, const int fileLine);
00108 void *_Mem_PoolDup(const void *in, size_t size, memPool_t *pool, const int tagNum, const char *fileName, const int fileLine);
00109 char* _Mem_PoolStrDup(const char *in, memPool_t *pool, const int tagNum, const char *fileName, const int fileLine) __attribute__ ((malloc));
00110 uint32_t _Mem_PoolSize(memPool_t *pool);
00111 uint32_t _Mem_TagSize(memPool_t *pool, const int tagNum);
00112 uint32_t _Mem_ChangeTag(memPool_t *pool, const int tagFrom, const int tagTo);
00113
00114 void _Mem_CheckPoolIntegrity(memPool_t *pool, const char *fileName, const int fileLine);
00115 void _Mem_CheckGlobalIntegrity(const char *fileName, const int fileLine);
00116
00117 void _Mem_TouchPool(memPool_t *pool, const char *fileName, const int fileLine);
00118 void _Mem_TouchGlobal(const char *fileName, const int fileLine);
00119
00120 void* _Mem_AllocatedInPool(memPool_t *pool, const void *pointer);
00121
00122 void Mem_Init(void);
00123 uint32_t Mem_Shutdown(void);
00124
00125 #endif