[632bc3] | 1 | /*
|
---|
| 2 | * MemDebug.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Apr 28, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[f0f1cc] | 8 | // NDEBUG implies NO_MEMDEBUG
|
---|
| 9 | #ifdef NDEBUG
|
---|
| 10 | # ifndef NO_MEMDEBUG
|
---|
| 11 | # define NO_MEMDEBUG
|
---|
| 12 | # endif
|
---|
| 13 | #endif
|
---|
| 14 |
|
---|
| 15 | // NO_MEMDEBUG and MEMDEBUG are mutually exclusive, but at least one must be set
|
---|
| 16 | #ifdef NO_MEMDEBUG
|
---|
| 17 | # ifdef MEMDEBUG
|
---|
| 18 | # undef MEMDEBUG
|
---|
| 19 | # endif
|
---|
| 20 | #else
|
---|
| 21 | # ifndef MEMDEBUG
|
---|
| 22 | # define MEMDEBUG
|
---|
| 23 | # endif
|
---|
| 24 | #endif
|
---|
| 25 |
|
---|
| 26 | #ifdef MEMDEBUG
|
---|
[fc6053] | 27 |
|
---|
[632bc3] | 28 | #include <iostream>
|
---|
| 29 | #include <cstdlib>
|
---|
[28c351] | 30 | #include <cstring>
|
---|
[f2f4ae] | 31 | #include <boost/thread.hpp>
|
---|
[632bc3] | 32 |
|
---|
[6d6b54] | 33 | #ifdef __GNUC__
|
---|
| 34 | #include <execinfo.h>
|
---|
| 35 | #include <cxxabi.h>
|
---|
| 36 | #endif
|
---|
| 37 |
|
---|
[632bc3] | 38 | using namespace std;
|
---|
| 39 |
|
---|
[492279] | 40 | // we need our own low level mutexex, since we cannot assure the time of construction and destruction
|
---|
| 41 | // otherwise
|
---|
| 42 | #if defined(unix) || defined(__unix)
|
---|
| 43 |
|
---|
| 44 | #include <pthread.h>
|
---|
| 45 | #include <cassert>
|
---|
| 46 | #define mutex_t pthread_mutex_t
|
---|
| 47 | #define mutex_init PTHREAD_MUTEX_INITIALIZER
|
---|
| 48 | #define mutex_lock(mtx) \
|
---|
| 49 | do{\
|
---|
[fc6053] | 50 | int res = pthread_mutex_lock(&(mtx));\
|
---|
[492279] | 51 | assert(!res && "Could not lock mutex!");\
|
---|
| 52 | }while(0)
|
---|
| 53 |
|
---|
| 54 | #define mutex_unlock(mtx) \
|
---|
| 55 | do{\
|
---|
| 56 | int res = pthread_mutex_unlock(&(mtx));\
|
---|
| 57 | assert(!res && "Could not unlock mutex!");\
|
---|
| 58 | }while(0)
|
---|
| 59 |
|
---|
| 60 | #else
|
---|
| 61 | # error "No thread structure defined for this plattform..."
|
---|
| 62 | #endif
|
---|
| 63 |
|
---|
[632bc3] | 64 | namespace Memory {
|
---|
[d79639] | 65 |
|
---|
| 66 | // This struct is added before each memory chunk
|
---|
| 67 | // and contains tracking information. Anything used
|
---|
| 68 | // to track memory cannot use any dynamic memory, so
|
---|
| 69 | // we have to resort to classic C-idioms here.
|
---|
| 70 | // This struct also contains pointers to the next
|
---|
| 71 | // an previous chunks to allow fast traversion of
|
---|
| 72 | // all allocated memory blocks
|
---|
[632bc3] | 73 | struct entry_t {
|
---|
[097902] | 74 | typedef unsigned int checksum_t;
|
---|
[d79639] | 75 | // we seperate the tracking info from the rest
|
---|
| 76 | // A checksum will be calculated for this part of
|
---|
| 77 | // the struct, so the information in here should
|
---|
| 78 | // not change during the lifetime of the memory
|
---|
[632bc3] | 79 | struct info_t {
|
---|
[d79639] | 80 | enum {length = 64};
|
---|
[1f7864] | 81 | char file[length+1];
|
---|
[632bc3] | 82 | int line;
|
---|
[6d6b54] | 83 | #ifdef __GNUC__ // function tracking only works with GCC
|
---|
| 84 | // function names can get looooong
|
---|
| 85 | enum {length2 = 256};
|
---|
| 86 | char function[length2+1];
|
---|
| 87 | #endif
|
---|
[632bc3] | 88 | size_t nbytes;
|
---|
| 89 | bool isUsed;
|
---|
| 90 | void *location;
|
---|
| 91 | } info;
|
---|
| 92 | bool isIgnored;
|
---|
[097902] | 93 | checksum_t checksum;
|
---|
[632bc3] | 94 | entry_t *prev;
|
---|
| 95 | entry_t *next;
|
---|
| 96 | };
|
---|
| 97 |
|
---|
[492279] | 98 |
|
---|
| 99 | mutex_t memorylock = mutex_init;
|
---|
[f2f4ae] | 100 |
|
---|
[d79639] | 101 | // start and end of the doubly-linked list
|
---|
[632bc3] | 102 | entry_t *begin=0;
|
---|
| 103 | entry_t *end=0;
|
---|
| 104 |
|
---|
[d79639] | 105 | // current amount of allocated memory
|
---|
[632bc3] | 106 | size_t state = 0;
|
---|
[d79639] | 107 | // maximum amount of allocated memory
|
---|
[632bc3] | 108 | size_t max = 0;
|
---|
[d79639] | 109 | // number of allocations that have been done so far
|
---|
| 110 | unsigned int allocs = 0;
|
---|
| 111 |
|
---|
[632bc3] | 112 |
|
---|
[d79639] | 113 | // this sets the alignment of the returned memory block
|
---|
| 114 | // malloc guarantees an alignment at the 8 byte border,
|
---|
| 115 | // so we just do the same
|
---|
[632bc3] | 116 | const int alignment = 8;
|
---|
| 117 |
|
---|
[d79639] | 118 | // calculates a simple checksum for the info block
|
---|
| 119 | // the checksum is used to find memory corruptions
|
---|
[097902] | 120 | inline entry_t::checksum_t calcChecksum(entry_t::info_t *info){
|
---|
[632bc3] | 121 | char *buffer = (char*)info;
|
---|
[097902] | 122 | entry_t::checksum_t checksum =0;
|
---|
[632bc3] | 123 | for(size_t i=0;i<sizeof(entry_t::info_t);i++){
|
---|
| 124 | checksum+=buffer[i];
|
---|
| 125 | }
|
---|
| 126 | return checksum;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[d79639] | 129 | // gets the next alignet point which is greater than nbytes
|
---|
| 130 | // this function is only called a fixed number of times, so
|
---|
| 131 | // there is no need to optimize
|
---|
[632bc3] | 132 | inline size_t doAlign(size_t nbytes){
|
---|
| 133 | int nonaligned = nbytes % alignment;
|
---|
| 134 | if(nonaligned) {
|
---|
| 135 | return(nbytes - nonaligned + alignment);
|
---|
| 136 | }
|
---|
| 137 | else{
|
---|
| 138 | return nbytes;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[d79639] | 142 | // Output some state information
|
---|
[632bc3] | 143 | void getState(){
|
---|
| 144 | cout << "Maximum allocated Memory: " << max << " bytes" << endl;
|
---|
| 145 | cout << "Currently allocated Memory: " << state <<" bytes" << endl;
|
---|
[d79639] | 146 | cout << allocs << " allocated chunks total" << endl;
|
---|
[632bc3] | 147 |
|
---|
[d79639] | 148 | // simple traversal of the chunk list
|
---|
[632bc3] | 149 | for(entry_t *pos=begin;pos;pos=pos->next){
|
---|
| 150 | cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl;
|
---|
[6d6b54] | 151 | #ifdef __GNUC__
|
---|
| 152 | cout << "Chunk reserved at: " << pos->info.function
|
---|
| 153 | << " (" << pos->info.file << ":" << pos->info.line << ")" << endl;
|
---|
| 154 | #else
|
---|
[632bc3] | 155 | cout << "Chunk reserved at: " << pos->info.file << ":" << pos->info.line << endl;
|
---|
[6d6b54] | 156 | #endif
|
---|
[632bc3] | 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[097902] | 160 | void dumpMemory(std::ostream &ost){
|
---|
| 161 | ost << "Maximum allocated Memory: " << max << " bytes" << endl;
|
---|
| 162 | ost << "Maximum allocated Memory: " << max << " bytes" << endl;
|
---|
| 163 | ost << "Currently allocated Memory: " << state <<" bytes" << endl;
|
---|
| 164 | ost << allocs << " allocated chunks total" << endl;
|
---|
| 165 | bool corrupted=false;
|
---|
| 166 | for(entry_t *pos=begin;pos;pos=pos->next){
|
---|
| 167 | ost << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl;
|
---|
| 168 | # ifdef __GNUC__
|
---|
| 169 | ost << "Chunk reserved at: " << pos->info.function
|
---|
| 170 | << " (" << pos->info.file << ":" << pos->info.line << ")" << endl;
|
---|
| 171 | # else
|
---|
| 172 | ost << "Chunk reserved at: " << pos->info.file << ":" << pos->info.line << endl;
|
---|
| 173 | # endif
|
---|
| 174 | ost << "Chunk address: " << pos->info.location << endl;
|
---|
| 175 | entry_t::checksum_t checksum = calcChecksum(&pos->info);
|
---|
| 176 | ost << "Checksum of chunk: " << checksum << endl;
|
---|
| 177 | ost << "Checksum at allocation time: " << pos->checksum << endl;
|
---|
| 178 | if(checksum!=pos->checksum){
|
---|
| 179 | ost << "!!!Chunk was corrupted!!!" << endl;
|
---|
| 180 | corrupted=true;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | if(corrupted){
|
---|
| 184 | ost << "\n!!!Memory corruption detected!!!" << endl;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[492279] | 188 | // Adds an entry to the linked list
|
---|
| 189 | void addEntry(entry_t *entry){
|
---|
| 190 | // check if the entry is already in the list
|
---|
| 191 | if(!entry->isIgnored)
|
---|
| 192 | return;
|
---|
| 193 |
|
---|
| 194 | mutex_lock(Memory::memorylock);
|
---|
| 195 |
|
---|
| 196 | entry->next=0; // the created block is last in the list
|
---|
| 197 | entry->prev=Memory::end; // the created block is last in the list
|
---|
| 198 | if(!Memory::begin){
|
---|
| 199 | // the list was empty... start a new one
|
---|
| 200 | Memory::begin=entry;
|
---|
| 201 | }
|
---|
| 202 | else {
|
---|
| 203 | // other blocks present... we can add to the last one
|
---|
| 204 | Memory::end->next=entry;
|
---|
| 205 | }
|
---|
| 206 | Memory::end=entry;
|
---|
| 207 |
|
---|
| 208 | // update some global info
|
---|
| 209 | Memory::state += entry->info.nbytes;
|
---|
| 210 | if(Memory::state>Memory::max){
|
---|
| 211 | Memory::max = Memory::state;
|
---|
| 212 | }
|
---|
| 213 | ++Memory::allocs;
|
---|
| 214 | // done with the list... it is safe to unlock now
|
---|
| 215 | mutex_unlock(Memory::memorylock);
|
---|
| 216 | entry->isIgnored = false;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[d79639] | 219 | // Deletes an entry from the linked list
|
---|
[632bc3] | 220 | void deleteEntry(entry_t *entry){
|
---|
| 221 | if(entry->isIgnored)
|
---|
| 222 | return;
|
---|
[d79639] | 223 |
|
---|
[492279] | 224 | mutex_lock(memorylock);
|
---|
[632bc3] | 225 | if(entry->prev){
|
---|
| 226 | entry->prev->next = entry->next;
|
---|
| 227 | }
|
---|
| 228 | else{
|
---|
[d79639] | 229 | // this node was the beginning of the list
|
---|
[632bc3] | 230 | begin = entry->next;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | if(entry->next){
|
---|
| 234 | entry->next->prev = entry->prev;
|
---|
| 235 | }
|
---|
| 236 | else{
|
---|
[d79639] | 237 | // this node was the end of the list
|
---|
[632bc3] | 238 | end = entry->prev;
|
---|
| 239 | }
|
---|
| 240 | Memory::state -= entry->info.nbytes;
|
---|
[492279] | 241 | mutex_unlock(memorylock);
|
---|
| 242 | entry->isIgnored = true;
|
---|
| 243 |
|
---|
[632bc3] | 244 | }
|
---|
| 245 |
|
---|
| 246 | void _ignore(void *ptr){
|
---|
[d79639] | 247 | // just deletes the node from the list, but leaves the info intact
|
---|
[632bc3] | 248 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
---|
| 249 | entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
|
---|
| 250 | deleteEntry(entry);
|
---|
| 251 | }
|
---|
[6d6b54] | 252 |
|
---|
| 253 | #ifdef __GNUC__
|
---|
| 254 | // this function let's us find the caller's name
|
---|
| 255 | char* getCaller(){
|
---|
| 256 | // stack looks like this:
|
---|
| 257 | // getCaller();
|
---|
| 258 | // operator new();
|
---|
| 259 | // function_we_are_looking_for(); <-
|
---|
| 260 | const size_t max_depth = 3;
|
---|
| 261 | void* stack_addrs[max_depth];
|
---|
| 262 | size_t stack_depth;
|
---|
| 263 | char **stack_strings=0;
|
---|
| 264 | const char *func_name=0;
|
---|
| 265 | const char *toplevel = "Global scope";
|
---|
| 266 | char *retval=0;
|
---|
| 267 |
|
---|
| 268 | // get the backtrace, depth three
|
---|
| 269 | stack_depth = backtrace(stack_addrs,max_depth);
|
---|
| 270 | stack_strings = backtrace_symbols(stack_addrs, stack_depth);
|
---|
| 271 | // used later for demangling
|
---|
| 272 | // reserved here, so we can free it unconditionally
|
---|
| 273 | char *dm_function = static_cast<char*>(malloc(entry_t::info_t::length2));
|
---|
| 274 | if(!dm_function){
|
---|
| 275 | // malloc failed... we are out of luck
|
---|
| 276 | throw std::bad_alloc();
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | // see if we found our function name
|
---|
| 280 | if(stack_depth==max_depth){
|
---|
| 281 | // find the mangled function name
|
---|
| 282 | char *begin = stack_strings[max_depth-1];
|
---|
| 283 | // function name starts with a (
|
---|
| 284 | while(*begin && *begin!='(') ++begin;
|
---|
| 285 | char *end=begin;
|
---|
| 286 | while(*end && *end!='+') ++end;
|
---|
| 287 |
|
---|
| 288 | // see if we found our function name
|
---|
| 289 | if(*begin && *end){
|
---|
| 290 | *begin++ = 0;
|
---|
| 291 | *end = 0;
|
---|
| 292 | // use the C++ demangler
|
---|
| 293 |
|
---|
| 294 | size_t sz = entry_t::info_t::length2;
|
---|
| 295 | int status;
|
---|
| 296 | char *func_ret = abi::__cxa_demangle(begin, dm_function, &sz, &status);
|
---|
| 297 | if(func_ret){
|
---|
| 298 | // abi might have realloced...
|
---|
| 299 | dm_function = func_ret;
|
---|
| 300 | func_name = dm_function;
|
---|
| 301 | }
|
---|
| 302 | else{
|
---|
| 303 | // demangling failed... get the function name without demangling
|
---|
| 304 | func_name = begin;
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 | else{
|
---|
| 308 | // function name not found... get the whole line
|
---|
| 309 | func_name = stack_strings[max_depth-1];
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | }
|
---|
| 313 | else{
|
---|
| 314 | func_name = toplevel;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | // now we copy the desired function name
|
---|
| 318 | if((retval = static_cast<char*>(malloc(strlen(func_name)+1)))){
|
---|
| 319 | // we know that the string will fit, so strcpy is safe here
|
---|
| 320 | strcpy(retval,func_name);
|
---|
| 321 | }
|
---|
| 322 | else{
|
---|
| 323 | free(stack_strings); // malloc()ed by backtrace_symbols
|
---|
| 324 | free(dm_function);
|
---|
| 325 | // uh-uh ... seems we are out of luck for allocations now
|
---|
| 326 | throw std::bad_alloc();
|
---|
| 327 | }
|
---|
| 328 | free(dm_function);
|
---|
| 329 | free(stack_strings); // malloc()ed by backtrace_symbols
|
---|
| 330 | return retval;
|
---|
| 331 | }
|
---|
| 332 | #endif
|
---|
[632bc3] | 333 | }
|
---|
| 334 |
|
---|
[6d6b54] | 335 | #ifdef __GNUC__
|
---|
| 336 |
|
---|
| 337 | void *operator new(size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc) {
|
---|
| 338 |
|
---|
| 339 | // to avoid allocations of 0 bytes if someone screws up
|
---|
| 340 | // allocation with 0 byte size are undefined behavior, so we are
|
---|
| 341 | // free to handle it this way
|
---|
| 342 | if(!nbytes) {
|
---|
| 343 | nbytes = 1;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | // get the size of the entry, including alignment
|
---|
| 347 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
---|
| 348 |
|
---|
| 349 | void *res;
|
---|
| 350 | if(!(res=malloc(entrySpace + nbytes))){
|
---|
| 351 | // new must throw, when space is low
|
---|
| 352 | throw std::bad_alloc();
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | // build the entry in front of the space
|
---|
| 356 | Memory::entry_t *entry = (Memory::entry_t*) res;
|
---|
| 357 | memset(res,0,entrySpace);
|
---|
| 358 | entry->info.nbytes = nbytes;
|
---|
| 359 | entry->info.isUsed = true;
|
---|
| 360 | strncpy(entry->info.file,file,Memory::entry_t::info_t::length);
|
---|
| 361 | entry->info.file[Memory::entry_t::info_t::length] = '\0';
|
---|
| 362 | entry->info.line=line;
|
---|
| 363 | strncpy(entry->info.function,func,Memory::entry_t::info_t::length2);
|
---|
| 364 | entry->info.function[Memory::entry_t::info_t::length2] = '\0';
|
---|
| 365 | // the space starts behind the info
|
---|
| 366 | entry->info.location = (char*)res + entrySpace;
|
---|
| 367 |
|
---|
[492279] | 368 | // mark the block as not in the list (will be changed by addEntry)
|
---|
| 369 | entry->isIgnored = true;
|
---|
| 370 | Memory::addEntry(entry);
|
---|
[6d6b54] | 371 |
|
---|
| 372 | // get the checksum...
|
---|
| 373 | entry->checksum = Memory::calcChecksum(&entry->info);
|
---|
| 374 |
|
---|
| 375 | // ok, space is prepared... the user can have it.
|
---|
| 376 | // the rest (constructor, deleting when something is thrown etc)
|
---|
| 377 | // is handled automatically
|
---|
| 378 | return entry->info.location;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | #else
|
---|
| 382 |
|
---|
[632bc3] | 383 | void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
|
---|
| 384 |
|
---|
[d79639] | 385 | // to avoid allocations of 0 bytes if someone screws up
|
---|
| 386 | // allocation with 0 byte size are undefined behavior, so we are
|
---|
| 387 | // free to handle it this way
|
---|
[632bc3] | 388 | if(!nbytes) {
|
---|
| 389 | nbytes = 1;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
[d79639] | 392 | // get the size of the entry, including alignment
|
---|
[632bc3] | 393 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
---|
| 394 |
|
---|
| 395 | void *res;
|
---|
| 396 | if(!(res=malloc(entrySpace + nbytes))){
|
---|
[d79639] | 397 | // new must throw, when space is low
|
---|
[632bc3] | 398 | throw std::bad_alloc();
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[d79639] | 401 | // build the entry in front of the space
|
---|
[632bc3] | 402 | Memory::entry_t *entry = (Memory::entry_t*) res;
|
---|
[68f03d] | 403 | memset(res,0,entrySpace);
|
---|
[632bc3] | 404 | entry->info.nbytes = nbytes;
|
---|
| 405 | entry->info.isUsed = true;
|
---|
[d79639] | 406 | strncpy(entry->info.file,file,Memory::entry_t::info_t::length);
|
---|
[1f7864] | 407 | entry->info.file[Memory::entry_t::info_t::length] = '\0';
|
---|
[632bc3] | 408 | entry->info.line=line;
|
---|
[d79639] | 409 | // the space starts behind the info
|
---|
[632bc3] | 410 | entry->info.location = (char*)res + entrySpace;
|
---|
| 411 |
|
---|
[492279] | 412 | // mark the block as not in the list (will be changed by addEntry)
|
---|
| 413 | entry->isIgnored = true;
|
---|
| 414 | Memory::addEntry(entry);
|
---|
[632bc3] | 415 |
|
---|
[d79639] | 416 | // get the checksum...
|
---|
[632bc3] | 417 | entry->checksum = Memory::calcChecksum(&entry->info);
|
---|
[d79639] | 418 | // this will be set to true, when the block is removed from
|
---|
| 419 | // the list for any reason
|
---|
[632bc3] | 420 | entry->isIgnored = false;
|
---|
| 421 |
|
---|
[d79639] | 422 | // ok, space is prepared... the user can have it.
|
---|
| 423 | // the rest (constructor, deleting when something is thrown etc)
|
---|
| 424 | // is handled automatically
|
---|
[632bc3] | 425 | return entry->info.location;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[6d6b54] | 428 | #endif
|
---|
| 429 |
|
---|
[632bc3] | 430 | void *operator new(size_t nbytes) throw(std::bad_alloc) {
|
---|
[d79639] | 431 | // Just forward to the other operator, when we do not know from
|
---|
| 432 | // where the allocation came
|
---|
[6d6b54] | 433 | #ifdef __GNUC__
|
---|
| 434 | // this might throw bad_alloc
|
---|
| 435 | char *caller = Memory::getCaller();
|
---|
| 436 | void* retval = 0;
|
---|
| 437 |
|
---|
| 438 | // if this throws, we have to clean up the caller anyway
|
---|
| 439 | try{
|
---|
| 440 | retval = operator new(nbytes,"Unknown",0,caller);
|
---|
| 441 | }
|
---|
| 442 | catch(...)
|
---|
| 443 | {
|
---|
| 444 | free(caller); // malloc()ed by Memory::getCaller();
|
---|
| 445 | throw;
|
---|
| 446 | }
|
---|
| 447 | free(caller); // malloc()ed by Memory::getCaller();
|
---|
| 448 | return retval;
|
---|
| 449 | #else
|
---|
[632bc3] | 450 | return operator new(nbytes,"Unknown",0);
|
---|
[6d6b54] | 451 | #endif
|
---|
[632bc3] | 452 | }
|
---|
| 453 |
|
---|
[6d6b54] | 454 | #ifdef __GNUC__
|
---|
| 455 |
|
---|
| 456 | void *operator new[] (size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc) {
|
---|
| 457 | // The difference between new and new[] is just for compiler bookkeeping.
|
---|
| 458 | return operator new(nbytes,file,line,func);
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | #else
|
---|
| 462 |
|
---|
[632bc3] | 463 | void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
|
---|
[d79639] | 464 | // The difference between new and new[] is just for compiler bookkeeping.
|
---|
[632bc3] | 465 | return operator new(nbytes,file,line);
|
---|
| 466 | }
|
---|
| 467 |
|
---|
[6d6b54] | 468 | #endif
|
---|
| 469 |
|
---|
[632bc3] | 470 | void *operator new[] (size_t nbytes) throw(std::bad_alloc) {
|
---|
[d79639] | 471 | // Forward again
|
---|
[6d6b54] | 472 | #ifdef __GNUC__
|
---|
| 473 | // this might throw bad_alloc
|
---|
| 474 | char *caller = Memory::getCaller();
|
---|
| 475 | void *retval=0;
|
---|
| 476 |
|
---|
| 477 | // if this throws, we have to clean up the caller anyway
|
---|
| 478 | try{
|
---|
| 479 | retval = operator new[] (nbytes,"Unknown",0,caller);
|
---|
| 480 | }
|
---|
| 481 | catch(...)
|
---|
| 482 | {
|
---|
| 483 | free(caller); // malloc()ed by Memory::getCaller();
|
---|
| 484 | throw;
|
---|
| 485 | }
|
---|
| 486 | free(caller); // malloc()ed by Memory::getCaller();
|
---|
| 487 | return retval;
|
---|
| 488 | #else
|
---|
[632bc3] | 489 | return operator new[] (nbytes,"Unknown",0);
|
---|
[6d6b54] | 490 | #endif
|
---|
[632bc3] | 491 | }
|
---|
| 492 |
|
---|
| 493 | void operator delete(void *ptr) throw() {
|
---|
[93dfc7b] | 494 | if(!ptr){
|
---|
| 495 | cerr << "Warning: Deleting NULL pointer" << endl;
|
---|
| 496 | return;
|
---|
| 497 | }
|
---|
[f2f4ae] | 498 |
|
---|
[d79639] | 499 | // get the size for the entry, including alignment
|
---|
[632bc3] | 500 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
---|
| 501 |
|
---|
[d79639] | 502 | // get the position for the entry from the pointer the user gave us
|
---|
[632bc3] | 503 | Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
|
---|
| 504 |
|
---|
[d79639] | 505 | // let's see if the checksum is still matching
|
---|
[632bc3] | 506 | if(Memory::calcChecksum(&entry->info)!=entry->checksum){
|
---|
[93dfc7b] | 507 | cerr << "Possible memory corruption detected!" << endl;
|
---|
| 508 | cerr << "Trying to recover allocation information..." << endl;
|
---|
| 509 | cerr << "Memory was allocated at " << entry->info.file << ":" << entry->info.line << endl;
|
---|
[632bc3] | 510 | terminate();
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[d79639] | 513 | // this will destroy the checksum, so double deletes are caught
|
---|
[632bc3] | 514 | entry->info.isUsed = false;
|
---|
| 515 | Memory::deleteEntry(entry);
|
---|
| 516 |
|
---|
[d79639] | 517 | // delete the space reserved by malloc
|
---|
[632bc3] | 518 | free((char*)ptr-entrySpace);
|
---|
| 519 | }
|
---|
| 520 |
|
---|
[d79639] | 521 | // operator that is called when the constructor throws
|
---|
| 522 | // do not call manually
|
---|
[632bc3] | 523 | void operator delete(void *ptr,const char*, int) throw() {
|
---|
| 524 | operator delete(ptr);
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | void operator delete[](void *ptr){
|
---|
[d79639] | 528 | // again difference between delete and delete[] is just in compiler bookkeeping
|
---|
[632bc3] | 529 | operator delete(ptr);
|
---|
| 530 | }
|
---|
| 531 |
|
---|
[d79639] | 532 | // and another operator that can be called when a constructor throws
|
---|
[632bc3] | 533 | void operator delete[](void *ptr,const char*, int) throw(){
|
---|
| 534 | operator delete(ptr);
|
---|
| 535 | }
|
---|
[5702280] | 536 | #endif
|
---|