Quake II RTX doxygen  1.0 dev
zone.c File Reference
#include "shared/shared.h"
#include "common/common.h"
#include "common/zone.h"

Go to the source code of this file.

Classes

struct  zhead_s
 
struct  zstatic_t
 
struct  zstats_t
 

Macros

#define Z_MAGIC   0x1d0d
 
#define Z_TAIL   0x5b7b
 
#define Z_TAIL_F(z)   *(uint16_t *)((byte *)(z) + (z)->size - sizeof(uint16_t))
 
#define Z_FOR_EACH(z)   for ((z) = z_chain.next; (z) != &z_chain; (z) = (z)->next)
 
#define Z_FOR_EACH_SAFE(z, n)   for ((z) = z_chain.next; (z) != &z_chain; (z) = (n))
 
#define Z_EXTRA   (sizeof(zhead_t) + sizeof(uint16_t))
 
#define Z_STATIC(x)   { { Z_MAGIC, TAG_STATIC, q_offsetof(zstatic_t, tail) + sizeof(uint16_t) }, x, Z_TAIL }
 

Typedefs

typedef struct zhead_s zhead_t
 

Functions

static void Z_Validate (zhead_t *z, const char *func)
 
void Z_Check (void)
 
void Z_LeakTest (memtag_t tag)
 
void Z_Free (void *ptr)
 
voidZ_Realloc (void *ptr, size_t size)
 
void Z_Stats_f (void)
 
void Z_FreeTags (memtag_t tag)
 
voidZ_TagMalloc (size_t size, memtag_t tag)
 
voidZ_TagMallocz (size_t size, memtag_t tag)
 
void Z_TagReserve (size_t size, memtag_t tag)
 
voidZ_ReservedAlloc (size_t size)
 
voidZ_ReservedAllocz (size_t size)
 
char * Z_ReservedCopyString (const char *in)
 
void Z_Init (void)
 
char * Z_TagCopyString (const char *in, memtag_t tag)
 
char * Z_CvarCopyString (const char *in)
 

Variables

static zhead_t z_chain
 
static const zstatic_t z_static []
 
static zstats_t z_stats [TAG_MAX]
 
static const char z_tagnames [TAG_MAX][8]
 
static byte * z_reserved_data
 
static size_t z_reserved_inuse
 
static size_t z_reserved_total
 

Macro Definition Documentation

◆ Z_EXTRA

#define Z_EXTRA   (sizeof(zhead_t) + sizeof(uint16_t))

Definition at line 47 of file zone.c.

◆ Z_FOR_EACH

#define Z_FOR_EACH (   z)    for ((z) = z_chain.next; (z) != &z_chain; (z) = (z)->next)

Definition at line 29 of file zone.c.

◆ Z_FOR_EACH_SAFE

#define Z_FOR_EACH_SAFE (   z,
 
)    for ((z) = z_chain.next; (z) != &z_chain; (z) = (n))

Definition at line 32 of file zone.c.

◆ Z_MAGIC

#define Z_MAGIC   0x1d0d

Definition at line 23 of file zone.c.

◆ Z_STATIC

#define Z_STATIC (   x)    { { Z_MAGIC, TAG_STATIC, q_offsetof(zstatic_t, tail) + sizeof(uint16_t) }, x, Z_TAIL }

◆ Z_TAIL

#define Z_TAIL   0x5b7b

Definition at line 24 of file zone.c.

◆ Z_TAIL_F

#define Z_TAIL_F (   z)    *(uint16_t *)((byte *)(z) + (z)->size - sizeof(uint16_t))

Definition at line 26 of file zone.c.

Typedef Documentation

◆ zhead_t

typedef struct zhead_s zhead_t

Function Documentation

◆ Z_Check()

void Z_Check ( void  )

Definition at line 111 of file zone.c.

112 {
113  zhead_t *z;
114 
115  Z_FOR_EACH(z) {
116  Z_Validate(z, __func__);
117  }
118 }

◆ Z_CvarCopyString()

char* Z_CvarCopyString ( const char *  in)

Definition at line 419 of file zone.c.

420 {
421  size_t len;
422  zstatic_t *z;
423  zstats_t *s;
424  int i;
425 
426  if (!in) {
427  return NULL;
428  }
429 
430  if (!in[0]) {
431  i = 10;
432  } else if (!in[1] && Q_isdigit(in[0])) {
433  i = in[0] - '0';
434  } else {
435  len = strlen(in) + 1;
436  return memcpy(Z_TagMalloc(len, TAG_CVAR), in, len);
437  }
438 
439  // return static storage
440  z = (zstatic_t *)&z_static[i];
441  s = &z_stats[TAG_STATIC];
442  s->count++;
443  s->bytes += z->z.size;
444  return z->data;
445 }

