UFO: Alien Invasion
Loading...
Searching...
No Matches
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>
Include dependency graph for mem.cpp:

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.
void _Mem_FreePool (memPool_t *pool, const char *fileName, const int fileLine)
 Free all items within a pool.
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.
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.
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.
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

◆ MEM_FOOT_SENTINEL

#define MEM_FOOT_SENTINEL   0xF00DF00D

Definition at line 70 of file mem.cpp.

Referenced by _Mem_Alloc(), and _Mem_CheckSentinels().

◆ MEM_HASH

◆ MEM_HEAD_SENTINEL_BOT

#define MEM_HEAD_SENTINEL_BOT   0xD0BAF0FF

Definition at line 69 of file mem.cpp.

Referenced by _Mem_Alloc(), and _Mem_CheckSentinels().

◆ MEM_HEAD_SENTINEL_TOP

#define MEM_HEAD_SENTINEL_TOP   0xFEBDFAED

Definition at line 68 of file mem.cpp.

Referenced by _Mem_Alloc(), and _Mem_CheckSentinels().

◆ MEM_MAX_POOLCOUNT

#define MEM_MAX_POOLCOUNT   32

Definition at line 74 of file mem.cpp.

Referenced by _Mem_CreatePool().

◆ MEM_MAX_POOLNAME

#define MEM_MAX_POOLNAME   64

Definition at line 32 of file mem.cpp.

Referenced by _Mem_CreatePool().

Function Documentation

◆ _Mem_Alloc()

◆ _Mem_AllocatedInPool()

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 486 of file mem.cpp.

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

Referenced by UI_FreeStringProperty(), and UI_ResetData().

◆ _Mem_ChangeTag()

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

Definition at line 426 of file mem.cpp.

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

◆ _Mem_CheckGlobalIntegrity()

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

Definition at line 470 of file mem.cpp.

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

◆ _Mem_CheckPoolIntegrity()

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

◆ _Mem_CheckSentinels()

◆ _Mem_CreatePool()

◆ _Mem_DeletePool()

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.

◆ _Mem_Free()

◆ _Mem_FreePool()

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 262 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().

◆ _Mem_FreeTag()

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 243 of file mem.cpp.

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

Referenced by SV_FreeTags().

◆ _Mem_PoolDup()

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

Definition at line 389 of file mem.cpp.

References _Mem_Alloc().

◆ _Mem_PoolSize()

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

Definition at line 418 of file mem.cpp.

References memPool_t::byteCount.

◆ _Mem_PoolStrDup()

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 407 of file mem.cpp.

References _Mem_Alloc().

Referenced by _Mem_PoolStrDupTo().

◆ _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 381 of file mem.cpp.

References _Mem_PoolStrDup().

◆ _Mem_ReAlloc()

void * _Mem_ReAlloc ( void * ptr,
size_t size,
const char * fileName,
const int fileLine )

◆ Mem_BlockRawSize()

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().

◆ Mem_BlockToFooter()

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().

◆ Mem_BlockToPtr()

void * Mem_BlockToPtr ( memBlock_t *const mem)
static

◆ Mem_FindPool()

memPool_t * Mem_FindPool ( const char * name)
static

Definition at line 83 of file mem.cpp.

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

Referenced by _Mem_CreatePool().

◆ Mem_Init()

void Mem_Init ( void )
See also
Qcommon_Init
Mem_Shutdown

Definition at line 506 of file mem.cpp.

References Sys_Error(), and z_lock.

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

◆ Mem_PtrToBlock()

memBlock_t * Mem_PtrToBlock ( void *const ptr)
static

Definition at line 163 of file mem.cpp.

Referenced by _Mem_Free(), and _Mem_ReAlloc().

◆ Mem_Shutdown()

Variable Documentation

◆ m_numPools

uint32_t m_numPools
static

Definition at line 77 of file mem.cpp.

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

◆ m_poolList

memPool_t m_poolList[MEM_MAX_POOLCOUNT]
static

Definition at line 76 of file mem.cpp.

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

◆ z_lock

SDL_mutex* z_lock
static

Definition at line 72 of file mem.cpp.

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