-
Notifications
You must be signed in to change notification settings - Fork 0
/
csa_alloc.c
171 lines (154 loc) · 4.11 KB
/
csa_alloc.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
#include <stdlib.h>
#include <stdio.h>
#include "csa_alloc.h"
#ifndef DEBUG
#define DEBUG 1 // when TRUE a leak report is printed at exit
#endif
#ifndef DETERMINISTIC
#define DETERMINISTIC 0 // when TRUE all mallocs are converted to callocs (no garbage)
#endif
#ifndef RECORD_ALLOC_COUNT_AND_SIZE
#define RECORD_ALLOC_COUNT_AND_SIZE 1
#endif
#ifndef DUMP_ON_DUPLICATE
#define DUMP_ON_DUPLICATE 1
#endif
#if DEBUG
typedef struct alloc_info_struct {
void *ptr;
const char *msg;
size_t size;
char freed;
} AllocInfo;
static AllocInfo *allocs = NULL;
static int n_allocs = 0;
#if RECORD_ALLOC_COUNT_AND_SIZE
static int alloc_count = 0;
static long long int alloc_total_size = 0;
static int free_count = 0;
static long long int free_total_size = 0;
#endif
#endif
void csa_alloc_print_report(void)
{
#if DEBUG
for(int i = 0; i < n_allocs; ++i){
if(!allocs[i].freed){
fprintf(stderr, "\033[1;33mLEAKED %p: %9lu bytes @ %s\033[0m\n", allocs[i].ptr, allocs[i].size, allocs[i].msg);
}
}
#if RECORD_ALLOC_COUNT_AND_SIZE
long long int ats = alloc_total_size;
long long int fts = free_total_size;
int scale = 0;
while(ats >= 1000000000LL || fts >= 1000000000LL){
ats /= 1000LL;
fts /= 1000LL;
++scale;
}
fprintf(stderr,
" \tAllocated\t Freed\n"
"Allocs\t\033[1;34m%9i\t%9i\033[0m\n"
"%sBytes%s\t\033[1;34m%9lli\t%9lli\033[0m\n"
"%s\033[0m\n",
alloc_count, free_count,
scale == 0 ? "" : (scale == 1 ? "K" : (scale == 2 ? "M" : (scale == 3 ? "G" : "!"))), scale == 0 ? " " : "", ats, fts,
alloc_count == free_count ? "\033[1;32mNo memory leaks!" : "\033[1;31mMemory leaks!"
);
#endif
#endif
}
#if DEBUG
static void log_alloc(void *ptr, const char *msg, size_t size)
{
#if RECORD_ALLOC_COUNT_AND_SIZE
++alloc_count;
alloc_total_size += size;
#endif
char found = 0;
for(int i = 0; i < n_allocs; ++i){
if(allocs[i].ptr == ptr){
if(allocs[i].freed){
allocs[i] = (AllocInfo){ptr, msg, size, 0};
found = 1;
}else{
fprintf(stderr, "ERROR: POINTER %p ALLOCATED REPEATEDLY WITHOUT FREE! (%9lu bytes @ %s)\n", ptr, size, msg);
}
}
}
if(found){
return;
}
++n_allocs;
allocs = realloc(allocs, n_allocs * sizeof(AllocInfo));
if(allocs == NULL){
fprintf(stderr, "ABORTING: failed to (re)allocate memory to record the allocation of memory for debugging purposes.\n");
exit(-1);
}
allocs[n_allocs - 1] = (AllocInfo){ptr, msg, size, 0};
}
#endif
void *_csa_calloc(const char *alloc_info, size_t nitems, size_t size)
{
#if DEBUG
void *data = calloc(nitems, size);
log_alloc(data, alloc_info, nitems * size);
return data;
#else // !DEBUG
(void)alloc_info; // unused
return calloc(nitems, size);
#endif // END DEBUG
}
void *_csa_malloc(const char *alloc_info, size_t size)
{
#if DETERMINISTIC
return _csa_calloc(alloc_info, 1, size); // when DETERMINISTIC=1, all mallocs become callocs (no junk, so program behaviour is deterministic)
#else // !DETERMINISTIC
#if DEBUG
void *data = malloc(size);
log_alloc(data, alloc_info, size);
return data;
#else // !DEBUG
(void)alloc_info; // unused
return malloc(size);
#endif // END DEBUG
#endif // END DETERMINISTIC
}
void csa_free(void *ptr)
{
#if DEBUG
if(ptr == NULL) return;
#if RECORD_ALLOC_COUNT_AND_SIZE
++free_count;
#endif
char found = 0;
for(int i = 0; i < n_allocs; ++i){
if(allocs[i].ptr == ptr){
if(found){
fprintf(stderr, "ERROR: POINTER %p OCCURS MORE THAN ONCE IN ARRAY 'allocs'! (this occurrence: %lu bytes @ %s)\n", ptr, allocs[i].size, allocs[i].msg);
#if DUMP_ON_DUPLICATE
for(int j = 0; j < n_allocs; ++j){
fprintf(stderr, "%s%p\t %i %9lu %s\033[0m\n", allocs[j].ptr == ptr ? "\033[1;31m" : "", allocs[j].ptr, allocs[j].freed, allocs[j].size, allocs[j].msg);
}
fprintf(stderr, "\n");
#endif
}
if(allocs[i].freed){
fprintf(stderr, "ERROR: POINTER %p (%lu bytes @ %s) IS BEING FREED A SECOND TIME!\n", ptr, allocs[i].size, allocs[i].msg);
}else{
allocs[i].freed = 1;
#if RECORD_ALLOC_COUNT_AND_SIZE
if(!found){
free_total_size += allocs[i].size;
}
#endif
}
found = 1;
}
}
if(!found){
fprintf(stderr, "ERROR: POINTER %p WAS NOT FOUND IN 'allocs' ARRAY!\n", ptr);
}
#endif
free(ptr);
}