-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug_memory.c
140 lines (129 loc) · 3.63 KB
/
debug_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
#define C_DEBUG_MEMORY_C /* Have to be defined before including debug_memory.h */
#include "debug_memory.h"
debug_node_t* create_node(Site* site) {
#ifdef DEBUG_MEMORY_DEBUG
printf("create_node for %s %4d [%p]\n", site->s.file, site->s.line, site);
#endif
debug_node_t* head = malloc(sizeof(debug_node_t));
head->site = site;
head->next = NULL;
return head;
}
void add_node(Site* site) {
#ifdef DEBUG_MEMORY_DEBUG
printf(" add_node for %s %4d [%p]\n", site->s.file, site->s.line, site);
#endif
debug_node_t* current;
if (debug_root_node == NULL) {
debug_root_node = create_node(site);
} else {
current = debug_root_node;
while (current->next != NULL) {
current = current->next;
}
current->next = create_node(site);
}
}
void remove_node(Site* site) {
#ifdef DEBUG_MEMORY_DEBUG
printf("remove_node for %s %4d [%p]\n", site->s.file, site->s.line, site);
#endif
debug_node_t* tmp_node;
debug_node_t* current;
if (debug_root_node->site == site) {
tmp_node = debug_root_node;
debug_root_node = debug_root_node->next;
free(tmp_node);
return;
}
current = debug_root_node;
while (current->next != NULL) {
if (current->next->site == site) {
tmp_node = current->next;
current->next = tmp_node->next;
free(tmp_node);
return;
}
current = current->next;
}
}
void print_debug_node(debug_node_t* node) {
printf("address : %p\n", node->site);
printf("size : %zu bytes\n", node->site->s.n);
printf("file : %s\n", node->site->s.file);
printf("line : %d\n", node->site->s.line);
printf("----------------------------------------\n");
}
void print_allocated_memory(void) {
debug_node_t *current = debug_root_node;
if (current == NULL) return;
printf("=== Allocated Memory (v%s) ==========\n", DEBUG_MEMORY_DOTTED_VERSION);
while (current != NULL) {
print_debug_node(current);
current = current->next;
}
printf("Total : %d bytes\n", total_allocated);
printf("========================================\n");
}
void* debug_malloc(size_t n, char *file, int line) {
char *rp;
rp = (char*)malloc(sizeof(Site)+n);
total_allocated += n;
((Site*)rp)->s.n = n;
((Site*)rp)->s.file = file;
((Site*)rp)->s.line = line;
add_node((Site*)rp);
return (void*)(rp + sizeof(Site));
}
void* debug_calloc(size_t c, size_t n, char *file, int line) {
char *rp;
rp = (char*)calloc(sizeof(Site)+n*c, sizeof(char));
total_allocated += n*c;
((Site*)rp)->s.n = n*c;
((Site*)rp)->s.file = file;
((Site*)rp)->s.line = line;
add_node((Site*)rp);
return (void*)(rp + sizeof(Site));
}
void* debug_realloc(void *ptr, size_t n, char *file, int line) {
char *rp;
char *tmp;
tmp = ((char*)ptr) - sizeof(Site);
total_allocated -= ((Site*)tmp)->s.n;
rp = (char*)realloc(tmp, sizeof(Site)+n);
total_allocated += n;
((Site*)rp)->s.n = n;
((Site*)rp)->s.file = file;
((Site*)rp)->s.line = line;
add_node((Site*)rp);
return (void*)(rp + sizeof(Site));
}
void debug_free(void *p, char *file, int line) {
char *rp;
rp = ((char*)p) - sizeof(Site);
total_allocated -= ((Site*)rp)->s.n;
((Site*)rp)->s.file = file;
((Site*)rp)->s.line = line;
remove_node((Site*)rp);
free(rp);
}
char* debug_strdup(const char* str, char *file, int line) {
char *rp = NULL;
if (str) {
size_t n = strlen(str) + 1;
rp = malloc(sizeof(Site)+n);
if (rp) {
#ifdef _MSC_VER
strcpy_s(rp + sizeof(Site), n, str);
#else
strcpy(rp + sizeof(Site), str);
#endif
total_allocated += n;
((Site*)rp)->s.n = n;
((Site*)rp)->s.file = file;
((Site*)rp)->s.line = line;
add_node((Site*)rp);
}
}
return rp + sizeof(Site);
}