Referenced by change_string_value(), Cvar_Get(), Cvar_SetByVar(), and get_engine_cvar().

◆ Z_Free()

void Z_Free ( void ptr)

Definition at line 147 of file zone.c.

148 {
149  zhead_t *z;
150  zstats_t *s;
151 
152  if (!ptr) {
153  return;
154  }
155 
156  z = (zhead_t *)ptr - 1;
157 
158  Z_Validate(z, __func__);
159 
160  s = &z_stats[z->tag < TAG_MAX ? z->tag : TAG_FREE];
161  s->count--;
162  s->bytes -= z->size;
163 
164  if (z->tag != TAG_STATIC) {
165  z->prev->next = z->next;
166  z->next->prev = z->prev;
167  z->magic = 0xdead;
168  z->tag = TAG_FREE;
169  free(z);
170  }
171 }

Referenced by AC_FreeChecks(), Action_Free(), BitField_Free(), Bitmap_Free(), BSP_Free(), BSP_Load(), bsp_mesh_destroy(), BSP_SavePatchedPVS(), change_string_value(), CL_CleanupDemos(), CL_CleanupDownloads(), CL_InitRefresh(), CL_LoadDownloadIgnores(), ClearServers(), CM_FreeMap(), Cmd_AliasSet(), Cmd_RemoveCommand(), Cmd_UnAlias_f(), Cmd_UnTrigger_f(), collect_cluster_lights(), Com_Crash_f(), Com_MapList_m(), computeTangents(), Con_Paste(), Con_RemoteMode_f(), create_shader_module_from_file(), Cvar_GetLatchedVars(), Cvar_SetByVar(), demo_destroy(), demo_free_playlist(), dummy_create(), Field_Free(), finish_download(), free_all_links(), free_msg_packet(), free_search_path(), FreeList(), FreeSlot(), FS_File_g(), FS_FreeList(), FS_Link_f(), FS_ListFiles(), FS_LoadFileEx(), FS_UnLink_f(), get_engine_cvar(), gtv_destroy(), HTTP_CleanupDownloads(), IMG_Load(), IMG_Load_GL(), IMG_ReloadAll(), IMG_Unload(), IMG_Unload_RTX(), Key_SetBinding(), Keybind_Free(), load_blue_noise(), load_cameras(), load_sky_and_lava_clusters(), LOC_Delete_f(), LOC_FreeLocations(), LOC_Update_f(), Menu_Free(), MVD_ClearState(), MVD_Free(), MVD_FreePlayer(), MVD_ParseServerData(), MVD_Shutdown(), MVD_StopRecord(), Netchan_Close(), pack_put(), Pairs_Free(), Parse_File(), parse_file_list(), ParseMasterArgs(), PlayerModel_Free(), PlayerModel_Load(), Pop(), Prompt_Action(), Prompt_Clear(), Prompt_ClearState(), Prompt_CompleteCommand(), Prompt_HistoryUp(), Prompt_LoadHistory(), q_printf(), R_FreeCaches(), R_ModeChanged(), R_NewMap(), R_SetSky_RTX(), R_Shutdown(), remove_all_ignores(), remove_client(), remove_ignore(), reset_unicast_strings(), S_FreeSound(), Savegame_Push(), SCR_ReadNextFrame(), SCR_StopCinematic(), SCR_UnDraw_f(), set_back_cvar(), Slider_Free(), SpinControl_Free(), SV_BeginDownload_f(), SV_CleanClient(), SV_CloseDownload(), SV_DelFilterCmd_f(), SV_DelMatch_f(), SV_DelStuffCmd_f(), SV_InitGameProgs(), SV_MvdShutdown(), SV_SetMaster_f(), SV_Shutdown(), SV_ShutdownClientSend(), VID_GetDefaultModeList(), vkpt_begin_command_buffer(), vkpt_free_command_buffers(), Z_FreeTags(), and Z_Realloc().

◆ Z_FreeTags()

void Z_FreeTags ( memtag_t  tag)

Definition at line 257 of file zone.c.

