-
Notifications
You must be signed in to change notification settings - Fork 5
/
gmem.h
149 lines (133 loc) · 4.04 KB
/
gmem.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef GMEM_H_
#define GMEM_H_
/*
* A set of macros / functions to allocate and release dynamic memory.
*
* When compiling with GMEM_CHECK defined, the macros keep track of all memory
* allocation and deallocation, and a summary is printed out at the end; when
* it is undefined, the macros are equivalent to raw calls to malloc / realloc
* / free, thus incurring no runtime cost; additionally, in this case when
* freeing, the freed variable is set to zero.
*
* Examples for calling the macros:
*
* GMEM_NEW(header, Header*, sizeof(Header))
* => header = (Header*) malloc(sizeof(Header))
*
* GMEM_REALLOC(data, Data*, 20, 30)
* => data = (Data*) realloc(data, 40), set last 10 elements to 0
*
* GMEM_DEL(header, Header*, sizeof(Header))
* => free(header), header = 0
*
* GMEM_NEWARR(data, Data*, 10, sizeof(Data))
* => data = (Data*) calloc(40, sizeof(Dasta))
*
* GMEM_DELARR(data, Data*, 10, sizeof(Data))
* => free(data), data = 0
*/
#include <stdlib.h>
#include <string.h>
#define _GMEM_NEW(scalar, type, size) \
do { \
scalar = (type) malloc(size); \
} while (0)
#define _GMEM_REALLOC(scalar, type, osize, nsize) \
do { \
scalar = (type) realloc(scalar, nsize); \
} while (0)
#define _GMEM_DEL(scalar, type, size) \
do { \
free(scalar); \
scalar = 0; \
} while (0)
void gmem_init(void);
void gmem_fini(void);
#if !defined(GMEM_CHECK) || GMEM_CHECK < 1
#define GMEM_NEW(scalar, type, size) _GMEM_NEW(scalar, type, size)
#define GMEM_REALLOC(scalar, type, osize, nsize) _GMEM_REALLOC(scalar, type, osize, nsize)
#define GMEM_DEL(scalar, type, size) _GMEM_DEL(scalar, type, size)
#define GMEM_NEWARR(array, type, count, size) \
do { \
array = (type) calloc(count, size); \
} while (0)
#define GMEM_DELARR(array, type, count, size) \
do { \
free(array); \
array = 0; \
} while (0)
#define GMEM_NEWSTR(tgt, src, len, ret) \
do { \
tgt = 0; \
if (!src) { \
ret = 0; \
break; \
} \
int l = len <= 0 ? strlen(src) + 1 : len; \
_GMEM_NEW(tgt, char*, l); \
memcpy(tgt, src, l); \
ret = l; \
} while (0)
#define GMEM_DELSTR(str, len) \
do { \
_GMEM_DEL(str, char*, len); \
} while (0)
#else
#define GMEM_NEW(scalar, type, size) \
do { \
_GMEM_NEW(scalar, type, size); \
gmem_new_called(__FILE__, __LINE__, scalar, 1, size); \
} while (0)
#define GMEM_REALLOC(scalar, type, osize, nsize) \
do { \
gmem_del_called(__FILE__, __LINE__, scalar, 1, osize); \
_GMEM_REALLOC(scalar, type, osize, nsize); \
gmem_new_called(__FILE__, __LINE__, scalar, 1, nsize); \
} while (0)
#define GMEM_DEL(scalar, type, size) \
do { \
gmem_del_called(__FILE__, __LINE__, scalar, 1, size); \
_GMEM_DEL(scalar, type, size); \
} while (0)
#define GMEM_NEWARR(array, type, count, size) \
do { \
array = (type) calloc(count, size); \
gmem_new_called(__FILE__, __LINE__, array, count, size); \
} while (0)
#define GMEM_DELARR(array, type, count, size) \
do { \
gmem_del_called(__FILE__, __LINE__, array, count, size); \
free(array); \
array = 0; \
} while (0)
#define GMEM_NEWSTR(tgt, src, len, ret) \
do { \
ret = gmem_strnew(__FILE__, __LINE__, &tgt, src, len); \
} while (0)
#define GMEM_DELSTR(str, len) \
do { \
gmem_strdel(__FILE__, __LINE__, &str, len); \
} while (0)
extern long gmem_new;
extern long gmem_del;
int gmem_new_called(const char* file,
int line,
void* var,
int count,
long size);
int gmem_del_called(const char* file,
int line,
void* var,
int count,
long size);
int gmem_strnew(const char* file,
int line,
char** tgt,
const char* src,
int len);
int gmem_strdel(const char* file,
int line,
char** str,
int len);
#endif /* #if !defined(GMEM_CHECK) || GMEM_CHECK < 1 */
#endif