-
Notifications
You must be signed in to change notification settings - Fork 6
/
inouealc.c
266 lines (251 loc) · 5.04 KB
/
inouealc.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#undef _MEMORY_DEBUG_
#include "psqlodbc.h"
#ifdef WIN32
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#else
#include <malloc.h>
#endif /* _DEBUG */
#endif /* WIN32 */
#include <string.h>
#include "misc.h"
typedef struct {
size_t len;
void *aladr;
} ALADR;
static int alsize = 0;
static int tbsize = 0;
static ALADR *altbl = NULL;
CSTR ALCERR = "alcerr";
void * pgdebug_alloc(size_t size)
{
void * alloced;
alloced = malloc(size);
MYLOG(2, " alloced=%p(" FORMAT_SIZE_T ")\n", alloced, size);
if (alloced)
{
if (!alsize)
{
alsize = 100;
altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
}
else if (tbsize >= alsize)
{
ALADR *al;
alsize *= 2;
if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
return alloced;
altbl = al;
}
altbl[tbsize].aladr = alloced;
altbl[tbsize].len = size;
tbsize++;
}
else
MYLOG(0, "%s:alloc " FORMAT_SIZE_T "byte\n", ALCERR, size);
return alloced;
}
void * pgdebug_calloc(size_t n, size_t size)
{
void * alloced = calloc(n, size);
if (alloced)
{
if (!alsize)
{
alsize = 100;
altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
}
else if (tbsize >= alsize)
{
ALADR *al;
alsize *= 2;
if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
return alloced;
altbl = al;
}
altbl[tbsize].aladr = alloced;
altbl[tbsize].len = n * size;
tbsize++;
}
else
MYLOG(0, "%s:calloc " FORMAT_SIZE_T "byte\n", ALCERR, size);
MYLOG(2, "calloced = %p\n", alloced);
return alloced;
}
void * pgdebug_realloc(void * ptr, size_t size)
{
void * alloced;
if (!ptr)
return pgdebug_alloc(size);
alloced = realloc(ptr, size);
if (!alloced)
{
MYLOG(0, "%s %p error\n", ALCERR, ptr);
}
else /* if (alloced != ptr) */
{
int i;
for (i = 0; i < tbsize; i++)
{
if (altbl[i].aladr == ptr)
{
altbl[i].aladr = alloced;
altbl[i].len = size;
break;
}
}
}
MYLOG(2, "%p->%p\n", ptr, alloced);
return alloced;
}
char * pgdebug_strdup(const char * ptr)
{
char * alloced = strdup(ptr);
if (!alloced)
{
MYLOG(0, "%s %p error\n", ALCERR, ptr);
}
else
{
if (!alsize)
{
alsize = 100;
altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
}
else if (tbsize >= alsize)
{
ALADR *al;
alsize *= 2;
if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
return alloced;
altbl = al;
}
altbl[tbsize].aladr = alloced;
altbl[tbsize].len = strlen(ptr) + 1;
tbsize++;
}
MYLOG(2, "%p->%p(%s)\n", ptr, alloced, alloced);
return alloced;
}
void pgdebug_free(void * ptr)
{
int i, j;
int freed = 0;
if (!ptr)
{
MYLOG(0, "%s null ptr\n", ALCERR);
return;
}
for (i = 0; i < tbsize; i++)
{
if (altbl[i].aladr == ptr)
{
for (j = i; j < tbsize - 1; j++)
{
altbl[j].aladr = altbl[j + 1].aladr;
altbl[j].len = altbl[j + 1].len;
}
tbsize--;
freed = 1;
break;
}
}
if (! freed)
{
MYLOG(0, "%s not found ptr %p\n", ALCERR, ptr);
return;
}
else
MYLOG(2, "ptr=%p\n", ptr);
free(ptr);
}
static BOOL out_check(void *out, size_t len, const char *name)
{
BOOL ret = TRUE;
int i;
for (i = 0; i < tbsize; i++)
{
if ((ULONG_PTR)out < (ULONG_PTR)(altbl[i].aladr))
continue;
if ((ULONG_PTR)out < (ULONG_PTR)(altbl[i].aladr) + altbl[i].len)
{
if ((ULONG_PTR)out + len > (ULONG_PTR)(altbl[i].aladr) + altbl[i].len)
{
ret = FALSE;
MYLOG(0, "%s:%s:out_check found memory buffer overrun %p(" FORMAT_SIZE_T ")>=%p(" FORMAT_SIZE_T ")\n", ALCERR, name, out, len, altbl[i].aladr, altbl[i].len);
}
break;
}
}
return ret;
}
char *pgdebug_strcpy(char *out, const char *in)
{
if (!out || !in)
{
MYLOG(0, "%s null pointer out=%p,in=%p\n", ALCERR, out, in);
return NULL;
}
out_check(out, strlen(in) + 1, __FUNCTION__);
return strcpy(out, in);
}
char *pgdebug_strncpy(char *out, const char *in, size_t len)
{
if (!out || !in)
{
MYLOG(0, "%s null pointer out=%p,in=%p\n", ALCERR, out, in);
return NULL;
}
out_check(out, len, __FUNCTION__);
return strncpy(out, in, len);
}
char *pgdebug_strncpy_null(char *out, const char *in, size_t len)
{
if (!out || !in)
{
MYLOG(0, "%s null pointer out=%p,in=%p\n", ALCERR, out, in);
return NULL;
}
out_check(out, len, __FUNCTION__);
strncpy_null(out, in, len);
return out;
}
void *pgdebug_memcpy(void *out, const void *in, size_t len)
{
if (!out || !in)
{
MYLOG(0, "%s null pointer out=%p,in=%p\n", ALCERR, out, in);
return NULL;
}
out_check(out, len, __FUNCTION__);
return memcpy(out, in, len);
}
void *pgdebug_memset(void *out, int c, size_t len)
{
if (!out)
{
MYLOG(0, "%s null pointer out=%p\n", ALCERR, out);
return NULL;
}
out_check(out, len, __FUNCTION__);
return memset(out, c, len);
}
void debug_memory_check(void)
{
int i;
if (0 == tbsize)
{
MYLOG(0, "no memry leak found and max count allocated so far is %d\n", alsize);
free(altbl);
alsize = 0;
}
else
{
MYLOG(0, "%s:memory leak found check count=%d alloc=%d\n", ALCERR, tbsize, alsize);
for (i = 0; i < tbsize; i++)
{
MYLOG(0, "%s:leak = %p(" FORMAT_SIZE_T ")\n", ALCERR, altbl[i].aladr, altbl[i].len);
}
}
}