Changeset 1775d3 for molecuilder/src/Helpers
- Timestamp:
- Apr 30, 2010, 8:01:18 AM (15 years ago)
- Children:
- 8c01ce
- Parents:
- 070651 (diff), c53e0b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- molecuilder/src/Helpers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/Helpers/MemDebug.cpp
r070651 r1775d3 8 8 #include <iostream> 9 9 #include <cstdlib> 10 #include <cstring> 10 11 11 12 using namespace std; 12 13 13 14 namespace Memory { 15 16 // This struct is added before each memory chunk 17 // and contains tracking information. Anything used 18 // to track memory cannot use any dynamic memory, so 19 // we have to resort to classic C-idioms here. 20 // This struct also contains pointers to the next 21 // an previous chunks to allow fast traversion of 22 // all allocated memory blocks 14 23 struct entry_t { 24 // we seperate the tracking info from the rest 25 // A checksum will be calculated for this part of 26 // the struct, so the information in here should 27 // not change during the lifetime of the memory 15 28 struct info_t { 16 char file[256]; 29 enum {length = 64}; 30 char file[length+1]; 17 31 int line; 18 32 size_t nbytes; … … 26 40 }; 27 41 42 // start and end of the doubly-linked list 28 43 entry_t *begin=0; 29 44 entry_t *end=0; 30 45 46 // current amount of allocated memory 31 47 size_t state = 0; 48 // maximum amount of allocated memory 32 49 size_t max = 0; 33 50 // number of allocations that have been done so far 51 unsigned int allocs = 0; 52 53 54 // this sets the alignment of the returned memory block 55 // malloc guarantees an alignment at the 8 byte border, 56 // so we just do the same 34 57 const int alignment = 8; 35 58 59 // calculates a simple checksum for the info block 60 // the checksum is used to find memory corruptions 36 61 inline char calcChecksum(entry_t::info_t *info){ 37 62 char *buffer = (char*)info; … … 43 68 } 44 69 70 // gets the next alignet point which is greater than nbytes 71 // this function is only called a fixed number of times, so 72 // there is no need to optimize 45 73 inline size_t doAlign(size_t nbytes){ 46 74 int nonaligned = nbytes % alignment; … … 53 81 } 54 82 83 // Output some state information 55 84 void getState(){ 56 85 cout << "Maximum allocated Memory: " << max << " bytes" << endl; 57 86 cout << "Currently allocated Memory: " << state <<" bytes" << endl; 58 87 cout << allocs << " allocated chunks total" << endl; 88 89 // simple traversal of the chunk list 59 90 for(entry_t *pos=begin;pos;pos=pos->next){ 60 91 cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl; … … 63 94 } 64 95 96 // Deletes an entry from the linked list 65 97 void deleteEntry(entry_t *entry){ 66 98 if(entry->isIgnored) 67 99 return; 100 68 101 if(entry->prev){ 69 102 entry->prev->next = entry->next; 70 103 } 71 104 else{ 105 // this node was the beginning of the list 72 106 begin = entry->next; 73 107 } … … 77 111 } 78 112 else{ 113 // this node was the end of the list 79 114 end = entry->prev; 80 115 } … … 84 119 85 120 void _ignore(void *ptr){ 121 // just deletes the node from the list, but leaves the info intact 86 122 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t)); 87 123 entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace); … … 92 128 void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) { 93 129 130 // to avoid allocations of 0 bytes if someone screws up 131 // allocation with 0 byte size are undefined behavior, so we are 132 // free to handle it this way 94 133 if(!nbytes) { 95 134 nbytes = 1; 96 135 } 97 136 137 // get the size of the entry, including alignment 98 138 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t)); 99 139 100 140 void *res; 101 141 if(!(res=malloc(entrySpace + nbytes))){ 142 // new must throw, when space is low 102 143 throw std::bad_alloc(); 103 144 } 104 145 146 // we got the space, so update the global info 105 147 Memory::state += nbytes; 106 148 if(Memory::state>Memory::max){ 107 149 Memory::max = Memory::state; 108 150 } 109 151 Memory::allocs++; 152 153 // build the entry in front of the space 110 154 Memory::entry_t *entry = (Memory::entry_t*) res; 111 155 entry->info.nbytes = nbytes; 112 156 entry->info.isUsed = true; 113 strncpy(entry->info.file,file, 256);114 entry->info.file[ 255] = '\0';157 strncpy(entry->info.file,file,Memory::entry_t::info_t::length); 158 entry->info.file[Memory::entry_t::info_t::length] = '\0'; 115 159 entry->info.line=line; 160 // the space starts behind the info 116 161 entry->info.location = (char*)res + entrySpace; 117 162 118 entry->next=0; 119 entry->prev=Memory::end; 163 // add the entry at the end of the list 164 entry->next=0; // the created block is last in the list 165 entry->prev=Memory::end; // the created block is last in the list 120 166 if(!Memory::begin){ 167 // the list was empty... start a new one 121 168 Memory::begin=entry; 122 169 } 123 170 else { 171 // other blocks present... we can add to the last one 124 172 Memory::end->next=entry; 125 173 } 126 174 Memory::end=entry; 127 175 176 // get the checksum... 128 177 entry->checksum = Memory::calcChecksum(&entry->info); 178 // this will be set to true, when the block is removed from 179 // the list for any reason 129 180 entry->isIgnored = false; 130 181 182 // ok, space is prepared... the user can have it. 183 // the rest (constructor, deleting when something is thrown etc) 184 // is handled automatically 131 185 return entry->info.location; 132 186 } 133 187 134 188 void *operator new(size_t nbytes) throw(std::bad_alloc) { 189 // Just forward to the other operator, when we do not know from 190 // where the allocation came 135 191 return operator new(nbytes,"Unknown",0); 136 192 } 137 193 138 194 void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc) { 195 // The difference between new and new[] is just for compiler bookkeeping. 139 196 return operator new(nbytes,file,line); 140 197 } 141 198 142 199 void *operator new[] (size_t nbytes) throw(std::bad_alloc) { 200 // Forward again 143 201 return operator new[] (nbytes,"Unknown",0); 144 202 } 145 203 146 204 void operator delete(void *ptr) throw() { 205 // get the size for the entry, including alignment 147 206 static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t)); 148 207 208 // get the position for the entry from the pointer the user gave us 149 209 Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace); 150 210 211 // let's see if the checksum is still matching 151 212 if(Memory::calcChecksum(&entry->info)!=entry->checksum){ 152 213 cout << "Possible memory corruption detected!" << endl; … … 156 217 } 157 218 219 // this will destroy the checksum, so double deletes are caught 158 220 entry->info.isUsed = false; 159 221 Memory::deleteEntry(entry); 160 222 223 // delete the space reserved by malloc 161 224 free((char*)ptr-entrySpace); 162 225 } 163 226 227 // operator that is called when the constructor throws 228 // do not call manually 164 229 void operator delete(void *ptr,const char*, int) throw() { 165 230 operator delete(ptr); … … 167 232 168 233 void operator delete[](void *ptr){ 234 // again difference between delete and delete[] is just in compiler bookkeeping 169 235 operator delete(ptr); 170 236 } 171 237 238 // and another operator that can be called when a constructor throws 172 239 void operator delete[](void *ptr,const char*, int) throw(){ 173 240 operator delete(ptr); -
molecuilder/src/Helpers/MemDebug.hpp
r070651 r1775d3 9 9 #define MEMDEBUG_HPP_ 10 10 11 /** 12 * @file 13 * This module allows easy automatic memory tracking. Link this module to replace the 14 * operators new, new[], delete and delete[] with custom versions that add tracking 15 * information to allocated blocks. 16 * 17 * All tracking is done in O(1) for new and delete operators. Summaries for the 18 * used memory take O(N), where N is the number of currently allocated memory chunks. 19 * 20 * To use full tracking including file name and line number include this file in 21 * your sourcefiles. 22 */ 23 11 24 namespace Memory { 25 26 /** 27 * Displays a short summary of the memory state. 28 */ 12 29 void getState(); 13 30 14 31 void _ignore(void*); 15 32 33 /** 34 * Use this to disable memory for a certain pointer on the heap. 35 * This is useful for pointers which should live over the run of the 36 * program, and which are deleted automatically at the end. Usually these 37 * pointers should be wrapped inside some kind of smart pointer to 38 * ensure destruction at the end. 39 */ 16 40 template <typename T> 17 41 T *ignore(T* ptr){ … … 26 50 void operator delete[] (void *ptr,const char*, int) throw(); 27 51 28 52 /** 53 * This macro replaces all occurences of the keyword new with a replaced 54 * version that allows tracking. 55 */ 29 56 #define new new(__FILE__,__LINE__) 30 57
Note:
See TracChangeset
for help on using the changeset viewer.