~ubuntu-branches/ubuntu/wily/ntop/wily-proposed

« back to all changes in this revision

Viewing changes to ntop/leaks.c

  • Committer: Bazaar Package Importer
  • Author(s): Dennis Schoen
  • Date: 2002-04-12 11:38:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412113847-4k4yydw0pzybc6g8
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "ntop.h"
 
2
 
 
3
 
 
4
#ifdef MEMORY_DEBUG 
 
5
 
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
 
 
9
#undef malloc
 
10
#undef free
 
11
#undef strdup
 
12
 
 
13
typedef struct memoryBlock {
 
14
  void*               memoryLocation;   /* Malloc address              */
 
15
  size_t              blockSize;        /* Block size                  */
 
16
  char*               programLocation;  /* Program address: file, line */
 
17
  struct memoryBlock* nextBlock;        /* Next memory block           */
 
18
  short alreadyTraced;
 
19
} MemoryBlock;
 
20
 
 
21
static MemoryBlock *theRoot = NULL;
 
22
static char tmpStr[255];
 
23
 
 
24
#ifdef MULTITHREADED
 
25
static PthreadMutex leaksMutex;
 
26
#endif
 
27
 
 
28
unsigned int PrintMemoryBlocks(); /* Forward declaration */
 
29
 
 
30
/* *************************************** */
 
31
 
 
32
void* myMalloc(size_t theSize, int theLine, char* theFile) {
 
33
  MemoryBlock *tmpBlock;
 
34
 
 
35
#if defined(MULTITHREADED)
 
36
  accessMutex(&leaksMutex, "myMalloc");
 
37
#endif
 
38
 
 
39
  tmpBlock = (MemoryBlock*)malloc(sizeof(MemoryBlock));
 
40
 
 
41
  if(tmpBlock == NULL) {
 
42
    traceEvent(TRACE_WARNING, "Malloc error (not enough memory): %s, %d\n", 
 
43
            theFile, theLine);
 
44
 
 
45
#if defined(MULTITHREADED)
 
46
    releaseMutex(&leaksMutex);
 
47
#endif
 
48
    return(NULL);
 
49
  }
 
50
  
 
51
  tmpBlock->blockSize = theSize;
 
52
  tmpBlock->memoryLocation = malloc(theSize);
 
53
  memset(tmpBlock->memoryLocation, 0xee, theSize); /* Fill it with garbage */
 
54
  tmpBlock->alreadyTraced = 0;
 
55
 
 
56
  allocatedMemory += theSize;
 
57
 
 
58
  if(tmpBlock->memoryLocation == NULL) {
 
59
    traceEvent(TRACE_WARNING, "Malloc error (not enough memory): %s, %d (size = %d)\n", 
 
60
            theFile, theLine, (int)theSize);
 
61
#if defined(MULTITHREADED)
 
62
    releaseMutex(&leaksMutex);
 
63
#endif
 
64
    return(NULL);
 
65
  }
 
66
 
 
67
  if(snprintf(tmpStr, sizeof(tmpStr), "file %s, line %d.", theFile, theLine) < 0)
 
68
    traceEvent(TRACE_ERROR, "Buffer overflow!");
 
69
  tmpBlock->programLocation = strdup(tmpStr);
 
70
  tmpBlock->nextBlock = theRoot;
 
71
  theRoot = tmpBlock;
 
72
 
 
73
#if defined(MULTITHREADED)
 
74
  releaseMutex(&leaksMutex);
 
75
#endif
 
76
 
 
77
  return(tmpBlock->memoryLocation);
 
78
}
 
79
 
 
80
/* *************************************** */
 
81
 
 
82
void* myCalloc(size_t numObj, size_t theSize, int theLine, char* theFile) {
 
83
  int numElems = numObj*theSize;
 
84
  void* thePtr = myMalloc(numElems, theLine, theFile);
 
85
 
 
86
  if(thePtr != NULL)
 
87
    memset(thePtr, '\0', numElems);
 
88
 
 
89
  return(thePtr);
 
90
}
 
91
 
 
92
/* *************************************** */
 