258 {
259  zhead_t *z, *n;
260 
261  Z_FOR_EACH_SAFE(z, n) {
262  Z_Validate(z, __func__);
263  n = z->next;
264  if (z->tag == tag) {
265  Z_Free(z + 1);
266  }
267  }
268 }

Referenced by PF_FreeTags().

◆ Z_Init()

void Z_Init ( void  )

Definition at line 392 of file zone.c.

393 {
395 }

Referenced by Qcommon_Init().

◆ Z_LeakTest()

void Z_LeakTest ( memtag_t  tag)

Definition at line 120 of file zone.c.

121 {
122  zhead_t *z;
123  size_t numLeaks = 0, numBytes = 0;
124 
125  Z_FOR_EACH(z) {
126  Z_Validate(z, __func__);
127  if (z->tag == tag) {
128  numLeaks++;
129  numBytes += z->size;
130  }
131  }
132 
133  if (numLeaks) {
134  Com_WPrintf("************* Z_LeakTest *************\n"
135  "%s leaked %"PRIz" bytes of memory (%"PRIz" object%s)\n"
136  "**************************************\n",
137  z_tagnames[tag < TAG_MAX ? tag : TAG_FREE],
138  numBytes, numLeaks, numLeaks == 1 ? "" : "s");
139  }
140 }

Referenced by CL_ShutdownRefresh(), FS_Shutdown(), MVD_Shutdown(), S_Shutdown(), SV_Shutdown(), and UI_Shutdown().

◆ Z_Realloc()

void* Z_Realloc ( void ptr,
size_t  size 
)

Definition at line 178 of file zone.c.

179 {
180  zhead_t *z;
181  zstats_t *s;
182 
183  if (!ptr) {
184  return Z_Malloc(size);
185  }
186 
187  if (!size) {
188  Z_Free(ptr);
189  return NULL;
190  }
191 
192  z = (zhead_t *)ptr - 1;
193 
194  Z_Validate(z, __func__);
195 
196  if (z->tag == TAG_STATIC) {
197  Com_Error(ERR_FATAL, "%s: couldn't realloc static memory", __func__);
198  }
199 
200  s = &z_stats[z->tag < TAG_MAX ? z->tag : TAG_FREE];
201  s->bytes -= z->size;
202 
203  if (size > SIZE_MAX - Z_EXTRA - 3) {
204  Com_Error(ERR_FATAL, "%s: bad size", __func__);
205  }
206 
207  size = (size + Z_EXTRA + 3) & ~3;
208  z = realloc(z, size);
209  if (!z) {
210  Com_Error(ERR_FATAL, "%s: couldn't realloc %"PRIz" bytes", __func__, size);
211  }
212 
213  z->size = size;
214  z->prev->next = z;
215  z->next->prev = z;
216 
217  s->bytes += size;
218 
219  Z_TAIL_F(z) = Z_TAIL;
220 
221  return z + 1;
222 }

Referenced by append_light_poly(), recv_func(), and VID_GetDefaultModeList().

◆ Z_ReservedAlloc()

void* Z_ReservedAlloc ( size_t  size)

Definition at line 349 of file zone.c.

350 {
351  void *ptr;
352 
353  if (!size) {
354  return NULL;
355  }
356 
357  if (size > z_reserved_total - z_reserved_inuse) {
358  Com_Error(ERR_FATAL, "%s: couldn't allocate %"PRIz" bytes", __func__, size);
359  }
360 
362  z_reserved_inuse += size;
363 
364  return ptr;
365 }

Referenced by AC_ParseCvar(), NetchanOld_Setup(), SV_MvdInit(), Z_ReservedAllocz(), and Z_ReservedCopyString().

◆ Z_ReservedAllocz()

void* Z_ReservedAllocz ( size_t  size)

Definition at line 367 of file zone.c.

368 {
369  if (!size) {
370  return NULL;
371  }
372  return memset(Z_ReservedAlloc(size), 0, size);
373 }

Referenced by MVD_GameInit().

◆ Z_ReservedCopyString()

char* Z_ReservedCopyString ( const char *  in)

Definition at line 375 of file zone.c.

376 {
377  size_t len;
378 
379  if (!in) {
380  return NULL;
381  }
382 
383  len = strlen(in) + 1;
384  return memcpy(Z_ReservedAlloc(len), in, len);
385 }

◆ Z_Stats_f()

void Z_Stats_f ( void  )

Definition at line 229 of file zone.c.

