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_t * Mem_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_t * Mem_PtrToBlock (void *const ptr)
static void * Mem_BlockToPtr (memBlock_t *const mem)
static memBlockFoot_t * Mem_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 sizesize, 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 sizesize, 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 sizesize, 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 _Mem_AllocatedInPool(), memPool_t::blocks, Mem_BlockToPtr(), MEM_HASH, memBlock_t::next, and pointer.

Referenced by _Mem_AllocatedInPool(), 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 _Mem_ChangeTag(), memPool_t::blocks, MEM_HASH, and memBlock_t::next.

Referenced by _Mem_ChangeTag().

◆ _Mem_CheckGlobalIntegrity()

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

◆ _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_DeletePool(), _Mem_FreePool(), memPool_t::inUse, and memPool_t::name.

Referenced by _Mem_DeletePool().

◆ _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(), _Mem_FreePool(), memPool_t::blockCount, memPool_t::blocks, memPool_t::byteCount, Mem_BlockToPtr(), and MEM_HASH.

Referenced by _Mem_DeletePool(), and _Mem_FreePool().

◆ _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(), _Mem_FreeTag(), memPool_t::blocks, Mem_BlockToPtr(), and MEM_HASH.

Referenced by _Mem_FreeTag(), and 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(), and _Mem_PoolDup().

Referenced by _Mem_PoolDup().

◆ _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 _Mem_PoolSize(), and memPool_t::byteCount.

Referenced by _Mem_PoolSize().

◆ _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(), and _Mem_PoolStrDup().

Referenced by _Mem_PoolStrDup(), and _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(), and _Mem_PoolStrDupTo().

Referenced by _Mem_PoolStrDupTo().

◆ _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 Mem_BlockRawSize(), and memBlock_t::memSize.

Referenced by _Mem_CheckPoolIntegrity(), _Mem_Free(), and Mem_BlockRawSize().

◆ Mem_BlockToFooter()

memBlockFoot_t * Mem_BlockToFooter ( memBlock_t *const mem)
static

◆ 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, Mem_FindPool(), memPool_t::name, name, and Q_streq.

Referenced by _Mem_CreatePool(), and Mem_FindPool().

◆ Mem_Init()

void Mem_Init ( void )
See also
Qcommon_Init
Mem_Shutdown

Definition at line 506 of file mem.cpp.

References Mem_Init(), Sys_Error(), and z_lock.

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

◆ Mem_PtrToBlock()

memBlock_t * Mem_PtrToBlock ( void *const ptr)
static

Definition at line 163 of file mem.cpp.

References Mem_PtrToBlock().

Referenced by _Mem_Free(), _Mem_ReAlloc(), and Mem_PtrToBlock().

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