-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
301 lines (240 loc) · 6.74 KB
/
memory.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memory.h"
#ifndef NDEBUG
#define DEBUG_MALLOC_OUT stderr
typedef struct {
void * ptr;
size_t size;
const char * srcname;
int srcline;
} memory_alloc_history_entry_t;
static memory_alloc_history_entry_t *memory_alloc_history = NULL;
static size_t memory_alloc_history_size = 0;
static size_t memory_alloc_history_pos = 0;
static memory_alloc_history_entry_t memory_remove_entry(void *ptr,
const char *srcname,
int srcline);
static void memory_add_entry(void *ptr, size_t size, const char *srcname,
int srcline);
void
memory_init(void)
{
memory_alloc_history = NULL;
memory_alloc_history_size = 0;
memory_alloc_history_pos = 0;
#ifdef DEBUG_MALLOC_VERBOSE
fprintf(DEBUG_MALLOC_OUT, "DEBUGGER: memory_init()\n");
#endif
}
void *
memory_malloc(size_t size, const char *srcname, int line)
{
void *ptr;
ptr = compat_malloc(size);
#ifdef DEBUG_MALLOC_VERBOSE
fprintf(DEBUG_MALLOC_OUT,
"malloc():\tin %s:%d %d bytes (-> %p)\n", srcname, line,
size, ptr);
if (ptr == NULL) fprintf(DEBUG_MALLOC_OUT,
"WARNING: malloc returned NULL\n");
#endif
memory_add_entry(ptr, size, srcname, line);
return ptr;
}
void *
memory_calloc(size_t nelem, size_t size, const char *srcname, int line)
{
void *ptr;
ptr = compat_calloc(nelem, size);
#ifdef DEBUG_MALLOC_VERBOSE
fprintf(DEBUG_MALLOC_OUT,
"calloc():\tin %s:%d %d bytes (-> %p)\n", srcname, line,
nelem*size, ptr);
if (ptr == NULL) fprintf(DEBUG_MALLOC_OUT,
"WARNING: calloc returned NULL\n");
#endif
memory_add_entry(ptr, size, srcname, line);
return ptr;
}
void *
memory_realloc(void *ptr, size_t size, const char *srcname, int line)
{
void *p;
p = compat_realloc(ptr, size);
#ifdef DEBUG_MALLOC_VERBOSE
fprintf(DEBUG_MALLOC_OUT,
"realloc():\tin %s:%d %d bytes (%p -> %p)\n", srcname, line,
size, ptr, p);
if (p == NULL) fprintf(DEBUG_MALLOC_OUT,
"WARNING: realloc returned NULL\n");
#endif
memory_remove_entry(ptr, srcname, line);
memory_add_entry(p, size, srcname, line);
return p;
}
void
memory_free(void *ptr, const char *srcname, int line)
{
#ifdef DEBUG_MALLOC_VERBOSE
memory_alloc_history_entry_t entry;
entry =
#endif
memory_remove_entry(ptr, srcname, line);
#ifdef DEBUG_MALLOC_VERBOSE
fprintf(DEBUG_MALLOC_OUT,
"free():\tin %s:%d %d bytes (%p allocated in %s:%d)\n",
srcname, line, entry.size, ptr, entry.srcname, entry.srcline);
#endif
free(ptr);
}
void
memory_done(void)
{
if (memory_alloc_history_pos != 0) {
#ifdef DEBUG_MALLOC_VERBOSE
fprintf(DEBUG_MALLOC_OUT,
"WARNING: have still %d malloc'ed chunks.\n",
memory_alloc_history_pos);
#endif
memory_list_allocated();
} else {
#ifdef DEBUG_MALLOC_VERBOSE
fprintf(DEBUG_MALLOC_OUT,
"OK: memory has been cleared.\n");
#endif
}
free(memory_alloc_history);
memory_alloc_history = NULL;
memory_alloc_history_size = 0;
memory_alloc_history_pos = 0;
}
void
memory_list_allocated(void)
{
size_t i;
fprintf(DEBUG_MALLOC_OUT,
"\n****************************************\n");
fprintf(DEBUG_MALLOC_OUT, "Memory pointer list:\n");
for (i = 0; i < memory_alloc_history_pos; i++)
fprintf(DEBUG_MALLOC_OUT,
"\tfrom %s:%d have %zu bytes (at %p)\n",
memory_alloc_history[i].srcname,
memory_alloc_history[i].srcline,
memory_alloc_history[i].size,
memory_alloc_history[i].ptr
);
fprintf(DEBUG_MALLOC_OUT,
"\n****************************************\n");
}
static void
memory_add_entry(void *ptr, size_t size, const char *srcname,
int srcline)
{
if (ptr == NULL) return;
if (memory_alloc_history_pos >= memory_alloc_history_size) {
memory_alloc_history_entry_t *mh;
mh = (memory_alloc_history_entry_t *)
realloc(memory_alloc_history,
(memory_alloc_history == NULL ? 32 :
memory_alloc_history_size<<1)*
sizeof(memory_alloc_history_entry_t));
assert(mh != NULL);
memory_alloc_history_size = (memory_alloc_history == NULL ? 32 :
memory_alloc_history_size<<1);
memory_alloc_history = mh;
}
assert(memory_alloc_history_pos < memory_alloc_history_size);
memory_alloc_history[memory_alloc_history_pos].ptr = ptr;
memory_alloc_history[memory_alloc_history_pos].size = size;
memory_alloc_history[memory_alloc_history_pos].srcname = srcname;
memory_alloc_history[memory_alloc_history_pos].srcline = srcline;
memory_alloc_history_pos++;
}
static memory_alloc_history_entry_t
memory_remove_entry(void *ptr,
const char *srcname,
int srcline)
{
size_t i;
memory_alloc_history_entry_t cp;
assert(memory_alloc_history_pos <= memory_alloc_history_size);
for (i = 0; i < memory_alloc_history_pos; i++)
if (memory_alloc_history[i].ptr == ptr) break;
if (i >= memory_alloc_history_pos) {
fprintf(DEBUG_MALLOC_OUT,
"ERROR: tried to free junk (%p) in %s:%d.\n", ptr,
srcname, srcline);
abort();
}
/* i pointing to to-be-removed entry */
memcpy(&cp, memory_alloc_history+i,
sizeof(memory_alloc_history_entry_t));
if (i+1 < memory_alloc_history_size)
memmove(memory_alloc_history+i, memory_alloc_history+i+1,
(memory_alloc_history_size-i-1)*
sizeof(memory_alloc_history_entry_t));
memset(&memory_alloc_history[memory_alloc_history_size-1], 0,
sizeof(memory_alloc_history_entry_t));
memory_alloc_history_pos--;
return cp;
}
void
memory_hexdump(void *ptr, const char *srcname, int srcline)
{
size_t i;
for (i = 0; i < memory_alloc_history_pos; i++)
if (memory_alloc_history[i].ptr == ptr) break;
if (i >= memory_alloc_history_pos) {
fprintf(DEBUG_MALLOC_OUT,
"ERROR: tried to hexdump junk in %s:%d.\n"
"\tPlease use memory_hexdump_alien(), "
"if you think that it's correct.\n",
srcname, srcline);
abort();
}
fprintf(DEBUG_MALLOC_OUT,
"\n****************************************\n");
fprintf(DEBUG_MALLOC_OUT, "\tfrom %s:%d have %zu bytes (at %p)\n",
memory_alloc_history[i].srcname,
memory_alloc_history[i].srcline,
memory_alloc_history[i].size,
memory_alloc_history[i].ptr
);
memory_hexdump_alien(ptr, memory_alloc_history[i].size);
fprintf(DEBUG_MALLOC_OUT,
"\n****************************************\n");
}
void
memory_hexdump_alien(void *ptr, size_t len)
{
size_t pos;
for (pos = 0; pos < len; pos++) {
if ((pos&0x0F) == 0)
fprintf(DEBUG_MALLOC_OUT, "%06zX: ", pos);
fprintf(DEBUG_MALLOC_OUT, "%02X ", ((char *)ptr)[pos]);
if ((pos&0x0F) == 0x0F)
fprintf(DEBUG_MALLOC_OUT, "\n");
}
if ((pos&0x0F)) fprintf(DEBUG_MALLOC_OUT, "\n");
}
#endif
void *
compat_malloc(size_t size)
{
if (size == 0) size++;
return malloc(size);
}
void *
compat_calloc(size_t nelem, size_t size)
{
if (size == 0) size++;
return calloc(nelem, size);
}
void *
compat_realloc(void *ptr, size_t size)
{
if (size == 0) size++;
return realloc(ptr, size);
}