-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemcheck.c
344 lines (275 loc) · 7.45 KB
/
memcheck.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
/*
* FILE: memcheck.c
*
* Simple-minded memory leak checker for C programs.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MEMCHECK_C
#include "memcheck.h"
#define DEBUG 0
/*
* Definition of data structure to keep memory allocation information.
*/
typedef
struct _mem_node
{
void *addr; /* Address of allocated memory. */
size_t nbytes; /* Number of bytes allocated. */
char *filename; /* Name of file where allocation occurred. */
int lineno; /* Line number of file where allocation occurred. */
struct _mem_node *next; /* Next node in linked list. */
}
mem_node;
/*
* Function prototypes.
*/
void allocate_mem_node(void *addr, size_t nbytes,
char *filename, int lineno);
void free_mem_node(mem_node *n);
void free_mem_node_and_adjust_pool(mem_node *n);
void free_all_mem_nodes(void);
mem_node *find_node(void *addr);
void *checked_malloc_fn(size_t size, char *filename, int lineno);
void *checked_calloc_fn(size_t nmemb, size_t size,
char *filename, int lineno);
void checked_free_fn(void *ptr, char *filename, int lineno);
void print_memory_leaks(void);
void dump_pool(void);
/*
* Initialize the memory pool linked list as a global variable.
*/
mem_node *pool = NULL;
/**********************************************************************
*
* Low-level functions for managing the memory pool linked list.
*
**********************************************************************/
/*
* Allocate a memory node, set its values and link it into the memory
* pool linked list.
*/
void
allocate_mem_node(void *addr, size_t nbytes, char *filename, int lineno)
{
mem_node *n;
char *fn;
/* Create the node and set its fields. */
n = (mem_node *)malloc(sizeof(mem_node));
if (n == NULL)
{
fprintf(stderr, "ERROR: memory allocation failed! Aborting...\n");
exit(1);
}
#if DEBUG == 1
fprintf(stderr, "Allocating %d bytes of memory at %p\n",
nbytes, addr);
#endif
/* Copy the filename, which is statically allocated (I think). */
fn = (char *)malloc((strlen(filename) + 1) * sizeof(char));
strcpy(fn, filename);
n->addr = addr;
n->nbytes = nbytes;
n->filename = fn;
n->lineno = lineno;
/* Add it to the front of the memory pool. */
n->next = pool;
pool = n;
}
/*
* Free a memory node from the pool.
*/
void
free_mem_node(mem_node *n)
{
if (n != NULL)
{
if (n->addr == NULL)
{
fprintf(stderr, "ERROR: invalid node at: %p\n", n->addr);
return;
}
#if DEBUG == 1
fprintf(stderr, "Freeing memory at %p\n", n->addr);
#endif
free(n->addr);
free(n->filename);
free(n);
}
}
/*
* Free a memory node from the pool. Adjust the 'next' pointer of the
* previous node (if any) to skip over this node.
*/
void
free_mem_node_and_adjust_pool(mem_node *n)
{
mem_node *p, *prev;
prev = NULL;
for (p = pool; p != NULL; p = p->next)
{
if (p->addr == n->addr)
{
if (prev == NULL)
{
/* The node to be removed is the first node. */
pool = p->next;
free_mem_node(p);
break; /* Nothing left to do. */
}
else
{
/*
* The node is in the interior of the memory pool
* linked list. Adjust the 'next' pointer of the
* previous node and free the node.
*/
prev->next = p->next;
free_mem_node(p);
break; /* Nothing left to do. */
}
}
else
{
prev = p;
}
}
}
/*
* Free all the memory nodes from the pool.
*/
void free_all_mem_nodes(void)
{
mem_node *n, *next;
n = pool;
while (n != NULL)
{
next = n->next;
free_mem_node(n);
n = next;
}
}
/*
* Return the node that corresponds to the address 'addr', or NULL if
* the address isn't found.
*/
mem_node *
find_node(void *addr)
{
mem_node *n;
for (n = pool; n != NULL; n = n->next)
{
if (n->addr == addr)
{
return n;
}
}
return NULL;
}
/*
* A debugging function to print the contents of the memory pool
* linked list.
*/
void
dump_pool(void)
{
mem_node *n;
for (n = pool; n != NULL; n = n->next)
{
fprintf(stderr, "NODE --------\n");
fprintf(stderr, "location: %p\n", (void *)n);
fprintf(stderr, "addr: %p\n", n->addr);
fprintf(stderr, "nbytes: %d\n", (int)n->nbytes);
fprintf(stderr, "filename: %s\n", n->filename);
fprintf(stderr, "line number: %d\n", n->lineno);
fprintf(stderr, "next: %p\n", (void *)n->next);
fprintf(stderr, "\n");
}
}
/**********************************************************************
*
* User-level functions.
*
* NOTE: Normally, the user will not use these functions, but will
* use 'malloc' and 'calloc' wrappers defined in "memcheck.h".
*
**********************************************************************/
/*
* Allocate 'size' bytes of memory. Also add the address, filename, and line
* number as a new node in the memory pool linked list.
*/
void *
checked_malloc_fn(size_t size, char *filename, int lineno)
{
void *mem;
mem = malloc(size);
if (mem == NULL)
{
fprintf(stderr, "ERROR: memory allocation failed! Aborting...\n");
exit(1);
}
allocate_mem_node(mem, size, filename, lineno);
return mem;
}
/*
* This function is the same as 'checked_malloc' except that it has
* a different signature (corresponding to 'calloc' in the first two
* arguments) and zeroes out all the allocated memory.
*/
void *
checked_calloc_fn(size_t nmemb, size_t size, char *filename, int lineno)
{
void *mem;
mem = calloc(nmemb, size);
if (mem == NULL)
{
fprintf(stderr, "ERROR: memory allocation failed! Aborting...\n");
exit(1);
}
allocate_mem_node(mem, (nmemb * size), filename, lineno);
return mem;
}
/*
* Free a pointer that was previously allocated by 'checked_malloc()'. If
* the memory being freed is not found in the memory pool, print an error
* message and abort.
*/
void
checked_free_fn(void *ptr, char *filename, int lineno)
{
mem_node *n = find_node(ptr);
if (n == NULL)
{
fprintf(stderr,
"ERROR: invalid attempt to free unallocated memory at %p "
"in file: %s, line: %d\n", ptr, filename, lineno);
fprintf(stderr, "Aborting...\n");
free_all_mem_nodes();
exit(1);
}
else
{
free_mem_node_and_adjust_pool(n);
}
}
/*
* This function is intended to be called at the end of a program only.
* It goes through the memory pool node-by-node and prints out information
* on the contents of the node. Any nodes that exist at the end of the
* program represent leaked memory.
*/
void
print_memory_leaks(void)
{
mem_node *n;
for (n = pool; n != NULL; n = n->next)
{
fprintf(stderr,
"Memory leak: %d bytes allocated at %p in "
"file: %s, line: %d.\n",
(int)n->nbytes, n->addr, n->filename, n->lineno);
}
free_all_mem_nodes();
}