UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mem.cpp File Reference

Memory handling with sentinel checking and pools with tags for grouped free'ing. More...

#include "common.h"
#include <SDL_thread.h>

Go to the source code of this file.

Data Structures

struct  memBlockFoot_t
 
struct  memBlock_t
 
struct  memPool_t
 

Macros

#define MEM_MAX_POOLNAME   64
 
#define MEM_HASH   11
 
#define MEM_HEAD_SENTINEL_TOP   0xFEBDFAED
 
#define MEM_HEAD_SENTINEL_BOT   0xD0BAF0FF
 
#define MEM_FOOT_SENTINEL   0xF00DF00D
 
#define MEM_MAX_POOLCOUNT   32
 

Functions

static memPool_tMem_FindPool (const char *name)
 
memPool_t_Mem_CreatePool (const char *name, const char *fileName, const int fileLine)
 
void _Mem_DeletePool (memPool_t *pool, const char *fileName, const int fileLine)
 
static memBlock_tMem_PtrToBlock (void *const ptr)
 
static voidMem_BlockToPtr (memBlock_t *const mem)
 
static memBlockFoot_tMem_BlockToFooter (memBlock_t *const mem)
 
static size_t Mem_BlockRawSize (memBlock_t const *const mem)
 
static void _Mem_CheckSentinels (memBlock_t *const mem, const char *fileName, const int fileLine)
 
void _Mem_Free (void *ptr, const char *fileName, const int fileLine)
 
void _Mem_FreeTag (memPool_t *pool, const int tagNum, const char *fileName, const int fileLine)
 Free memory blocks assigned to a specified tag within a pool. More...
 
void _Mem_FreePool (memPool_t *pool, const char *fileName, const int fileLine)
 Free all items within a pool. More...
 
void_Mem_Alloc (size_t size, bool zeroFill, memPool_t *pool, const int tagNum, const char *fileName, const int fileLine)
 Optionally returns 0 filled memory allocated in a pool with a tag. More...
 
void_Mem_ReAlloc (void *ptr, size_t size, const char *fileName, const int fileLine)
 
char * _Mem_PoolStrDupTo (const char *in, char **out, memPool_t *pool, const int tagNum, const char *fileName, const int fileLine)
 Saves a string to client hunk. More...
 
void_Mem_PoolDup (const void *in, size_t size, memPool_t *pool, const int tagNum, const char *fileName, const int fileLine)
 
char * _Mem_PoolStrDup (const char *in, memPool_t *pool, const int tagNum, const char *fileName, const int fileLine)
 No need to null terminate the extra spot because Mem_Alloc returns zero-filled memory. More...
 
uint32_t _Mem_PoolSize (memPool_t *pool)
 
uint32_t _Mem_ChangeTag (memPool_t *pool, const int tagFrom, const int tagTo)
 
static void _Mem_CheckPoolIntegrity (memPool_t *pool, const char *fileName, const int fileLine)
 
void _Mem_CheckGlobalIntegrity (const char *fileName, const int fileLine)
 
bool _Mem_AllocatedInPool (memPool_t *pool, const void *pointer)
 
void Mem_Init (void)
 
void Mem_Shutdown (void)
 

Variables

static SDL_mutex * z_lock
 
static memPool_t m_poolList [MEM_MAX_POOLCOUNT]
 
static uint32_t m_numPools
 

Detailed Description

Memory handling with sentinel checking and pools with tags for grouped free'ing.

Definition in file mem.cpp.

Macro Definition Documentation

#define MEM_FOOT_SENTINEL   0xF00DF00D

Definition at line 70 of file mem.cpp.

Referenced by _Mem_Alloc(), and _Mem_CheckSentinels().

#define MEM_HEAD_SENTINEL_BOT   0xD0BAF0FF

Definition at line 69 of file mem.cpp.

Referenced by _Mem_Alloc(), and _Mem_CheckSentinels().

#define MEM_HEAD_SENTINEL_TOP   0xFEBDFAED

Definition at line 68 of file mem.cpp.

Referenced by _Mem_Alloc(), and _Mem_CheckSentinels().

#define MEM_MAX_POOLCOUNT   32

Definition at line 74 of file mem.cpp.

Referenced by _Mem_CreatePool().

#define MEM_MAX_POOLNAME   64

Definition at line 32 of file mem.cpp.

Referenced by _Mem_CreatePool().

Function Documentation

bool _Mem_AllocatedInPool ( memPool_t pool,
const void pointer 
)

Searches a given pointer in all memory pool blocks

Parameters
poolThe pool to search the pointer in
pointerThe pointer to search in the pool

Definition at line 482 of file mem.cpp.

References memPool_t::blocks, Mem_BlockToPtr(), MEM_HASH, memBlock_t::next, and pointer.

Referenced by UI_FreeStringProperty(), and UI_ResetData().

uint32_t _Mem_ChangeTag ( memPool_t pool,
const int  tagFrom,
const int  tagTo 
)

Definition at line 422 of file mem.cpp.

References memPool_t::blocks, MEM_HASH, and memBlock_t::next.

