| [632bc3] | 1 | /* | 
|---|
|  | 2 | * MemDebug.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Apr 28, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #include <iostream> | 
|---|
|  | 9 | #include <cstdlib> | 
|---|
| [28c351] | 10 | #include <cstring> | 
|---|
| [632bc3] | 11 |  | 
|---|
|  | 12 | using namespace std; | 
|---|
|  | 13 |  | 
|---|
|  | 14 | namespace Memory { | 
|---|
| [d79639] | 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 | 
|---|
| [632bc3] | 23 | struct entry_t { | 
|---|
| [d79639] | 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 | 
|---|
| [632bc3] | 28 | struct info_t { | 
|---|
| [d79639] | 29 | enum {length = 64}; | 
|---|
| [1f7864] | 30 | char file[length+1]; | 
|---|
| [632bc3] | 31 | int line; | 
|---|
|  | 32 | size_t nbytes; | 
|---|
|  | 33 | bool isUsed; | 
|---|
|  | 34 | void *location; | 
|---|
|  | 35 | } info; | 
|---|
|  | 36 | bool isIgnored; | 
|---|
|  | 37 | char checksum; | 
|---|
|  | 38 | entry_t *prev; | 
|---|
|  | 39 | entry_t *next; | 
|---|
|  | 40 | }; | 
|---|
|  | 41 |  | 
|---|
| [d79639] | 42 | // start and end of the doubly-linked list | 
|---|
| [632bc3] | 43 | entry_t *begin=0; | 
|---|
|  | 44 | entry_t *end=0; | 
|---|
|  | 45 |  | 
|---|
| [d79639] | 46 | // current amount of allocated memory | 
|---|
| [632bc3] | 47 | size_t state = 0; | 
|---|
| [d79639] | 48 | // maximum amount of allocated memory | 
|---|
| [632bc3] | 49 | size_t max = 0; | 
|---|
| [d79639] | 50 | // number of allocations that have been done so far | 
|---|
|  | 51 | unsigned int allocs = 0; | 
|---|
|  | 52 |  | 
|---|
| [632bc3] | 53 |  | 
|---|
| [d79639] | 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 | 
|---|
| [632bc3] | 57 | const int alignment = 8; | 
|---|
|  | 58 |  | 
|---|
| [d79639] | 59 | // calculates a simple checksum for the info block | 
|---|
|  | 60 | // the checksum is used to find memory corruptions | 
|---|
| [632bc3] | 61 | inline char calcChecksum(entry_t::info_t *info){ | 
|---|
|  | 62 | char *buffer = (char*)info; | 
|---|
|  | 63 | char checksum =0; | 
|---|
|  | 64 | for(size_t i=0;i<sizeof(entry_t::info_t);i++){ | 
|---|
|  | 65 | checksum+=buffer[i]; | 
|---|
|  | 66 | } | 
|---|
|  | 67 | return checksum; | 
|---|
|  | 68 | } | 
|---|
|  | 69 |  | 
|---|
| [d79639] | 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 | 
|---|
| [632bc3] | 73 | inline size_t doAlign(size_t nbytes){ | 
|---|
|  | 74 | int nonaligned = nbytes % alignment; | 
|---|
|  | 75 | if(nonaligned) { | 
|---|
|  | 76 | return(nbytes - nonaligned + alignment); | 
|---|
|  | 77 | } | 
|---|
|  | 78 | else{ | 
|---|
|  | 79 | return nbytes; | 
|---|
|  | 80 | } | 
|---|
|  | 81 | } | 
|---|
|  | 82 |  | 
|---|
| [d79639] | 83 | // Output some state information | 
|---|
| [632bc3] | 84 | void getState(){ | 
|---|
|  | 85 | cout << "Maximum allocated Memory: " << max << " bytes" << endl; | 
|---|
|  | 86 | cout << "Currently allocated Memory: " << state <<" bytes" << endl; | 
|---|
| [d79639] | 87 | cout << allocs << " allocated chunks total" << endl; | 
|---|
| [632bc3] | 88 |  | 
|---|
| [d79639] | 89 | // simple traversal of the chunk list | 
|---|
| [632bc3] | 90 | for(entry_t *pos=begin;pos;pos=pos->next){ | 
|---|
|  | 91 | cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl; | 
|---|
|  | 92 | cout << "Chunk reserved at: " << pos->info.file << ":" << pos->info.line << endl; | 
|---|
|  | 93 | } | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
| [d79639] | 96 | // Deletes an entry from the linked list | 
|---|
| [632bc3] | 97 | void deleteEntry(entry_t *entry){ | 
|---|
|  | 98 | if(entry->isIgnored) | 
|---|
|  | 99 | return; | 
|---|
| [d79639] | 100 |  | 
|---|
| [632bc3] | 101 | if(entry->prev){ | 
|---|
|  | 102 | entry->prev->next = entry->next; | 
|---|
|  | 103 | } | 
|---|
|  | 104 | else{ | 
|---|
| [d79639] | 105 | // this node was the beginning of the list | 
|---|
| [632bc3] | 106 | begin = entry->next; | 
|---|
|  | 107 | } | 
|---|
|  | 108 |  | 
|---|
|  | 109 | if(entry->next){ | 
|---|
|  | 110 | entry->next->prev = entry->prev; | 
|---|
|  | 111 | } | 
|---|
|  | 112 | else{ | 
|---|
| [d79639] | 113 | // this node was the end of the list | 
|---|
| [632bc3] | 114 | end = entry->prev; | 
|---|
|  | 115 | } | 
|---|
|  | 116 | entry->isIgnored = true; | 
|---|
|  | 117 | Memory::state  -= entry->info.nbytes; | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 | void _ignore(void *ptr){ | 
|---|
| [d79639] | 121 | // just deletes the node from the list, but leaves the info intact | 
|---|
| [632bc3] | 122 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t)); | 
|---|
|  | 123 | entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace); | 
|---|
|  | 124 | deleteEntry(entry); | 
|---|
|  | 125 | } | 
|---|
|  | 126 | } | 
|---|
|  | 127 |  | 
|---|
|  | 128 | void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) { | 
|---|
|  | 129 |  | 
|---|
| [d79639] | 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 | 
|---|
| [632bc3] | 133 | if(!nbytes) { | 
|---|
|  | 134 | nbytes = 1; | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [d79639] | 137 | // get the size of the entry, including alignment | 
|---|
| [632bc3] | 138 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t)); | 
|---|
|  | 139 |  | 
|---|
|  | 140 | void *res; | 
|---|
|  | 141 | if(!(res=malloc(entrySpace + nbytes))){ | 
|---|
| [d79639] | 142 | // new must throw, when space is low | 
|---|
| [632bc3] | 143 | throw std::bad_alloc(); | 
|---|
|  | 144 | } | 
|---|
|  | 145 |  | 
|---|
| [d79639] | 146 | // we got the space, so update the global info | 
|---|
| [632bc3] | 147 | Memory::state += nbytes; | 
|---|
|  | 148 | if(Memory::state>Memory::max){ | 
|---|
|  | 149 | Memory::max = Memory::state; | 
|---|
|  | 150 | } | 
|---|
| [d79639] | 151 | Memory::allocs++; | 
|---|
| [632bc3] | 152 |  | 
|---|
| [d79639] | 153 | // build the entry in front of the space | 
|---|
| [632bc3] | 154 | Memory::entry_t *entry = (Memory::entry_t*) res; | 
|---|
| [68f03d] | 155 | memset(res,0,entrySpace); | 
|---|
| [632bc3] | 156 | entry->info.nbytes = nbytes; | 
|---|
|  | 157 | entry->info.isUsed = true; | 
|---|
| [d79639] | 158 | strncpy(entry->info.file,file,Memory::entry_t::info_t::length); | 
|---|
| [1f7864] | 159 | entry->info.file[Memory::entry_t::info_t::length] = '\0'; | 
|---|
| [632bc3] | 160 | entry->info.line=line; | 
|---|
| [d79639] | 161 | // the space starts behind the info | 
|---|
| [632bc3] | 162 | entry->info.location = (char*)res + entrySpace; | 
|---|
|  | 163 |  | 
|---|
| [d79639] | 164 | // add the entry at the end of the list | 
|---|
|  | 165 | entry->next=0;            // the created block is last in the list | 
|---|
|  | 166 | entry->prev=Memory::end;  // the created block is last in the list | 
|---|
| [632bc3] | 167 | if(!Memory::begin){ | 
|---|
| [d79639] | 168 | // the list was empty... start a new one | 
|---|
| [632bc3] | 169 | Memory::begin=entry; | 
|---|
|  | 170 | } | 
|---|
|  | 171 | else { | 
|---|
| [d79639] | 172 | // other blocks present... we can add to the last one | 
|---|
| [632bc3] | 173 | Memory::end->next=entry; | 
|---|
|  | 174 | } | 
|---|
|  | 175 | Memory::end=entry; | 
|---|
|  | 176 |  | 
|---|
| [d79639] | 177 | // get the checksum... | 
|---|
| [632bc3] | 178 | entry->checksum = Memory::calcChecksum(&entry->info); | 
|---|
| [d79639] | 179 | // this will be set to true, when the block is removed from | 
|---|
|  | 180 | // the list for any reason | 
|---|
| [632bc3] | 181 | entry->isIgnored = false; | 
|---|
|  | 182 |  | 
|---|
| [d79639] | 183 | // ok, space is prepared... the user can have it. | 
|---|
|  | 184 | // the rest (constructor, deleting when something is thrown etc) | 
|---|
|  | 185 | // is handled automatically | 
|---|
| [632bc3] | 186 | return entry->info.location; | 
|---|
|  | 187 | } | 
|---|
|  | 188 |  | 
|---|
|  | 189 | void *operator new(size_t nbytes) throw(std::bad_alloc) { | 
|---|
| [d79639] | 190 | // Just forward to the other operator, when we do not know from | 
|---|
|  | 191 | // where the allocation came | 
|---|
| [632bc3] | 192 | return operator new(nbytes,"Unknown",0); | 
|---|
|  | 193 | } | 
|---|
|  | 194 |  | 
|---|
|  | 195 | void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc) { | 
|---|
| [d79639] | 196 | // The difference between new and new[] is just for compiler bookkeeping. | 
|---|
| [632bc3] | 197 | return operator new(nbytes,file,line); | 
|---|
|  | 198 | } | 
|---|
|  | 199 |  | 
|---|
|  | 200 | void *operator new[] (size_t nbytes) throw(std::bad_alloc) { | 
|---|
| [d79639] | 201 | // Forward again | 
|---|
| [632bc3] | 202 | return operator new[] (nbytes,"Unknown",0); | 
|---|
|  | 203 | } | 
|---|
|  | 204 |  | 
|---|
|  | 205 | void operator delete(void *ptr) throw() { | 
|---|
| [d79639] | 206 | // get the size for the entry, including alignment | 
|---|
| [632bc3] | 207 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t)); | 
|---|
|  | 208 |  | 
|---|
| [d79639] | 209 | // get the position for the entry from the pointer the user gave us | 
|---|
| [632bc3] | 210 | Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace); | 
|---|
|  | 211 |  | 
|---|
| [d79639] | 212 | // let's see if the checksum is still matching | 
|---|
| [632bc3] | 213 | if(Memory::calcChecksum(&entry->info)!=entry->checksum){ | 
|---|
|  | 214 | cout << "Possible memory corruption detected!" << endl; | 
|---|
|  | 215 | cout << "Trying to recover allocation information..." << endl; | 
|---|
|  | 216 | cout << "Memory was allocated at " << entry->info.file << ":" << entry->info.line << endl; | 
|---|
|  | 217 | terminate(); | 
|---|
|  | 218 | } | 
|---|
|  | 219 |  | 
|---|
| [d79639] | 220 | // this will destroy the checksum, so double deletes are caught | 
|---|
| [632bc3] | 221 | entry->info.isUsed = false; | 
|---|
|  | 222 | Memory::deleteEntry(entry); | 
|---|
|  | 223 |  | 
|---|
| [d79639] | 224 | // delete the space reserved by malloc | 
|---|
| [632bc3] | 225 | free((char*)ptr-entrySpace); | 
|---|
|  | 226 | } | 
|---|
|  | 227 |  | 
|---|
| [d79639] | 228 | // operator that is called when the constructor throws | 
|---|
|  | 229 | // do not call manually | 
|---|
| [632bc3] | 230 | void operator delete(void *ptr,const char*, int) throw() { | 
|---|
|  | 231 | operator delete(ptr); | 
|---|
|  | 232 | } | 
|---|
|  | 233 |  | 
|---|
|  | 234 | void operator delete[](void *ptr){ | 
|---|
| [d79639] | 235 | // again difference between delete and delete[] is just in compiler bookkeeping | 
|---|
| [632bc3] | 236 | operator delete(ptr); | 
|---|
|  | 237 | } | 
|---|
|  | 238 |  | 
|---|
| [d79639] | 239 | // and another operator that can be called when a constructor throws | 
|---|
| [632bc3] | 240 | void operator delete[](void *ptr,const char*, int) throw(){ | 
|---|
|  | 241 | operator delete(ptr); | 
|---|
|  | 242 | } | 
|---|