30#include <SDL_thread.h>
32#define MEM_MAX_POOLNAME 64
68#define MEM_HEAD_SENTINEL_TOP 0xFEBDFAED
69#define MEM_HEAD_SENTINEL_BOT 0xD0BAF0FF
70#define MEM_FOOT_SENTINEL 0xF00DF00D
74#define MEM_MAX_POOLCOUNT 32
107 Sys_Error(
"Mem_CreatePool: nullptr name %s:#%i", fileName, fileLine);
109 Com_Printf(
"Mem_CreatePoole: name '%s' too long, truncating!\n",
name);
124 Sys_Error(
"Mem_CreatePool: MEM_MAX_POOLCOUNT");
156 pool->
name[0] =
'\0';
187 Sys_Error(
"Mem_CheckSentinels: bad memory header top sentinel [buffer underflow]\n"
188 "free: %s:#%i", fileName, fileLine);
190 Sys_Error(
"Mem_CheckSentinels: bad memory header bottom sentinel [buffer underflow]\n"
191 "free: %s:#%i", fileName, fileLine);
193 Sys_Error(
"Mem_CheckSentinels: bad memory footer sentinel [buffer overflow]\n"
204void _Mem_Free (
void* ptr,
const char* fileName,
const int fileLine)
227 *prev = search->
next;
230 prev = &search->
next;
248 for (
int j = 0; j <
MEM_HASH; j++) {
251 if (mem->tagNum == tagNum)
267 for (
int j = 0; j <
MEM_HASH; j++) {
285 Sys_Error(
"Mem_Alloc: Error - no pool given\n" "alloc: %s:#%i", fileName, fileLine);
289 Sys_Error(
"Mem_Alloc: Attempted allocation of '" UFO_SIZE_T "' memory ignored\n" "alloc: %s:#%i",
size, fileName, fileLine);
291 if (
size > 0x40000000)
292 Sys_Error(
"Mem_Alloc: Attempted allocation of '" UFO_SIZE_T "' bytes!\n" "alloc: %s:#%i",
size, fileName, fileLine);
298 Sys_Error(
"Mem_Alloc: failed on allocation of '" UFO_SIZE_T "' bytes\n" "alloc: %s:#%i",
size, fileName, fileLine);
302 memset(mem, 0,
size);
357 memset((
byte*)newPtr + mem->
memSize, 0, delta);
391 assert(in !=
nullptr);
394 void* copy =
_Mem_Alloc(
size,
false, pool, tagNum, fileName, fileLine);
395 memcpy(copy, in,
size);
409 char* out = (
char*)
_Mem_Alloc((
size_t)(strlen(in) + 1),
true, pool, tagNum, fileName, fileLine);
431 uint32_t numChanged = 0;
433 for (
int j = 0; j <
MEM_HASH; j++) {
435 if (mem->tagNum == tagFrom) {
465 Sys_Error(
"Mem_CheckPoolIntegrity: bad block count\n" "check: %s:#%i", fileName, fileLine);
467 Sys_Error(
"Mem_CheckPoolIntegrity: bad pool size\n" "check: %s:#%i", fileName, fileLine);
494 for ( ; mem; mem = mem->
next) {
508 z_lock = SDL_CreateMutex();
510 Sys_Error(
"Mem_Init: Cannot create mutex for memory management: %s", SDL_GetError());
541static void Mem_Check_f (
void)
546static void Mem_Stats_f (
void)
548 uint32_t totalBytes,
i;
574 Com_Printf(
"----- ----- -------------------- ---------- \n");
576 uint32_t totalBytes = 0;
579 for (
i = 0, mem = best->
blocks[j]; mem; mem = mem->
next,
i++) {
591 Com_Printf(
"----------------------------------------\n");
592 Com_Printf(
"Total: %i blocks, %i bytes (%6.3fMB)\n",
i, totalBytes, totalBytes/1048576.0f);
598 Com_Printf(
"--- ------ ---------- ---------- --------\n");
600 uint32_t totalBlocks = 0;
602 uint32_t poolNum = 0;
617 Com_Printf(
"----------------------------------------\n");
618 Com_Printf(
"Total: %i pools, %i blocks, %i bytes (%6.3fMB)\n",
i, totalBlocks, totalBytes, totalBytes/1048576.0f);
625 {
"mem_stats", Mem_Stats_f,
"Prints out current internal memory statistics"},
626 {
"mem_check", Mem_Check_f,
"Checks global memory integrity"},
627 {
nullptr,
nullptr,
nullptr}
633void Mem_InitCallbacks (
void)
641void Mem_ShutdownCallbacks (
void)
void Cmd_TableAddList(const cmdList_t *cmdList)
const char * Cmd_Args(void)
Returns a single string containing argv(1) to argv(argc()-1).
int Cmd_Argc(void)
Return the number of arguments of the current command. "command parameter" will result in a argc of 2...
void Cmd_TableRemoveList(const cmdList_t *cmdList)
void Com_Printf(const char *const fmt,...)
definitions common between client and server, but not game lib
void Sys_Error(const char *error,...)
static void _Mem_CheckSentinels(memBlock_t *const mem, const char *fileName, const int fileLine)
void * _Mem_PoolDup(const void *in, size_t size, memPool_t *pool, const int tagNum, const char *fileName, const int fileLine)
static void * Mem_BlockToPtr(memBlock_t *const mem)
#define MEM_FOOT_SENTINEL
#define MEM_MAX_POOLCOUNT
static memPool_t * Mem_FindPool(const char *name)
void * _Mem_ReAlloc(void *ptr, size_t size, 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.
#define MEM_HEAD_SENTINEL_TOP
void _Mem_DeletePool(memPool_t *pool, const char *fileName, const int fileLine)
void _Mem_FreePool(memPool_t *pool, const char *fileName, const int fileLine)
Free all items within a pool.
static void _Mem_CheckPoolIntegrity(memPool_t *pool, const char *fileName, const int fileLine)
void _Mem_CheckGlobalIntegrity(const char *fileName, const int fileLine)
static uint32_t m_numPools
uint32_t _Mem_ChangeTag(memPool_t *pool, const int tagFrom, const int tagTo)
static size_t Mem_BlockRawSize(memBlock_t const *const mem)
#define MEM_HEAD_SENTINEL_BOT
static SDL_mutex * z_lock
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.
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.
memPool_t * _Mem_CreatePool(const char *name, const char *fileName, const int fileLine)
uint32_t _Mem_PoolSize(memPool_t *pool)
static memBlockFoot_t * Mem_BlockToFooter(memBlock_t *const mem)
static memBlock_t * Mem_PtrToBlock(void *const ptr)
void _Mem_Free(void *ptr, const char *fileName, const int fileLine)
static memPool_t m_poolList[MEM_MAX_POOLCOUNT]
bool _Mem_AllocatedInPool(memPool_t *pool, const void *pointer)
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.
#define Mem_DeletePool(pool)
#define Mem_CheckGlobalIntegrity()
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
QGL_EXTERN GLint GLenum GLboolean GLsizei const GLvoid * pointer
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
char name[MEM_MAX_POOLNAME]
memBlock_t * blocks[MEM_HASH]