230 {
231  size_t bytes = 0, count = 0;
232  zstats_t *s;
233  int i;
234 
235  Com_Printf(" bytes blocks name\n"
236  "--------- ------ -------\n");
237 
238  for (i = 0, s = z_stats; i < TAG_MAX; i++, s++) {
239  if (!s->count) {
240  continue;
241  }
242  Com_Printf("%9"PRIz" %6"PRIz" %s\n", s->bytes, s->count, z_tagnames[i]);
243  bytes += s->bytes;
244  count += s->count;
245  }
246 
247  Com_Printf("--------- ------ -------\n"
248  "%9"PRIz" %6"PRIz" total\n",
249  bytes, count);
250 }

Referenced by Qcommon_Init().

◆ Z_TagCopyString()

char* Z_TagCopyString ( const char *  in,
memtag_t  tag 
)

Definition at line 402 of file zone.c.

403 {
404  size_t len;
405 
406  if (!in) {
407  return NULL;
408  }
409 
410  len = strlen(in) + 1;
411  return memcpy(Z_TagMalloc(len, tag), in, len);
412 }

◆ Z_TagMalloc()

void* Z_TagMalloc ( size_t  size,
memtag_t  tag 
)

Definition at line 275 of file zone.c.

276 {
277  zhead_t *z;
278  zstats_t *s;
279 
280  if (!size) {
281  return NULL;
282  }
283 
284  if (tag == TAG_FREE) {
285  Com_Error(ERR_FATAL, "%s: bad tag", __func__);
286  }
287 
288  if (size > SIZE_MAX - Z_EXTRA - 3) {
289  Com_Error(ERR_FATAL, "%s: bad size", __func__);
290  }
291 
292  size = (size + Z_EXTRA + 3) & ~3;
293  z = malloc(size);
294  if (!z) {
295  Com_Error(ERR_FATAL, "%s: couldn't allocate %"PRIz" bytes", __func__, size);
296  }
297  z->magic = Z_MAGIC;
298  z->tag = tag;
299  z->size = size;
300 
301 #ifdef _DEBUG
302 #if (defined __GNUC__)
303  z->addr = __builtin_return_address(0);
304 #elif (defined _MSC_VER)
305  z->addr = _ReturnAddress();
306 #else
307  z->addr = NULL;
308 #endif
309  z->time = time(NULL);
310 #endif
311 
312  z->next = z_chain.next;
313  z->prev = &z_chain;
314  z_chain.next->prev = z;
315  z_chain.next = z;
316 
317  if (z_perturb && z_perturb->integer) {
318  memset(z + 1, z_perturb->integer, size - Z_EXTRA);
319  }
320 
321  Z_TAIL_F(z) = Z_TAIL;
322 
323  s = &z_stats[tag < TAG_MAX ? tag : TAG_FREE];
324  s->count++;
325  s->bytes += size;
326 
327  return z + 1;
328 }

Referenced by FS_LoadFileEx(), PF_TagMalloc(), Z_CvarCopyString(), Z_TagCopyString(), Z_TagMallocz(), and Z_TagReserve().

◆ Z_TagMallocz()

void* Z_TagMallocz ( size_t  size,
memtag_t  tag 
)

Definition at line 330 of file zone.c.

331 {
332  if (!size) {
333  return NULL;
334  }
335  return memset(Z_TagMalloc(size, tag), 0, size);
336 }

Referenced by CM_LoadMap(), and NetchanNew_Setup().

◆ Z_TagReserve()

void Z_TagReserve ( size_t  size,
memtag_t  tag 
)

Definition at line 342 of file zone.c.

343 {
344  z_reserved_data = Z_TagMalloc(size, tag);
345  z_reserved_total = size;
346  z_reserved_inuse = 0;
347 }

Referenced by AC_ParseCvar(), MVD_GameInit(), NetchanOld_Setup(), and SV_MvdInit().

◆ Z_Validate()

static void Z_Validate ( zhead_t z,
const char *  func 
)
inlinestatic

Definition at line 98 of file zone.c.

99 {
100  if (z->magic != Z_MAGIC) {
101  Com_Error(ERR_FATAL, "%s: bad magic", func);
102  }
103  if (Z_TAIL_F(z) != Z_TAIL) {
104  Com_Error(ERR_FATAL, "%s: bad tail", func);
105  }
106  if (z->tag == TAG_FREE) {
107  Com_Error(ERR_FATAL, "%s: bad tag", func);
108  }
109 }

