-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.h
152 lines (139 loc) · 3.69 KB
/
debug.h
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
#ifndef __debug_h__
#define __debug_h__
void* ___d_malloc(size_t, const char*, int);
void* ___d_realloc(void*, size_t, const char*, int);
void ___d_free(void*, const char*, int);
char* ___d_strdup(const char*, const char*, int);
void ___d_exit(int);
#ifdef __implement__
struct ___d_meminfo {
unsigned long magic;
const char* fname;
int line;
size_t size;
struct ___d_meminfo* next;
struct ___d_meminfo* before;
};
static struct ___d_meminfo* ___d_meminfo_f = NULL;
static struct ___d_meminfo* ___d_meminfo_l = NULL;
void*
___d_malloc(size_t size, const char* fname, int line)
{
void* pv = malloc(size + sizeof(struct ___d_meminfo));
struct ___d_meminfo* info;
if (NULL == pv) {
fprintf(stderr, "%s(%d):%s\n", fname, line, "*** malloc failed ***\n");
fflush(stderr);
return NULL;
}
info = (struct ___d_meminfo*)pv;
info->magic = (unsigned long)'Samy';
info->fname = fname;
info->line = line;
info->size = size;
info->next = NULL;
info->before = NULL;
if (NULL == ___d_meminfo_f) {
___d_meminfo_f = ___d_meminfo_l = info;
} else {
___d_meminfo_l->next = info;
info->before = ___d_meminfo_l;
___d_meminfo_l = info;
}
info++;
return (void*)info;
}
void*
___d_realloc(void* pv, size_t size, const char* fname, int line)
{
void* result;
struct ___d_meminfo* info = (struct ___d_meminfo*)pv;
info--;
if (info->magic != (unsigned long)'Samy') {
fprintf(stderr, "%s(%d):%s\n", fname, line, "*** realloc called with invalid pv ***\n");
fflush(stderr);
return realloc(pv, size);
}
result = ___d_malloc(size, fname, line);
if (NULL == result) {
___d_free(pv, fname, line);
return NULL;
}
memcpy(result, pv, info->size);
info->size = size;
___d_free(pv, fname, line);
return result;
}
void
___d_free(void* pv, const char* fname, int line)
{
struct ___d_meminfo* info;
if (NULL == pv) return;
info = (struct ___d_meminfo*)pv;
info--;
if (info->magic != (unsigned long)'Samy') {
fprintf(stderr, "%s(%d):%s\n", fname, line, "*** free called with invalid pv ***\n");
fflush(stderr);
return free(pv);
/*
} else {
fprintf(stderr, "%s(%d):free %s(%d)\n", fname, line, info->fname, info->line);
fflush(stderr);
*/
}
info->magic = 0;
if (NULL != info->next) info->next->before = info->before;
if (NULL != info->before) info->before->next = info->next;
if (___d_meminfo_f == info) ___d_meminfo_f = info->next;
if (___d_meminfo_l == info) ___d_meminfo_l = info->before;
free(info);
}
char*
___d_strdup(const char* c, const char* fname, int line)
{
int size = strlen(c);
char* result = (char*)___d_malloc(size + 1, fname, line);
if (NULL != result) {
memcpy(result, c, size);
result[size] = 0;
}
return result;
}
void
___d_exit(int code)
{
struct ___d_meminfo* p = ___d_meminfo_f;
char* data;
unsigned int ui;
if (NULL != p) {
fprintf(stderr, "*** memory leak detect ***\n");
for (; NULL != p; p = p->next) {
data = (char*)(&p[1]);
fprintf(stderr, "%s(%d):0x%08x %dbyte(s) [", p->fname, p->line, &p[1], p->size);
for (ui = 0; ui < p->size; ui++) {
fprintf(stderr, "%c", data[ui]);
if (ui == 7) {
fprintf(stderr, "...");
break;
}
}
fprintf(stderr,"(");
for (ui = 0; ui < p->size; ui++) {
fprintf(stderr, "%02x", data[ui] & 0xff);
if (ui == 7) {
fprintf(stderr, "...");
break;
}
}
fprintf(stderr, ")]\n");
}
}
exit(code);
}
#endif /* __implement__ */
#define malloc(s) ___d_malloc(s,__FILE__,__LINE__)
#define realloc(v,s) ___d_realloc(v,s,__FILE__,__LINE__)
#define free(v) ___d_free(v,__FILE__,__LINE__)
#define strdup(c) ___d_strdup(c,__FILE__,__LINE__)
#define exit(i) ___d_exit(i)
#endif /* __debug_h__ */