93
 
 
94
void* myRealloc(void* thePtr, size_t theSize, int theLine, char* theFile) {
 
95
  MemoryBlock *theScan, *lastPtr, *theNewPtr;
 
96
  
 
97
#if defined(MULTITHREADED)
 
98
  accessMutex(&leaksMutex, "myRealloc");
 
99
#endif
 
100
 
 
101
  theScan = theRoot;
 
102
 
 
103
  while((theScan != NULL) && (theScan->memoryLocation != thePtr)) {
 
104
    lastPtr = theScan;
 
105
    theScan = theScan->nextBlock;
 
106
  }
 
107
 
 
108
  if(theScan == NULL) {
 
109
    traceEvent(TRACE_WARNING, "Realloc error (Ptr %p NOT allocated): %s, %d\n", 
 
110
            thePtr, theFile, theLine);
 
111
#if defined(MULTITHREADED)
 
112
    releaseMutex(&leaksMutex);
 
113
#endif
 
114
    return(NULL);
 
115
  } else {    
 
116
    theNewPtr = myMalloc(theSize, theLine, theFile);
 
117
      
 
118
    if(theSize > theScan->blockSize)
 
119
      memcpy(theNewPtr, thePtr, theScan->blockSize);
 
120
    else
 
121
      memcpy(theNewPtr, thePtr, theSize);
 
122
        
 
123
    free(theScan->memoryLocation);
 
124
    free(theScan->programLocation);
 
125
      
 
126
    if(theScan == theRoot)
 
127
      theRoot = theRoot->nextBlock;
 
128
    else
 
129
      lastPtr->nextBlock = theScan->nextBlock;
 
130
 
 
131
    free(theScan);     
 
132
 
 
133
#if defined(MULTITHREADED)
 
134
    releaseMutex(&leaksMutex);
 
135
#endif
 
136
 
 
137
    return(theNewPtr);
 
138
  }
 
139
}
 
140
 
 
141
/* *************************************** */
 
142
 
 
143
void myFree(void **thePtr, int theLine, char* theFile) {
 
144
  MemoryBlock *theScan, *lastPtr;
 
145
  
 
146
#if defined(MULTITHREADED)
 
147
  accessMutex(&leaksMutex, "myFree");
 
148
#endif
 
149
 
 
150
  theScan = theRoot;
 
151
 
 
152
  while((theScan != NULL) && (theScan->memoryLocation != *thePtr)) {
 
153
    lastPtr = theScan;
 
154
    theScan = theScan->nextBlock;
 
155
  }
 
156
 
 
157
  if(theScan == NULL) {
 
158
    traceEvent(TRACE_WARNING, "Free error (Ptr %p NOT allocated): %s, %d\n", 
 
159
               *thePtr, theFile, theLine);
 
160
#if defined(MULTITHREADED)
 
161
    releaseMutex(&leaksMutex);
 
162
#endif
 
163
    return;
 
164
  } else {
 
165
    allocatedMemory -= theScan->blockSize;
 
166
 
 
167
    free(theScan->memoryLocation);
 
168
    free(theScan->programLocation);
 
169
 
 
170
    if(theScan == theRoot)
 
171
      theRoot = theRoot->nextBlock;
 
172
    else
 
173
      lastPtr->nextBlock = theScan->nextBlock;
 
174
 
 
175
    free(theScan);
 
176
    *thePtr = NULL;
 
177
  }
 
178
 
 
179
#if defined(MULTITHREADED)
 
180
  releaseMutex(&leaksMutex);
 
181
#endif
 
182
}
 
183
 
 
184
/* *************************************** */
 
185
 
 
186
char* myStrdup(char* theStr, int theLine, char* theFile) {
 
187
  char* theOut;
 
188
  int len = strlen(theStr);
 
189
  
 
190
  theOut = (char*)myMalloc((len+1), theLine, theFile);
 
191
  strncpy(theOut, theStr, len);
 
192
  theOut[len] = '\0';
 
193
 
 
194
  return(theOut);
 
195
}
 
196
 
 
197
/* *************************************** */
 
198
 
 
199
void resetLeaks(void) {
 
200
  MemoryBlock *theScan;
 
201
 
 
202
  theScan = theRoot;
 
203
 
 
204
  while(theScan != NULL) {
 
205
    theScan->alreadyTraced = 1;
 
206
    theScan = theScan->nextBlock;
 
207
  }
 
208
 
 
209
  allocatedMemory = 0; /* Reset counter */
 
210
}
 
211
 
 
212
/* *************************************** */
 
213
 
 
214
unsigned int PrintMemoryBlocks(void) {
 
215
  MemoryBlock *theScan;
 
216
  int i = 0;
 
217
  unsigned int totMem = 0;
 
218
 
 
219
  theScan = theRoot;
 
220
 
 
221
  while(theScan != NULL) {
 
222
    MemoryBlock* tmp;
 
223
 
 
224
    if(!theScan->alreadyTraced) {
 
225
      traceEvent(TRACE_INFO,"Block %5d (addr %p, size %4d): %s\n", i++, 
 
226
              theScan->memoryLocation, theScan->blockSize, theScan->programLocation);
 
227
      totMem += theScan->blockSize;
 
228
    }
 
229
 
 
230
    theScan->alreadyTraced = 1;
 
231
    tmp = theScan->memoryLocation;
 
232
    theScan = theScan->nextBlock;
 
233
  }
 
234
 
 
235
  traceEvent(TRACE_INFO,"\nTotal allocated memory: %u bytes\n\n", totMem);
 
236
 
 
237
  /* PrintMemoryBlocks(); */
 
238
 
 
239
  return(totMem);
 
240
}
 