Referenced by Z_Check(), Z_Free(), Z_FreeTags(), Z_LeakTest(), and Z_Realloc().

Variable Documentation

◆ z_chain

zhead_t z_chain
static

Definition at line 49 of file zone.c.

Referenced by Z_Init(), and Z_TagMalloc().

◆ z_reserved_data

byte* z_reserved_data
static

Definition at line 338 of file zone.c.

Referenced by Z_ReservedAlloc(), and Z_TagReserve().

◆ z_reserved_inuse

size_t z_reserved_inuse
static

Definition at line 339 of file zone.c.

Referenced by Z_ReservedAlloc(), and Z_TagReserve().

◆ z_reserved_total

size_t z_reserved_total
static

Definition at line 340 of file zone.c.

Referenced by Z_ReservedAlloc(), and Z_TagReserve().

◆ z_static

const zstatic_t z_static[]
static
Initial value:
= {
#define Z_STATIC(x)
Z_STATIC("0"),
Z_STATIC("1"),
Z_STATIC("2"),
Z_STATIC("3"),
Z_STATIC("4"),
Z_STATIC("5"),
Z_STATIC("6"),
Z_STATIC("7"),
Z_STATIC("8"),
Z_STATIC("9"),
}

Definition at line 57 of file zone.c.

Referenced by Z_CvarCopyString().

◆ z_stats

zstats_t z_stats[TAG_MAX]
static

Definition at line 81 of file zone.c.

Referenced by Z_CvarCopyString(), Z_Free(), Z_Realloc(), Z_Stats_f(), and Z_TagMalloc().

◆ z_tagnames

const char z_tagnames[TAG_MAX][8]
static
Initial value:
= {
"game",
"static",
"generic",
"cmd",
"cvar",
"fs",
"refresh",
"ui",
"server",
"mvd",
"sound",
"cmodel"
}

Definition at line 83 of file zone.c.

Referenced by Z_LeakTest(), and Z_Stats_f().

Z_ReservedAlloc
void * Z_ReservedAlloc(size_t size)
Definition: zone.c:349
zstatic_t
Definition: zone.c:51
Z_EXTRA
#define Z_EXTRA
Definition: zone.c:47
z_reserved_total
static size_t z_reserved_total
Definition: zone.c:340
zstats_t
Definition: zone.c:76
Z_FOR_EACH_SAFE
#define Z_FOR_EACH_SAFE(z, n)
Definition: zone.c:32
Z_FOR_EACH
#define Z_FOR_EACH(z)
Definition: zone.c:29
zhead_s::prev
struct zhead_s * prev
Definition: zone.c:43
zhead_s::magic
uint16_t magic
Definition: zone.c:36
zstats_t::bytes
size_t bytes
Definition: zone.c:78
zstatic_t::data
char data[2]
Definition: zone.c:53
zstatic_t::z
zhead_t z
Definition: zone.c:52
Z_MAGIC
#define Z_MAGIC
Definition: zone.c:23
Z_TagMalloc
void * Z_TagMalloc(size_t size, memtag_t tag)
Definition: zone.c:275
z_static
static const zstatic_t z_static[]
Definition: zone.c:57
Com_Error
void Com_Error(error_type_t type, const char *fmt,...)
Definition: g_main.c:258
z_perturb
cvar_t * z_perturb
Definition: common.c:75
zhead_s::next
struct zhead_s * next
Definition: zone.c:43
Z_Free
void Z_Free(void *ptr)
Definition: zone.c:147
zstats_t::count
size_t count
Definition: zone.c:77
zhead_s::size
size_t size
Definition: zone.c:38
z_stats
static zstats_t z_stats[TAG_MAX]
Definition: zone.c:81
z_tagnames
static const char z_tagnames[TAG_MAX][8]
Definition: zone.c:83
zhead_s::tag
uint16_t tag
Definition: zone.c:37
z_chain
static zhead_t z_chain
Definition: zone.c:49
Z_STATIC
#define Z_STATIC(x)
z_reserved_inuse
static size_t z_reserved_inuse
Definition: zone.c:339
Z_Validate
static void Z_Validate(zhead_t *z, const char *func)
Definition: zone.c:98
z_reserved_data
static byte * z_reserved_data
Definition: zone.c:338
Z_TAIL
#define Z_TAIL
Definition: zone.c:24
Z_TAIL_F
#define Z_TAIL_F(z)
Definition: zone.c:26
zhead_s
Definition: zone.c:35