-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMemLeak.c
462 lines (420 loc) · 14 KB
/
CMemLeak.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*******************************************************************************
* No Copyright - this is freeware
********************************************************************************
File: CMemLeak.c
Author: Xie Wei Bao (UK) Ltd
email: tech@cup.btinternet.co.uk
Purpose: Detecting memory leaks
********************************************************************************
*/
#include "CMemLeak.h"
#undef malloc
#undef realloc
#undef free
#undef strdup
#undef calloc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
/* Guards for checking illegal memory writes */
static const char xwbProtect[] = "DeAd";
static const unsigned int xwbProtSize = sizeof (xwbProtect);
/* Filename of report file */
static const char xwbReportFilename[] = "CMemLeak.txt";
/* Uninitialized memory - pick a value that will cause the most problems */
static const unsigned char xwbUninit = 0x55;
/* Clean memory - pick a value which will cause the most problems */
static const unsigned char xwbFreed = 0xAA;
static const char xwbIMW[] = "IMW"; /* Illegal memory write */
static const char xwbMLK[] = "MLK"; /* Memory leak */
static const char xwbFNH[] = "FNH"; /* Free Non Heap memory */
static const char xwbFMW[] = "FMW"; /* Free Memory Write */
/* Node for storing the allocation details */
struct XWBNode
{
struct XWBNode* mPrev;
struct XWBNode* mNext;
void* mPtr;
unsigned int mSize;
const char* mFile;
unsigned int mLine;
const char* mName;
};
struct XWBList
{
/* Doubly linked list */
struct XWBNode* mHead;
struct XWBNode* mTail;
FILE* mReport;
unsigned long mAllocUsed; /* Max in the life of the program */
unsigned long mAllocTotal; /* Number of allocations */
unsigned long mAllocCurrent; /* Current allocation */
unsigned int mFree; /* 1 if memory to be freed */
unsigned int mAllocMax; /* Not yet - preallocate nodes */
struct XWBNode* mNode; /* Not yet - contiguous node storage */
struct XWBNode* mUnused; /* Not yet - chain of free nodes */
};
/* Link for storing allocation details */
static struct XWBList xwbMem =
{
(struct XWBNode*) 0,
(struct XWBNode*) 0
};
/*******************************************************************************
* Forward declarations
*******************************************************************************/
static struct XWBNode* XWBNodeNew (void);
static void XWBNodeDelete (struct XWBNode* that);
static void XWBNodeFree (
struct XWBNode* that,
const char* iName,
const char* iFile,
const unsigned int iLine);
static void XWBNodeLink (
struct XWBNode*,
struct XWBNode*,
struct XWBNode*);
static void XWBNodeSet (
struct XWBNode* that,
void* iPtr,
const unsigned int iSize,
const char* iFile,
const unsigned int iLine);
static void XWBNodeIMWCheck (struct XWBNode* that);
static void XWBMemNew (void);
static struct XWBNode* XWBMemFind (
void* iPtr,
unsigned int* oSIze,
const char** oFile,
unsigned int* oLine);
static void XWBMemDump (void);
static void XWBMemInsert (
void* iPtr,
const unsigned int iSize,
const char* iFile,
const unsigned int iLine);
/*******************************************************************************
* New node
*******************************************************************************/
static struct XWBNode* XWBNodeNew (void)
{
struct XWBNode* that = (struct XWBNode*) malloc (sizeof (struct XWBNode));
that->mPrev = 0;
that->mNext = 0;
that->mName = 0;
return that;
}
/*******************************************************************************
* Delete node
*******************************************************************************/
static void XWBNodeDelete (struct XWBNode* that)
{
/* Unlink */
if (that->mPrev)
that->mPrev->mNext = that->mNext;
if (that->mNext)
that->mNext->mPrev = that->mPrev;
free (that);
}
/*******************************************************************************
* Free a node
*******************************************************************************/
static void XWBNodeFree (
struct XWBNode* that,
const char* iName,
const char* iFile,
const unsigned int iLine)
{
that->mFile = iFile;
that->mLine = iLine;
that->mName = iName;
}
/*******************************************************************************
* Link up node
*******************************************************************************/
static void XWBNodeLink (
struct XWBNode* that,
struct XWBNode* iPrev,
struct XWBNode* iNext
)
{
that->mPrev = iPrev;
if (iPrev != 0)
iPrev->mNext = that;
that->mNext = iNext;
if (iNext != 0)
iNext->mPrev = that;
}
/*******************************************************************************
* Set up node
*******************************************************************************/
static void XWBNodeSet (
struct XWBNode* that,
void* iPtr,
const unsigned int iSize,
const char* iFile,
const unsigned int iLine
)
{
that->mPtr = iPtr;
that->mSize = iSize;
that->mFile = iFile;
that->mLine = iLine;
}
/*******************************************************************************
* Initialization
*******************************************************************************/
static void XWBMemNew (void)
{
/* Set up the doubly linked list */
xwbMem.mHead = XWBNodeNew ();
xwbMem.mTail = XWBNodeNew ();
XWBNodeLink (xwbMem.mHead, 0, xwbMem.mTail);
XWBNodeLink (xwbMem.mTail, xwbMem.mHead, 0);
/* Initialize statistics */
xwbMem.mAllocUsed = 0L;
xwbMem.mAllocTotal = 0L;
xwbMem.mAllocCurrent = 0L;
xwbMem.mFree = 1;
xwbMem.mReport = fopen (xwbReportFilename, "w");
atexit (XWBReportFinal);
}
/*******************************************************************************
* Dump List - used for debugging only
*******************************************************************************/
static void XWBMemDump ()
{
int count;
struct XWBNode* iter = xwbMem.mHead;
for (count = 0; iter != 0; count++, iter = iter->mNext)
{
fprintf (xwbMem.mReport, "%d node %p prev %p next %p\n",
count, (void*)iter, (void*)iter->mPrev, (void*)iter->mNext);
}
fprintf (xwbMem.mReport, "\n");
}
/*******************************************************************************
* Insert into the tracking list
*******************************************************************************/
void XWBMemInsert (
void* iPtr,
const unsigned int iSize,
const char* iFile,
const unsigned int iLine
)
{
struct XWBNode* node;
if (xwbMem.mHead == 0)
{
XWBMemNew ();
}
/* Link in the new node */
node = XWBNodeNew ();
XWBNodeSet (node, iPtr, iSize, iFile, iLine);
XWBNodeLink (node, xwbMem.mTail->mPrev, xwbMem.mTail);
xwbMem.mAllocTotal += 1;
xwbMem.mAllocCurrent += iSize;
if (xwbMem.mAllocUsed < xwbMem.mAllocCurrent)
xwbMem.mAllocUsed = xwbMem.mAllocCurrent;
}
/*******************************************************************************
* Find a memory pointer
*******************************************************************************/
static struct XWBNode* XWBMemFind (
void* iPtr,
unsigned int* oSize,
const char** oFile,
unsigned int* oLine
)
{
struct XWBNode* result = 0;
struct XWBNode* iter;
iter = xwbMem.mTail;
while ((iter = iter->mPrev) != xwbMem.mHead)
{
if (iter->mPtr == iPtr)
{
result = iter;
*oSize = iter->mSize;
*oFile = iter->mFile;
*oLine = iter->mLine;
break;
}
}
return result;
}
/*******************************************************************************
* Allocate memory
*******************************************************************************/
void* XWBMalloc (unsigned int iSize, const char* iFile, const unsigned int iLine)
{
register int usize;
unsigned char* result;
usize = ((iSize + xwbProtSize) / sizeof (unsigned int) + 1) * sizeof (unsigned int);
result = malloc (usize);
memset (result, xwbUninit, usize);
memcpy (&result[iSize], xwbProtect, xwbProtSize);
XWBMemInsert (result, iSize, iFile, iLine);
return (void*) result;
}
/*******************************************************************************
* Allocate memory
*******************************************************************************/
void* XWBRealloc (void* iPtr, unsigned int iSize, const char* iFile, const unsigned int iLine)
{
register int usize;
unsigned char* result;
struct XWBNode* node;
unsigned int size, line;
const char* name;
usize = ((iSize + xwbProtSize) / sizeof (unsigned int) + 1) * sizeof (unsigned int);
result = realloc (iPtr, usize);
/* memset (result, xwbUninit, usize); */
memcpy (&result[iSize], xwbProtect, xwbProtSize);
/* Update the allocation details */
name = iFile;
line = iLine;
node = XWBMemFind (iPtr, &size, &name, &line);
XWBNodeSet (node, result, iSize, name, line);
xwbMem.mAllocCurrent -= size;
xwbMem.mAllocCurrent += iSize;
if (xwbMem.mAllocUsed < xwbMem.mAllocCurrent)
xwbMem.mAllocUsed = xwbMem.mAllocCurrent;
return (void*) result;
}
/*******************************************************************************
* Unallocate memory
*******************************************************************************/
void XWBFree (void* iPtr, const char* iDesc, const char* iFile, const unsigned int iLine)
{
/* Check if it is one of ours */
const char* file;
unsigned int line;
unsigned int size;
struct XWBNode* node;
node = XWBMemFind (iPtr, &size, &file, &line);
if (node != 0)
{
unsigned char* ptr = (unsigned char*) iPtr;
if (memcmp (&ptr[size], xwbProtect, xwbProtSize) != 0)
{
/* Illegal memory write */
fprintf (xwbMem.mReport, "%s: %s allocated %s: %u\n", xwbIMW, iDesc, file, line);
fprintf (xwbMem.mReport, " : %s deallocated %s: %u\n", iDesc, iFile, iLine);
}
memset (iPtr, xwbFreed, size);
if (xwbMem.mFree)
{
free (iPtr);
XWBNodeDelete (node);
}
else
{
/* Save the freed memory details */
XWBNodeFree (node, iDesc, iFile, iLine);
}
xwbMem.mAllocCurrent -= size;
}
else
{
/* Free non-heap memory */
fprintf (xwbMem.mReport, "%s: %s deallocated %s: %u\n", xwbFNH, iDesc, iFile, iLine);
/* Don't do it otherwise it might crash */
}
}
/*******************************************************************************
* Do not free
*******************************************************************************/
void XWBNoFree (void)
{
if (xwbMem.mHead == 0)
{
XWBMemNew ();
}
xwbMem.mFree = 0;
}
/*******************************************************************************
* Report
*******************************************************************************/
void XWBReport (const char* iTag)
{
struct XWBNode* iter;
unsigned char* ptr;
unsigned int size;
register unsigned int u;
if (xwbMem.mHead == 0)
{
XWBMemNew ();
}
if (iTag)
fprintf (xwbMem.mReport, "\n%s\n", iTag);
/* XWBListDump (); */
iter = xwbMem.mHead;
while ((iter = iter->mNext) != xwbMem.mTail)
{
ptr = (unsigned char*) iter->mPtr;
size = iter->mSize;
if (iter->mName)
{
/* Check that there are no FMWs */
for (u = 0; u < size; u++)
{
if (ptr[u] != xwbFreed)
{
fprintf (xwbMem.mReport, "%s: %s freed at %s: %u\n",
xwbFMW, iter->mName, iter->mFile, iter->mLine);
break;
}
}
}
else
{
fprintf (xwbMem.mReport, "%s: %p %u bytes allocated %s: %u\n",
xwbMLK, iter->mPtr, iter->mSize, iter->mFile, iter->mLine);
if (memcmp (&ptr[size], xwbProtect, xwbProtSize) != 0)
{
/* Illegal memory write */
fprintf (xwbMem.mReport, "%s: %p allocated %s: %u\n",
xwbIMW, ptr, iter->mFile, iter->mLine);
}
}
}
/* Print statistics */
fprintf (xwbMem.mReport, "Total allocations : %ld\n",
xwbMem.mAllocTotal);
fprintf (xwbMem.mReport, "Max memory allocation: %ld (%dK)\n",
xwbMem.mAllocUsed, xwbMem.mAllocUsed / 1024);
fprintf (xwbMem.mReport, "Total leak : %ld\n\n",
xwbMem.mAllocCurrent);
}
/*******************************************************************************
* Final Report
*******************************************************************************/
void XWBReportFinal (void)
{
XWBReport ("Final Report");
fclose (xwbMem.mReport);
xwbMem.mReport = 0;
}
/*******************************************************************************
* Duplicate a string
*******************************************************************************/
char* XWBStrDup (const char* iOrig, const char* iFile, const unsigned int iLine)
{
char* result;
result = XWBMalloc (strlen (iOrig) + 1, iFile, iLine);
strcpy (result, iOrig);
return result;
}
/*******************************************************************************
* Allocate a number of items of a specified size
*******************************************************************************/
void* XWBCalloc (unsigned int iNum, unsigned int iSize, const char* iFile, const unsigned int iLine)
{
void* result;
unsigned int actual = (((iSize - 1)/sizeof(int)) + 1) * sizeof (int) * iNum;
result = XWBMalloc (actual, iFile, iLine);
memset (result, 0, actual);
return result;
}