241
 
 
242
/* *************************************** */
 
243
 
 
244
size_t GimmePointerSize(void* thePtr) {
 
245
  MemoryBlock *theScan;
 
246
  
 
247
  theScan = theRoot;
 
248
 
 
249
  while((theScan != NULL) && (theScan->memoryLocation != thePtr))
 
250
    theScan = theScan->nextBlock;
 
251
 
 
252
  if(theScan == NULL) {
 
253
    traceEvent(TRACE_WARNING, "GimmePointerSize error: Ptr %p NOT allocated\n", thePtr);
 
254
    return(-1);
 
255
  } else
 
256
    return(theScan->blockSize);
 
257
}
 
258
 
 
259
/* *************************************** */
 
260
 
 
261
int GimmePointerInfo(void* thePtr) {
 
262
  MemoryBlock *theScan;
 
263
  
 
264
  theScan = theRoot;
 
265
 
 
266
  while((theScan != NULL) && (theScan->memoryLocation != thePtr))
 
267
    theScan = theScan->nextBlock;
 
268
 
 
269
  if(theScan == NULL) {
 
270
    traceEvent(TRACE_WARNING, "GimmePointerInfo error: Ptr %p NOT allocated\n", thePtr);
 
271
    return -1;
 
272
  } else {      
 
273
    traceEvent(TRACE_WARNING, "Block (addr %p, size %d): %s\n", theScan->memoryLocation, 
 
274
            theScan->blockSize, theScan->programLocation);
 
275
    return 0;
 
276
  }
 
277
}
 
278
 
 
279
/* *************************************** */
 
280
 
 
281
void myAddLeak(void* thePtr, int theLine, char* theFile) {
 
282
  MemoryBlock *tmpBlock;
 
283
 
 
284
  if(thePtr == NULL) 
 
285
    return;
 
286
 
 
287
  tmpBlock = (MemoryBlock*)malloc(sizeof(MemoryBlock));
 
288
 
 
289
  if(tmpBlock == NULL) {
 
290
    traceEvent(TRACE_WARNING, "Malloc error (not enough memory): %s, %d\n", 
 
291
            theFile, theLine);
 
292
    return;
 
293
  }
 
294
  
 
295
  tmpBlock->blockSize = 0;
 
296
  tmpBlock->memoryLocation = thePtr;
 
297
  if(snprintf(tmpStr, sizeof(tmpStr), "file %s, line %d.", theFile, theLine) < 0)
 
298
    traceEvent(TRACE_ERROR, "Buffer overflow!");
 
299
  tmpBlock->programLocation = strdup(tmpStr);
 
300
  tmpBlock->nextBlock = theRoot;
 
301
  theRoot = tmpBlock;
 
302
}
 
303
 
 
304
/* *************************************** */
 
305
 
 
306
void myRemoveLeak(void* thePtr, int theLine, char* theFile) {
 
307
  MemoryBlock *theScan, *lastPtr;
 
308
  
 
309
  theScan = theRoot;
 
310
 
 
311
  while((theScan != NULL) && (theScan->memoryLocation != thePtr)) {
 
312
    lastPtr = theScan;
 
313
    theScan = theScan->nextBlock;
 
314
  }
 
315
 
 
316
  if(theScan == NULL) {
 
317
    traceEvent(TRACE_WARNING, "Free  block error (Ptr %p NOT allocated): %s, %d\n", 
 
318
               thePtr, theFile, theLine);
 
319
    return;
 
320
  } else {
 
321
    free(theScan->programLocation);
 
322
    
 
323
    if(theScan == theRoot)
 
324
      theRoot = theRoot->nextBlock;
 
325
    else
 
326
      lastPtr->nextBlock = theScan->nextBlock;
 
327
 
 
328
    free(theScan);
 
329
  }
 
330
}
 
331
 
 
332
/* *************************************** */
 
333
 
 
334
void initLeaks(void) {
 
335
#ifdef MULTITHREADED
 
336
  createMutex(&leaksMutex);
 
337
#endif
 
338
}
 
339
 
 
340
/* *************************************** */
 
341
 
 
342
void termLeaks(void) {
 
343
  PrintMemoryBlocks();
 
344
#ifdef MULTITHREADED
 
345
  deleteMutex(&leaksMutex);
 
346
#endif
 
347
}
 
348
 
 
349
/* ************************************ */
 
350
 
 
351
void* ntop_malloc(unsigned int sz, char* file, int line) {
 
352
 
 
353
#ifdef DEBUG
 
354
  traceEvent(TRACE_WARNING, "malloc(%d) [%s] @ %s:%d", 
 
355
             sz, formatBytes(allocatedMemory, 0), file, line);
 
356
#endif
 
357
 
 
358
  return(myMalloc(sz, line, file));
 
359
}
 
