-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMalloc.c
366 lines (296 loc) · 7.47 KB
/
Malloc.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* This file is part of the memory management and leak detector MALLOC.
Written by Dick Grune, Vrije Universiteit, Amsterdam.
$Id: Malloc.c,v 1.7 2012-06-13 09:59:52 Gebruiker Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "Malloc.h"
#undef new
#define new use_my_new /* don't call Malloc in Malloc.c */
#define my_new(type) ((type *)malloc(sizeof (type)))
/* All output goes through designated files, so we block printf, etc. */
#undef printf
#define printf use_fprintf
#undef putchar
#define putchar use_fprintf
#ifndef lint
static void
fprintloc(FILE *f, const char *fname, int l_nmb) {
fprintf(f, "\"%s\", line %d: ", fname, l_nmb);
}
static void
out_of_memory(const char *fname, int l_nmb, size_t size) {
fprintloc(stderr, fname, l_nmb);
fprintf(stderr, "Out of memory, requested size = %lld\n",
(long long int)size);
exit(1);
}
#if defined MEMLEAK || defined MEMCLOBBER
/* Both need almost the same information: MEMLEAK obviously needs a list of
all blocks still allocated, but MEMCLOBBER needs the same list to find
the size of a block given to Free(), in order to clobber it.
MEMCLOBBER does not need total, balance and max, but finecombing them out
would be too much.
*/
static unsigned long long int total = 0;
static unsigned long long int balance = 0;
static unsigned long long int max = 0;
struct record {
struct record *next;
const char *addr;
size_t size;
const char *fname;
int l_nmb;
};
#define HASH_SIZE 16381 /* largest prime under 2^16 */
static struct record *record_hash[HASH_SIZE];
#define chain_start(x) record_hash[((unsigned int)(x)%HASH_SIZE)]
static void
record_alloc(char *addr, size_t size, const char *fname, int l_nmb) {
struct record *new;
struct record **r_hook = &chain_start(addr);
if (addr == 0) return;
new = my_new(struct record);
new->addr = addr;
new->size = size;
new->fname = fname; /* no need to copy fname */
new->l_nmb = l_nmb;
new->next = *r_hook;
*r_hook = new;
total += size;
balance += size;
if (balance > max) {
max = balance;
}
}
static struct record **
record_pointer_for_address(const char *addr) {
struct record **rp = &chain_start(addr);
while (*rp) {
if ((*rp)->addr == addr) break;
rp = &(*rp)->next;
}
return rp;
}
static size_t
record_free(char *addr) {
struct record **oldp = record_pointer_for_address(addr);
struct record *old = *oldp;
if (old == 0) return (size_t) -1;
*oldp = old->next;/* this loses the struct record; is that a problem? */
balance -= old->size;
return old->size;
}
#endif /* defined MEMLEAK || defined MEMCLOBBER */
void
MemClobber(void *p, size_t size) {
unsigned char *s = (unsigned char *)p;
size_t i;
for (i = 0; i < size; i++) {
s[i] = 0125; /* 0101 0101 */
}
}
#ifdef MEMLEAK
struct entry {
struct entry *next;
const char *fname;
int l_nmb;
unsigned int n_blocks;
int var_size; /* all blocks have the same size or not */
size_t size; /* !var_size: the one size; var_size: sum of sizes */
};
static struct entry *
compacted_leaks(void) {
struct entry *res = 0;
int i;
for (i = 0; i < HASH_SIZE; i++) {
struct record *r = record_hash[i];
while (r) {
struct entry *e = res;
/* try to find an entry for this location */
while (e) {
if ( e->fname == r->fname
&& e->l_nmb == r->l_nmb
) break;
e = e->next;
}
if (e) { /* update the entry */
if (e->var_size) {
e->size += r->size;
}
else if (e->size != r->size) {
/* switch to var_size */
e->var_size = 1;
e->size =
e->n_blocks*e->size + r->size;
}
e->n_blocks++;
}
else { /* create a new entry */
e = my_new(struct entry);
e->fname = r->fname;
e->l_nmb = r->l_nmb;
e->n_blocks = 1;
e->var_size = 0;
e->size = r->size;
e->next = res;
res = e;
}
r = r->next;
}
}
return res;
}
static int
number_of_leaks(const struct entry *e) {
int res = 0;
while (e != 0) {
res++;
e = e->next;
}
return res;
}
static void
report_actual_leaks(FILE *f) {
const struct entry *e = compacted_leaks();
int n_leaks = number_of_leaks(e);
if (n_leaks == 0) return;
fprintf(f, "There %s %d case%s of unreclaimed memory:\n",
(n_leaks == 1 ? "was" : "were"),
n_leaks,
(n_leaks == 1 ? "" : "s")
);
while (e) {
fprintloc(f, e->fname, e->l_nmb);
fprintf(f, "left allocated: %d block%s of size ",
e->n_blocks, (e->n_blocks == 1 ? "" : "s")
);
if (e->var_size) {
/* e->size is the sum of the sizes */
fprintf(f, "%zd on average",
(e->size + e->n_blocks/2) / e->n_blocks
);
}
else {
/* e->size is the single size */
fprintf(f, "%zd", e->size);
}
if (e->n_blocks > 1) {
fprintf(f, " = %zd",
(e->var_size ? e->size : e->size*e->n_blocks));
}
fprintf(f, "\n");
e = e->next;
}
}
void
ReportMemoryLeaks(FILE *f) {
if (f == 0) f = stderr;
report_actual_leaks(f);
fprintf(f, "Total memory allocated= %lld", total);
fprintf(f, ", maximum allocated = %lld", max);
fprintf(f, ", garbage left = %lld", balance);
fprintf(f, "\n");
}
#else /* no MEMLEAK */
/*ARGSUSED*/
void
ReportMemoryLeaks(FILE *f) {
}
#endif /* MEMLEAK */
void *
_leak_malloc(int chk, size_t size, const char *fname, int l_nmb) {
void *res = malloc(size);
if (chk && res == 0) {
out_of_memory(fname, l_nmb, size);
/*NOTREACHED*/
}
#if defined MEMLEAK || defined MEMCLOBBER
record_alloc(res, size, fname, l_nmb);
#ifdef MEMCLOBBER
MemClobber((char *)res, size);
#endif
#endif
return res;
}
void *
_leak_calloc(int chk, size_t n, size_t size, const char *fname, int l_nmb) {
void *res = calloc(n, size);
if (chk && res == 0) {
out_of_memory(fname, l_nmb, n*size);
/*NOTREACHED*/
}
#if defined MEMLEAK || defined MEMCLOBBER
record_alloc(res, n*size, fname, l_nmb);
#endif
return res;
}
void *
_leak_realloc(int chk, void *addr, size_t size, const char *fname, int l_nmb) {
void *res;
#if defined MEMLEAK || defined MEMCLOBBER
size_t old_size = record_free(addr);
/* we report first, because the realloc() below may cause a crash */
if ( /* we are not reallocating address 0, which is allowed */
addr != 0
&& /* the address was never handed out before */
old_size == (size_t) -1
) {
fprintloc(stderr, fname, l_nmb);
fprintf(stderr, ">>>> unallocated block reallocated <<<<\n");
}
#endif
res = realloc(addr, size);
if (chk && res == 0) {
out_of_memory(fname, l_nmb, size);
/*NOTREACHED*/
}
#if defined MEMLEAK || defined MEMCLOBBER
record_alloc(res, size, fname, l_nmb);
#endif
#ifdef MEMCLOBBER
if (old_size > 0 && size > old_size) {
MemClobber(((char *)res)+old_size, size-old_size);
}
#endif
return res;
}
/* ARGSUSED */
void
_leak_free(void *addr, const char *fname, int l_nmb) {
#if defined MEMLEAK || defined MEMCLOBBER
size_t old_size = record_free(addr);
/* we report first, because the free() below may cause a crash */
if (old_size == (size_t) -1) {
fprintloc(stderr, fname, l_nmb);
fprintf(stderr, ">>>> unallocated block freed ");
fprintf(stderr, "or multiple free of allocated block <<<<\n");
}
else {
#ifdef MEMCLOBBER
MemClobber((char *)addr, old_size);
#endif
}
#endif
free(addr);
}
char *
_new_string(const char *s, const char *fname, int l_nmb) {
return strcpy((char *)(_leak_malloc(1, strlen(s)+1, fname, l_nmb)), s);
}
#endif /* not lint */
#ifdef lint
static void
satisfy_lint(void *x) {
void *v;
v = _leak_malloc(0, 0, 0, 0);
v = _leak_calloc(0, 0, 0, 0, 0);
v = _leak_realloc(0, 0, 0, 0, 0);
_leak_free(x, 0, 0);
ReportMemoryLeaks(0);
v = _new_string(0, 0, 0);
satisfy_lint(v);
}
#endif