Changeset 115 for trunk/IPs/systemC/processor/Morpheo/Common
- Timestamp:
- Apr 20, 2009, 11:29:17 PM (16 years ago)
- Location:
- trunk/IPs/systemC/processor/Morpheo/Common
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/IPs/systemC/processor/Morpheo/Common/include/MemCheck.h
r112 r115 6 6 7 7 // Usurp the new operator (both scalar and array versions) 8 void* operator new (std::size_t, const char*, long);8 void* operator new (std::size_t, const char*, long); 9 9 void* operator new[](std::size_t, const char*, long); 10 10 #define new new (__FILE__, __LINE__) 11 11 12 12 extern bool traceFlag; 13 13 #define TRACE_ON() traceFlag = true -
trunk/IPs/systemC/processor/Morpheo/Common/src/MemCheck.cpp
r113 r115 1 1 #ifdef DEBUG_MEMORY_LEAK 2 2 3 #include "Common/include/Message.h" 3 4 #include <cstdio> 4 5 #include <cstdlib> 5 6 #include <cassert> 6 7 #include <cstddef> 8 #include <map> 9 7 10 using namespace std; 11 using namespace morpheo; 12 8 13 #undef new 9 14 10 15 // Global flags set by macros in MemCheck.h 11 bool traceFlag = false;16 bool traceFlag = true; 12 17 bool activeFlag = false; 13 18 14 19 namespace { 15 20 16 // Memory map entry type 17 struct Info { 18 void* ptr; 19 const char* file; 20 long line; 21 }; 21 // Memory map entry type 22 struct Info 23 { 24 // void* ptr; 25 const char* file; 26 long line; 27 size_t size; 28 }; 22 29 23 // Memory map data 24 const size_t MAXPTRS = 10000u; 25 Info memMap[MAXPTRS]; 26 size_t nptrs = 0; 30 bool Info_lt(std::pair<void *,Info> a, std::pair<void *,Info> b) 31 { 32 return a.second.size < b.second.size; 33 } 34 35 // Memory map data 36 typedef map<void *,Info> memMap_t; 37 typedef map<void *,Info>::iterator memMap_it; 27 38 28 // Searches the map for an address 29 int findPtr(void* p) { 30 for(size_t i = 0; i < nptrs; ++i) 31 if(memMap[i].ptr == p) 32 return i; 33 return -1; 39 memMap_t memMap; 40 41 // Searches the map for an address 42 memMap_it findPtr(void* p) 43 { 44 return memMap.find(p); 45 } 46 47 void delPtr(void* p) 48 { 49 memMap_it it = findPtr(p); 50 51 assert(it != memMap.end()); 52 53 // Remove pointer from map 54 memMap.erase(it); 55 } 56 57 // Dummy type for static destructor 58 struct Sentinel 59 { 60 ~Sentinel() 61 { 62 size_t size = memMap.size(); 63 if(size > 0) 64 { 65 size_t size_sum = 0; 66 msgWarning("Leaked memory at:\n"); 67 for (memMap_it it=memMap.begin(); 68 it!=memMap.end(); 69 ++it) 70 { 71 size_t s = it->second.size; 72 size_sum += s; 73 74 msgWarning("\t%p : %u bytes (file: %s, line %ld)\n", 75 it->first, 76 s, 77 it->second.file, 78 it->second.line); 79 } 80 msgWarning("Total : %u bytes on %d localisations\n",size_sum, size); 81 82 uint32_t nb_top = (size>10)?10:size; 83 msgWarning("Top %d of leaked memory\n",nb_top); 84 for (uint32_t i=0; i<nb_top; ++i) 85 { 86 memMap_it max = max_element(memMap.begin(), memMap.end(),Info_lt); 87 88 msgWarning(" * [%d] %u bytes (file: %s, line %ld)\n", 89 i, 90 max->second.size, 91 max->second.file, 92 max->second.line); 93 94 memMap.erase(max); 95 } 34 96 } 35 36 void delPtr(void* p) { 37 int pos = findPtr(p); 38 assert(pos >= 0); 39 // Remove pointer from map 40 for(size_t i = pos; i < nptrs-1; ++i) 41 memMap[i] = memMap[i+1]; 42 --nptrs; 43 } 44 45 // Dummy type for static destructor 46 struct Sentinel { 47 ~Sentinel() { 48 if(nptrs > 0) { 49 printf("Leaked memory at:\n"); 50 for(size_t i = 0; i < nptrs; ++i) 51 printf("\t%p (file: %s, line %ld)\n", 52 memMap[i].ptr, memMap[i].file, memMap[i].line); 53 } 54 else 55 printf("No user memory leaks!\n"); 56 } 57 }; 58 59 // Static dummy object 60 Sentinel s; 61 97 else 98 msgInformation("No user memory leaks!\n"); 99 } 100 }; 101 102 // Static dummy object 103 Sentinel s; 104 62 105 } // End anonymous namespace 63 106 64 107 // Overload scalar new 65 void* operator new(size_t siz, const char* file, long line) { 66 void* p = malloc(siz); 67 if(activeFlag) { 68 if(nptrs == MAXPTRS) { 69 printf("memory map too small (increase MAXPTRS)\n"); 70 exit(1); 71 } 72 memMap[nptrs].ptr = p; 73 memMap[nptrs].file = file; 74 memMap[nptrs].line = line; 75 ++nptrs; 76 } 77 if(traceFlag) { 78 printf("Allocated %u bytes at address %p ", siz, p); 79 printf("(file: %s, line: %ld)\n", file, line); 80 } 81 return p; 108 void* operator new(size_t size, const char* file, long line) 109 { 110 void* p = malloc(size); 111 112 if(activeFlag) 113 { 114 Info info; 115 info.file = file; 116 info.line = line; 117 info.size = size; 118 119 memMap[p] = info; 120 } 121 122 if(traceFlag) 123 { 124 msgInformation("Allocated %u bytes at address %p (file: %s, line: %ld)\n", size, p, file, line); 125 } 126 127 return p; 82 128 } 83 129 84 130 // Overload array new 85 void* operator new[](size_t siz, const char* file, long line) { 86 return operator new(siz, file, line); 131 void* operator new[](size_t size, const char* file, long line) 132 { 133 return operator new(size, file, line); 87 134 } 88 135 89 136 // Override scalar delete 90 void operator delete(void* p) { 91 if(findPtr(p) >= 0) { 92 free(p); 93 assert(nptrs > 0); 94 delPtr(p); 95 if(traceFlag) 96 printf("Deleted memory at address %p\n", p); 97 } 98 else if(!p && activeFlag) 99 printf("Attempt to delete unknown pointer: %p\n", p); 137 void operator delete(void* p) 138 { 139 memMap_it it = findPtr(p); 140 141 if (it != memMap.end()) 142 { 143 free(p); 144 delPtr(p); 145 146 if(traceFlag) 147 msgInformation("Deleted memory at address %p\n", p); 148 } 149 else 150 if(!p && activeFlag) 151 msgError("Attempt to delete unknown pointer: %p\n", p); 100 152 } 101 153 102 154 // Override array delete 103 void operator delete[](void* p) { 104 operator delete(p); 155 void operator delete[](void* p) 156 { 157 operator delete(p); 105 158 } 159 106 160 #endif // DEBUG_MEMORY_LEAK -
trunk/IPs/systemC/processor/Morpheo/Common/src/Message.cpp
r100 r115 6 6 */ 7 7 8 #include "Common/include/Debug.h"8 #include <sys/types.h> 9 9 #include "Common/include/Environment.h" 10 10 #include "Common/include/ErrorMorpheo.h" 11 11 #include "Common/include/Filename.h" 12 #include <sys/types.h>12 #include "Common/include/Debug.h" 13 13 14 14 namespace morpheo {
Note: See TracChangeset
for help on using the changeset viewer.