360
 
 
361
/* ************************************ */
 
362
 
 
363
void* ntop_calloc(unsigned int c, unsigned int sz, char* file, int line) {
 
364
#ifdef DEBUG
 
365
  traceEvent(TRACE_WARNING, "calloc(%d,%d) [%s] @ %s:%d",
 
366
             c, sz, formatBytes(allocatedMemory, 0), file, line);
 
367
#endif
 
368
  return(myCalloc(c, sz, line, file));
 
369
}
 
370
 
 
371
/* ************************************ */
 
372
 
 
373
void* ntop_realloc(void* ptr, unsigned int sz, char* file, int line) {  
 
374
#ifdef DEBUG
 
375
  traceEvent(TRACE_WARNING, "realloc(%p,%d) [%s] @ %s:%d",
 
376
             ptr, sz, formatBytes(allocatedMemory, 0), file, line);
 
377
#endif  
 
378
  return(myRealloc(ptr, sz, line, file));
 
379
}
 
380
 
 
381
/* ************************************ */
 
382
 
 
383
char* ntop_strdup(char *str, char* file, int line) {
 
384
#ifdef DEBUG
 
385
  traceEvent(TRACE_WARNING, "strdup(%s) [%s] @ %s:%d", str, 
 
386
             formatBytes(allocatedMemory, 0), file, line);
 
387
#endif
 
388
  return(myStrdup(str, line, file));
 
389
}
 
390
 
 
391
/* ************************************ */
 
392
 
 
393
void ntop_free(void **ptr, char* file, int line) {
 
394
#ifdef DEBUG
 
395
  traceEvent(TRACE_WARNING, "free(%x) [%s] @ %s:%d", ptr, 
 
396
             formatBytes(allocatedMemory, 0), file, line);
 
397
#endif
 
398
  myFree(ptr, line, file);
 
399
}
 
400
 
 
401
/* ****************************************** */
 
402
 
 
403
#else /* MEMORY_DEBUG */
 
404
 
 
405
/* ****************************************** */
 
406
 
 
407
#undef malloc /* just to be safe */
 
408
void* ntop_safemalloc(unsigned int sz, char* file, int line) {
 
409
  void *thePtr;
 
410
  
 
411
#ifdef DEBUG
 
412
  if((sz == 0) || (sz > 32768)) {
 
413
    traceEvent(TRACE_WARNING, "WARNING: called malloc(%u) @ %s:%d", 
 
414
               sz, file, line);
 
415
  }
 
416
#endif
 
417
 
 
418
  thePtr = malloc(sz);
 
419
  memset(thePtr, 0xee, sz); /* Fill it with garbage */
 
420
  return(thePtr);
 
421
}
 
422
 
 
423
/* ****************************************** */
 
424
 
 
425
/* Courtesy of Wies-Software <wies@wiessoft.de> */
 
426
#undef calloc /* just to be safe */
 
427
void* ntop_safecalloc(unsigned int c, unsigned int sz, char* file, int line) {  
 
428
  void *thePtr;
 
429
  
 
430
#ifdef DEBUG
 
431
  if((sz == 0) || (sz > 32768)) {
 
432
    traceEvent(TRACE_WARNING, "WARNING: called calloc(%u,%u) @ %s:%d",
 
433
               c, sz, file, line);
 
434
  }
 
435
#endif
 
436
  
 
437
  thePtr = calloc(c, sz);
 
438
  return(thePtr);
 
439
}
 
440
 
 
441
/* ****************************************** */
 
442
 
 
443
/* Courtesy of Wies-Software <wies@wiessoft.de> */
 
444
#undef realloc /* just to be safe */
 
445
void* ntop_saferealloc(void* ptr, unsigned int sz, char* file, int line) {
 
446
  void *thePtr;
 
447
  
 
448
#ifdef DEBUG
 
449
  if((sz == 0) || (sz > 32768)) {
 
450
    traceEvent(TRACE_WARNING, "WARNING: called realloc(%p,%u) @ %s:%d",
 
451
               ptr, sz, file, line);
 
452
  }
 
453
#endif
 
454
  
 
455
  thePtr = realloc(ptr, sz);
 
456
  return(thePtr);
 
457
}
 
458
 
 
459
/* ****************************************** */
 
460
 
 
461
#undef free /* just to be safe */
 
462
void ntop_safefree(void **ptr, char* file, int line) {
 
463
 
 
464
  if((ptr == NULL) || (*ptr == NULL)) {
 
465
    traceEvent(TRACE_WARNING, "WARNING: free of NULL pointer @ %s:%d", 
 
466
               file, line);
 
467
  } else {
 
468
    free(*ptr);
 
469
    *ptr = NULL;
 
470
  }
 
471
}
 
472
 
 
473
#endif /* MEMORY_DEBUG */