This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathwbMalloc.h
140 lines (119 loc) · 3.22 KB
/
wbMalloc.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
#ifndef __WB_MALLOC_H__
#define __WB_MALLOC_H__
#ifdef WB_USE_CUSTOM_MALLOC
#ifdef __linux__
#define THROW __THROW
#else
#define THROW
#endif
static inline void *_malloc(size_t size) THROW {
if (size == 0) {
return NULL;
} else {
int err;
void *res = memmgr_alloc((ulong)size, &err);
if (err) {
fprintf(stderr, "<<MEMORY>>:: Memory allocation failed\n");
exit(1);
} else {
size_t ii = 0;
unsigned char *p = (unsigned char *)res;
while (ii++ < size) {
*p++ = 0;
}
return res;
}
}
}
static inline void _free(void *ptr) THROW {
if (ptr != nullptr) {
memmgr_free(ptr);
}
}
static inline void *_calloc(size_t nmemb, size_t size) THROW {
return _malloc(nmemb * size);
}
static inline void *_realloc(void *ptr, size_t size) THROW {
if (size == 0) {
free(ptr);
return NULL;
} else if (ptr == nullptr) {
return malloc(size);
} else {
void *buf;
unsigned char *dst;
unsigned char *src;
size_t alloc_size, to_copy, i = 0;
// Allocate new buffer
buf = malloc(size);
if (buf != 0) {
// Find original allocation size
alloc_size = (size_t)memmgr_get_block_size(ptr);
to_copy = alloc_size;
if (to_copy > size) {
to_copy = size;
}
// Copy data to new buffer
dst = (unsigned char *)buf;
src = (unsigned char *)ptr;
while (i++ < to_copy) {
*dst++ = *src++;
}
// Free the old buffer
free(ptr);
}
return buf;
}
}
#define wbNew(type) ((type *)_malloc(sizeof(type)))
#define wbNewArray(type, len) ((type *)_malloc((len) * sizeof(type)))
#define wbMalloc(sz) _malloc(sz)
#define wbDelete(var) \
_free(var); \
var = nullptr
#define wbFree(var) \
_free(var); \
var = nullptr
#define wbRealloc(var, newSize) _realloc(var, newSize)
#define wbReallocArray(t, m, n) ((t *)_realloc(m, n * sizeof(t)))
#define free _free
#define malloc _malloc
#define calloc _calloc
#define realloc _realloc
#else /* WB_USE_CUSTOM_MALLOC */
static inline void *xMalloc(size_t sz) {
void *mem = nullptr;
if (sz != 0) {
mem = malloc(sz);
}
return mem;
}
static inline void xFree(void *mem) {
if (mem != nullptr) {
free(mem);
}
return;
}
static inline void *xRealloc(void *mem, size_t sz) {
if (mem == nullptr) {
return NULL;
} else if (sz == 0) {
xFree(mem);
return NULL;
} else {
void *res = realloc(mem, sz);
wbAssert(res != nullptr);
return res;
}
}
#define wbNew(type) ((type *)wbMalloc(sizeof(type)))
#define wbNewArray(type, len) ((type *)wbMalloc((len) * sizeof(type)))
#define wbMalloc(sz) xMalloc(sz)
#define wbDelete(var) wbFree(var)
#define wbFree(var) \
xFree(var); \
var = nullptr
#define wbRealloc(var, newSize) xRealloc(var, newSize)
#define wbReallocArray(t, m, n) ((t *)xRealloc(m, n * sizeof(t)))
#endif /* WB_USE_CUSTOM_MALLOC */
#endif /* __WB_MALLOC_H__ */