- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Estimation/Discrete/ModeEstimationPerformance/leaks.cpp

    00001 //#include <iostream>
    00002
    00003 #include <malloc.h>
    00004 #include <cstring>
    00005 #include <stdio.h>
    00006
    00007 //using namespace std;
    00008
    00009 typedef struct {
    00010         void * p;
    00011         size_t size;
    00012 } Leak;
    00013
    00014 Leak * leaks = NULL;
    00015 int numLeaks = 0;
    00016 int maxNumLeaks = 0;
    00017
    00018 size_t curStackSize = 0;
    00019 size_t maxStackSize = 0;
    00020
    00021 size_t stackSizeRef = 0;
    00022 size_t maxStackSinceMonitor = 0;
    00023
    00024 int ignoredEntries = 0;
    00025 const size_t LowerBound = 300;
    00026 const size_t UpperBound = 10000;
    00027
    00028 size_t currentStackSize(void)
    00029 {
    00030         return curStackSize;
    00031 }
    00032
    00033 void beginMonitoringStack(void)
    00034 {
    00035         stackSizeRef = curStackSize;
    00036         maxStackSinceMonitor = curStackSize;
    00037 }
    00038
    00039 size_t maxIncreaseSinceBeginMonitor(void)
    00040 {
    00041         return (maxStackSinceMonitor - stackSizeRef);
    00042 }
    00043
    00044 size_t maxStackEver(void)
    00045 {
    00046         return maxStackSize;
    00047 }
    00048
    00049
    00050 void resize()
    00051 {
    00052         if(numLeaks >= maxNumLeaks) {
    00053                 int newMaxLeaks;
    00054                 if(maxNumLeaks == 0) {
    00055                         newMaxLeaks = 2;
    00056                 } else {
    00057                         newMaxLeaks = maxNumLeaks << 1;
    00058                 }
    00059                 leaks = (Leak *)realloc(leaks, newMaxLeaks * sizeof(Leak));
    00060                 maxNumLeaks = newMaxLeaks;
    00061         }
    00062         //cout << "Resized.." << endl;
    00063 }
    00064
    00065 void * allocateNew(size_t size) throw() {
    00066         resize();
    00067         void * ptr = malloc(size);
    00068         leaks[numLeaks].p = ptr;
    00069         leaks[numLeaks].size = size;
    00070         if(size < LowerBound || size > UpperBound) {
    00071                 curStackSize += size;
    00072                 if(curStackSize > maxStackSize)
    00073                         maxStackSize = curStackSize;
    00074                 if(curStackSize > maxStackSinceMonitor)
    00075                         maxStackSinceMonitor = curStackSize;
    00076         } else {
    00077                 ignoredEntries++;
    00078         }
    00079         numLeaks++;
    00080         //fprintf(stderr, "Memory Allocated: 0x%08x of size %8d\n", ptr, size);
    00081         return ptr;
    00082 }
    00083
    00084 void deallocateNew(void * p) throw() {
    00085         if(leaks == NULL) {
    00086 //OM            fprintf(stderr, "Memory Deleted: 0x%08x\n", p);
    00087                 free(p);
    00088                 return;
    00089         }
    00090         int i;
    00091         for(i = numLeaks-1; i >= 0; i--) {
    00092                 if(leaks[i].p == p) {
    00093                         free(leaks[i].p);
    00094                         if(leaks[i].size < LowerBound || leaks[i].size > UpperBound) {
    00095                                 curStackSize -= leaks[i].size;
    00096                         } else {
    00097                                 ignoredEntries--;
    00098 //OM                            fprintf(stderr, "Warning, Memory allocation @ 0x%08x of size %d was "
    00099 //                                                              "ignored but deleted\n", leaks[i].p, leaks[i].size);
    00100                         }
    00101                         break;
    00102                 }
    00103         }
    00104         if(i < 0) {
    00105                 if(p != NULL) {
    00106 //OM                    printf("LEAK TRACE ERROR: Couldn't find memory to release!  Address error: 0x%08x\n", p);
    00107                 } // At present, there's a bug in VC++ such that it deletes NULL pointers in the Standard Template Library
    00108         } else
    00109         {
    00110                 memmove(leaks + i, leaks + i + 1, sizeof(Leak) * (numLeaks - i - 1));
    00111                 numLeaks--;
    00112         }
    00113         //cout << "Deleted.." << endl;
    00114 }
    00115
    00116 void* operator new(size_t size) throw () {
    00117         return allocateNew(size);
    00118 }
    00119
    00120
    00121 void* operator new[] (size_t size) throw () {
    00122         return allocateNew(size);
    00123 }
    00124
    00125 void operator delete (void *p) throw () {
    00126         deallocateNew(p);
    00127 }
    00128
    00129 void operator delete[] (void *p) throw () {
    00130         deallocateNew(p);
    00131 }
    00132
    00133
    00134 class CleanupLeaks {
    00135 public:
    00136         CleanupLeaks() {
    00137         }
    00138
    00139         virtual ~CleanupLeaks() {
    00140                 if(leaks != NULL) {
    00141                         for(int i = 0; i < numLeaks; i++) {
    00142 //OM                            fprintf(stderr, "Leak: @ 0x%08x of size %8d\n",
    00143 //                                      leaks[i].p, leaks[i].size);
    00144                         }
    00145                         free(leaks);
    00146                         leaks = NULL;
    00147                         if(ignoredEntries > 0) {
    00148 //OM                            fprintf(stderr, "Warning, ignored %d allocations\n", ignoredEntries);
    00149                         }
    00150                 }
    00151         }
    00152 } cleanupLeaks;