void _Mem_CheckGlobalIntegrity ( const char *  fileName,
const int  fileLine 
)

Definition at line 466 of file mem.cpp.

References _Mem_CheckPoolIntegrity(), i, memPool_t::inUse, and m_numPools.

static void _Mem_CheckPoolIntegrity ( memPool_t pool,
const char *  fileName,
const int  fileLine 
)
static
static void _Mem_CheckSentinels ( memBlock_t *const  mem,
const char *  fileName,
const int  fileLine 
)
static
void _Mem_DeletePool ( memPool_t pool,
const char *  fileName,
const int  fileLine 
)
See also
_Mem_CreatePool
_Mem_FreePool

Definition at line 146 of file mem.cpp.

References _Mem_FreePool(), memPool_t::inUse, and memPool_t::name.

void _Mem_FreePool ( memPool_t pool,
const char *  fileName,
const int  fileLine 
)

Free all items within a pool.

See also
_Mem_CreatePool
_Mem_DeletePool

Definition at line 260 of file mem.cpp.

References _Mem_Free(), memPool_t::blockCount, memPool_t::blocks, memPool_t::byteCount, Mem_BlockToPtr(), and MEM_HASH.

Referenced by _Mem_DeletePool().

void _Mem_FreeTag ( memPool_t pool,
const int  tagNum,
const char *  fileName,
const int  fileLine 
)

Free memory blocks assigned to a specified tag within a pool.

Definition at line 241 of file mem.cpp.

References _Mem_Free(), memPool_t::blocks, Mem_BlockToPtr(), and MEM_HASH.

Referenced by SV_FreeTags().

void* _Mem_PoolDup ( const void in,
size_t  size,
memPool_t pool,
const int  tagNum,
const char *  fileName,
const int  fileLine 
)

Definition at line 385 of file mem.cpp.

References _Mem_Alloc().

uint32_t _Mem_PoolSize ( memPool_t pool)
Parameters
[in]poolThe pool to get the size from

Definition at line 414 of file mem.cpp.

References memPool_t::byteCount.

char* _Mem_PoolStrDup ( const char *  in,
memPool_t pool,
const int  tagNum,
const char *  fileName,
const int  fileLine 
)

No need to null terminate the extra spot because Mem_Alloc returns zero-filled memory.

Parameters
[in]inString to store in the given pool
[in]poolThe pool to allocate the memory in
[in]tagNum
[in]fileNameThe filename where this function was called from
[in]fileLineThe line where this function was called from

Definition at line 403 of file mem.cpp.

References _Mem_Alloc().

Referenced by _Mem_PoolStrDupTo().

char* _Mem_PoolStrDupTo ( const char *  in,
char **  out,
memPool_t pool,
const int  tagNum,
const char *  fileName,
const int  fileLine 
)

Saves a string to client hunk.

Parameters
[in]inString to store in the given pool
[out]outThe location where you want the pool pointer to be stored
[in]poolThe pool to allocate the memory in
[in]tagNum
[in]fileNameThe filename where this function was called from
[in]fileLineThe line where this function was called from

Definition at line 377 of file mem.cpp.

References _Mem_PoolStrDup().

void* _Mem_ReAlloc ( void ptr,
size_t  size,
const char *  fileName,
const int  fileLine 
)
static size_t Mem_BlockRawSize ( memBlock_t const *const  mem)
static

Definition at line 178 of file mem.cpp.

References memBlock_t::memSize.

Referenced by _Mem_CheckPoolIntegrity(), and _Mem_Free().

static memBlockFoot_t* Mem_BlockToFooter ( memBlock_t *const  mem)
static

Definition at line 173 of file mem.cpp.

References Mem_BlockToPtr(), and memBlock_t::memSize.

Referenced by _Mem_Alloc(), and _Mem_CheckSentinels().

static void* Mem_BlockToPtr ( memBlock_t *const  mem)
static
static memPool_t* Mem_FindPool ( const char *  name)
static

Definition at line 83 of file mem.cpp.

References i, memPool_t::inUse, m_numPools, memPool_t::name, and Q_streq.

Referenced by _Mem_CreatePool().

void Mem_Init ( void  )
See also
Qcommon_Init
Mem_Shutdown

Definition at line 588 of file mem.cpp.

References Cmd_AddCommand(), and z_lock.

Referenced by main(), Qcommon_Init(), and TEST_Init().

static memBlock_t* Mem_PtrToBlock ( void *const  ptr)
static

Definition at line 163 of file mem.cpp.

Referenced by _Mem_Free(), and _Mem_ReAlloc().

void Mem_Shutdown ( void  )

Variable Documentation

uint32_t m_numPools
static

Definition at line 77 of file mem.cpp.

Referenced by _Mem_CheckGlobalIntegrity(), _Mem_CreatePool(), Mem_FindPool(), and Mem_Shutdown().

memPool_t m_poolList[MEM_MAX_POOLCOUNT]
static

Definition at line 76 of file mem.cpp.

SDL_mutex* z_lock
static

Definition at line 72 of file mem.cpp.

Referenced by _Mem_Alloc(), _Mem_Free(), Mem_Init(), and Mem_Shutdown().