-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-malloc.c
204 lines (183 loc) · 4.19 KB
/
count-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
/* count-malloc.c : count mallocs and frees */
/*
Copyright (C) 2005 by zunda <zunda at freeshell.org>
Permission is granted for use, copying, modification, distribution,
and distribution of modified versions of this work under the terms of
GPL version 2 or later.
*/
/* If we're not using GNU C, elide __attribute__ */
#ifndef __GNUC__
#define __attribute__(x) /*NOTHING*/
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <count-malloc.h>
#undef malloc
#undef free
#undef realloc
#include <sys/types.h>
void *malloc(size_t size);
void free(void *ptr);
void *realloc(void *ptr, size_t size);
struct _malloc_entry_s
{
void *ptr;
const char *func;
const char *file;
int line;
size_t size;
struct _malloc_entry_s *next;
};
int malloc_count = 0;
static int malloc_count_reg = 0;
static struct _malloc_entry_s *first = NULL;
static struct _malloc_entry_s *last = NULL;
static void _add(void *ptr, const char *func, const char *file, int line, size_t size);
static int _remove(void *ptr);
static void _counting_check(void);
void
_add(void *ptr, const char *func, const char *file, int line, size_t size)
{
char ptrbuf[10];
struct _malloc_entry_s *new;
malloc_count++;
new = (struct _malloc_entry_s *) malloc(sizeof(struct _malloc_entry_s));
if (new)
{
new->ptr = ptr;
new->func = func;
new->file = file;
new->line = line;
new->size = size;
new->next = NULL;
if (!first) first = new;
if (last) last->next = new;
last = new;
}
else
{
fputs("!! ", stderr);
snprintf(ptrbuf, sizeof(ptrbuf), "%9p", ptr);
fputs(ptrbuf, stderr);
fputs(" - failed to register a memory allocation.\n", stderr);
}
}
int
_remove(void *ptr)
{
struct _malloc_entry_s *prev, *cur;
malloc_count--;
prev = NULL;
cur = first;
while(cur && cur->ptr != ptr)
{
prev = cur;
cur = cur->next;
}
if (cur && cur->ptr == ptr)
{
if (prev)
{
prev->next = cur->next;
}
else
{
first = cur->next;
}
if (last == cur)
{
last = prev;
}
free(cur);
}
if (cur)
return 1;
else
return 0;
}
void
_counting_check(void)
{
size_t i;
unsigned char *ptr;
if (malloc_count != 0)
{
struct _malloc_entry_s *cur;
fprintf(stderr, "!! %d block(s) left\n", malloc_count);
cur = first;
while(cur)
{
fprintf(stderr, "!! %9p allocated in %s %s:%d\n", cur->ptr, cur->func, cur->file, cur->line);
fputs(" ", stderr);
for(i = 0; i < (cur->size > 70 ? 70 : cur->size); i++)
{
ptr = (unsigned char*) cur->ptr + i;
fputc(isprint((int) *ptr) ? *ptr : ' ', stderr);
}
fputs("\n", stderr);
cur = cur->next;
}
abort();
}
}
void
counting_malloc_register_only(void *ptr, const char *func, const char *file, const int line)
{
if (!malloc_count_reg)
{
malloc_count_reg = 1;
atexit(_counting_check);
}
_add(ptr, func, file, line, 0);
if (getenv("MALLOC_COUNT_SHOW"))
fprintf(stderr, "! %3d %9p - in %s around %s:%d\n", malloc_count, ptr, func, file, line);
}
void *
counting_malloc(size_t size, const char *func, const char *file, const int line)
{
void *r;
if (!malloc_count_reg)
{
malloc_count_reg = 1;
atexit(_counting_check);
}
r = malloc(size);
if (r) _add(r, func, file, line, size);
if (getenv("MALLOC_COUNT_SHOW"))
fprintf(stderr, "! %3d %9p - %d bytes in %s %s:%d\n", malloc_count, r, size, func, file, line);
return r;
}
void
counting_free(void *ptr)
{
if (getenv("MALLOC_COUNT_SHOW"))
fprintf(stderr, "! %3d %9p - freeing\n", malloc_count, ptr);
free(ptr);
if (!_remove(ptr))
fprintf(stderr, "!! %9p - DOUBLE FREE?\n", ptr );
}
void *
counting_realloc(void *ptr, size_t size, const char *func, const char *file, const int line)
{
void *r;
if (!malloc_count_reg)
{
malloc_count_reg = 1;
atexit(_counting_check);
}
r = realloc(ptr, size);
if (ptr)
{
_remove(ptr);
if (getenv("MALLOC_COUNT_SHOW"))
fprintf(stderr, "! %3d %9p - reallocated in %s %s:%d\n", malloc_count, ptr, func, file, line);
}
if (getenv("MALLOC_COUNT_SHOW"))
fprintf(stderr, "! %3d %9p - %d bytes in %s %s:%d\n", malloc_count, r, size, func, file, line);
if (r) _add(r, func, file, line, size);
return r;
}