-
Notifications
You must be signed in to change notification settings - Fork 6
/
memory.c
225 lines (197 loc) · 7.31 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
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include "util.h"
struct memory_source msrc_default = MEMSRC(
"Default",
MEMSRC_DEFAULT_MALLOC,
MEMSRC_DEFAULT_FREE,
MEMSRC_NO_FLAGS
);
/**************************************************************************
No debug
**************************************************************************/
#ifdef NDEBUG
void *impl_xmalloc(size_t size, struct memory_source *src)
{
void *ret = 0;
if (src->malloc) {
if (src->flags & MEMSRC_RETURN_IMMEDIATELY)
return (*src->malloc)(size, src);
else
ret = (*src->malloc)(size + MEMDEBUG_OVERHEAD, src);
}
if (!ret)
ret = malloc(size + MEMDEBUG_OVERHEAD);
if (!ret)
XDIE("Out of memory, xmalloc failed.");
return ret;
}
void *impl_xmallocz(size_t size, struct memory_source *src)
{
void *ret = impl_xmalloc(size, src);
memset(ret, 0, size);
return ret;
}
void impl_xfree(void *ptr, struct memory_source *src)
{
if (src->free)
(*src->free)(ptr, src);
else
free(ptr);
}
char *impl_xstrdup(const char *str, struct memory_source *src)
{
size_t len = strlen(str);
char *ret = impl_xmalloc(len+1, src);
return strcpy(ret, str);
}
#else
/**************************************************************************
Memory debug
**************************************************************************/
void *impl_xmalloc(size_t size, struct memory_source *src, const char *file, unsigned int line)
{
void *ret = 0;
if (src->malloc) {
if (src->flags & MEMSRC_RETURN_IMMEDIATELY)
return (*src->malloc)(size, src);
else
ret = (*src->malloc)(size + MEMDEBUG_OVERHEAD, src);
}
if (!ret)
ret = malloc(size + MEMDEBUG_OVERHEAD);
if (!ret)
XDIE("Out of memory, xmalloc(z) failed.");
struct memory_stat *stat = (struct memory_stat*)ret;
stat->file = file;
stat->line = line;
stat->size = size;
stat->prev = 0;
if (!src->stat_list) {
stat->next = 0;
src->stat_list = stat;
} else {
stat->next = src->stat_list;
src->stat_list->prev = stat;
src->stat_list = stat;
}
src->allocs++;
src->bytes += size;
ret += sizeof(struct memory_stat);
return ret;
}
void *impl_xmallocz(size_t size, struct memory_source *src, const char *file, unsigned int line)
{
void *ret = impl_xmalloc(size, src, file, line);
memset(ret, 0, size);
return ret;
}
void impl_xfree(void *ptr, struct memory_source *src)
{
if (src->free && (src->flags & MEMSRC_RETURN_IMMEDIATELY)) {
(*src->free)(ptr, src);
return;
}
struct memory_stat *memstat = ptr - sizeof(struct memory_stat);
if (memstat->next)
memstat->next->prev = (memstat->prev) ? memstat->prev : 0;
if (memstat->prev)
memstat->prev->next = (memstat->next) ? memstat->next : 0;
if (src->stat_list == memstat)
src->stat_list = memstat->next;
src->frees++;
src->bytes -= memstat->size;
if (src->free)
(*src->free)(memstat, src);
else
free(memstat);
}
char *impl_xstrdup(const char *str, struct memory_source *src, const char *file, unsigned int line)
{
size_t len = strlen(str);
char *ret = impl_xmalloc(len+1, src, file, line);
return strcpy(ret, str);
}
/**************************************************************************
Debug report utils
**************************************************************************/
#ifndef MEMDEBUG_ASCII_STATS
static void print_source_stat(struct memory_source *src, int details)
{
int diff = (int)src->allocs - src->frees;
printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
printf("┃ Source: %-63s ┃\n", src->name);
printf("┠─────────────────────────────────────────────────────────────────────────┨\n");
printf("┃ Allocs: %-58u ┃\n", src->allocs);
printf("┃ Frees: %-58u ┃\n", src->frees);
printf("┃ Diff: %-58d ┃\n", diff);
printf("┃ Bytes taken: %-58u ┃\n", src->bytes);
printf("┃ + overhead: %-58d ┃\n", diff * (int)MEMDEBUG_OVERHEAD);
if (!diff || !src->stat_list || !details) {
printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
} else {
printf("┠────────────┬────────────┬───────────────────────────────────────────────┨\n");
printf("┃ ptr │ size │ location ┃\n");
printf("┠────────────┼────────────┼───────────────────────────────────────────────┨\n");
struct memory_stat *stat = src->stat_list;
while (stat) {
char location[50];
snprintf(location, sizeof(location), "%s:%u", stat->file, stat->line);
location[sizeof(location)-1] = '\0';
printf("┃ %10p │ %10zu │ %-45s ┃\n", stat+1, stat->size,
location);
stat = stat->next;
}
printf("┗━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
}
}
#else
static void print_source_stat(struct memory_source *src, int details)
{
int diff = (int)src->allocs - src->frees;
printf("/=========================================================================\\\n");
printf("| Source: %-63s |\n", src->name);
printf("+-------------------------------------------------------------------------+\n");
printf("| Allocs: %-58u |\n", src->allocs);
printf("| Frees: %-58u |\n", src->frees);
printf("| Diff: %-58d |\n", diff);
printf("| Bytes taken: %-58u |\n", src->bytes);
printf("| + overhead: %-58d |\n", diff * MEMDEBUG_OVERHEAD);
if (!diff || !src->stat_list || !details) {
printf("\\=========================================================================/\n");
} else {
printf("+------------+------------+-----------------------------------------------+\n");
printf("| ptr | size | location |\n");
printf("+------------+------------+-----------------------------------------------+\n");
struct memory_stat *stat = src->stat_list;
while (stat) {
char location[50];
snprintf(location, sizeof(location), "%s:%u", stat->file, stat->line);
location[sizeof(location)-1] = '\0';
printf("| %10p | %10u | %-45s |\n", stat+1, stat->size,
location);
stat = stat->next;
}
printf("\\============+============+===============================================/\n");
}
}
#endif /* #ifndef else MEMDEBUG_ASCII_STATS */
#endif /* #ifdef else NDEBUG */
void xmemstat(struct memory_source **sources, size_t n, int details)
{
#ifndef NDEBUG
size_t i;
printf("\033[32m");
print_source_stat(&msrc_default, details);
for (i = 0; i < n; ++i) {
if (i % 2)
printf("\033[36m");
else
printf("\033[0m");
print_source_stat(sources[i], details);
}
printf("\033[0m");
fflush(stdout);
#endif
}