-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
97 lines (83 loc) · 1.53 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
grabbed memory management from Quake 2
https://github.com/id-Software/Quake-2
*/
#include "qcommon.h"
/*
========================
Z_Free
========================
*/
void Z_Free (void *ptr)
{
zhead_t *z;
z = ((zhead_t *)ptr) - 1;
if (z->magic != Z_MAGIC)
fprintf(stderr, "Z_Free: bad magic");
z->prev->next = z->next;
z->next->prev = z->prev;
z_count--;
z_bytes -= z->size;
free (z);
}
/*
========================
Z_Stats_f
========================
*/
void Z_Stats_f (void)
{
Com_Printf("%i bytes in %i blocks\n", z_bytes, z_count);
//printf("%i bytes in %i blocks\n", z_bytes, z_count);
}
/*
========================
Z_FreeTags
========================
*/
void Z_FreeTags (int tag)
{
zhead_t *z, *next;
for (z=z_chain.next ; z != &z_chain ; z=next)
{
next = z->next;
if (z->tag == tag)
Z_Free ((void *)(z+1));
}
}
/*
========================
Z_TagMalloc
========================
*/
void *Z_TagMalloc (int size, int tag)
{
zhead_t *z;
size = size + sizeof(zhead_t);
z = malloc(size);
if(z)
{
memset (z, 0, size);
z_count++;
z_bytes += size;
z->magic = Z_MAGIC;
z->tag = tag;
z->size = size;
z->next = z_chain.next;
z->prev = &z_chain;
z_chain.next->prev = z;
z_chain.next = z;
}
else
Sys_Error("Z_Malloc: failed on allocation of %i bytes",size);
return (void *)(z+1);
}
/*
========================
Z_Malloc
========================
*/
void *Z_Malloc (int size)
{
return Z_TagMalloc (size, 0);
}