Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Helpers/MemDebug.cpp

    r68f03d r28c351  
    1313
    1414namespace 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
    2315  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
    2816    struct info_t {
    29       enum {length = 64};
    30       char file[length+1];
     17      char file[256];
    3118      int line;
    3219      size_t nbytes;
     
    4027  };
    4128
    42   // start and end of the doubly-linked list
    4329  entry_t *begin=0;
    4430  entry_t *end=0;
    4531
    46   // current amount of allocated memory
    4732  size_t state = 0;
    48   // maximum amount of allocated memory
    4933  size_t max = 0;
    50   // number of allocations that have been done so far
    51   unsigned int allocs = 0;
    5234
    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
    5735  const int alignment = 8;
    5836
    59   // calculates a simple checksum for the info block
    60   // the checksum is used to find memory corruptions
    6137  inline char calcChecksum(entry_t::info_t *info){
    6238    char *buffer = (char*)info;
     
    6844  }
    6945
    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
    7346  inline size_t doAlign(size_t nbytes){
    7447    int nonaligned = nbytes % alignment;
     
    8154  }
    8255
    83   // Output some state information
    8456  void getState(){
    8557    cout << "Maximum allocated Memory: " << max << " bytes" << endl;
    8658    cout << "Currently allocated Memory: " << state <<" bytes" << endl;
    87     cout << allocs << " allocated chunks total" << endl;
    8859
    89     // simple traversal of the chunk list
    9060    for(entry_t *pos=begin;pos;pos=pos->next){
    9161      cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl;
     
    9464  }
    9565
    96   // Deletes an entry from the linked list
    9766  void deleteEntry(entry_t *entry){
    9867    if(entry->isIgnored)
    9968      return;
    100 
    10169    if(entry->prev){
    10270      entry->prev->next = entry->next;
    10371    }
    10472    else{
    105       // this node was the beginning of the list
    10673      begin = entry->next;
    10774    }
     
    11178    }
    11279    else{
    113       // this node was the end of the list
    11480      end = entry->prev;
    11581    }
     
    11985
    12086  void _ignore(void *ptr){
    121     // just deletes the node from the list, but leaves the info intact
    12287    static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
    12388    entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
     
    12893void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
    12994
    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
    13395  if(!nbytes) {
    13496    nbytes = 1;
    13597  }
    13698
    137   // get the size of the entry, including alignment
    13899  static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
    139100
    140101  void *res;
    141102  if(!(res=malloc(entrySpace + nbytes))){
    142     // new must throw, when space is low
    143103    throw std::bad_alloc();
    144104  }
    145105
    146   // we got the space, so update the global info
    147106  Memory::state += nbytes;
    148107  if(Memory::state>Memory::max){
    149108    Memory::max = Memory::state;
    150109  }
    151   Memory::allocs++;
    152110
    153   // build the entry in front of the space
    154111  Memory::entry_t *entry = (Memory::entry_t*) res;
    155   memset(res,0,entrySpace);
    156112  entry->info.nbytes = nbytes;
    157113  entry->info.isUsed = true;
    158   strncpy(entry->info.file,file,Memory::entry_t::info_t::length);
    159   entry->info.file[Memory::entry_t::info_t::length] = '\0';
     114  strncpy(entry->info.file,file,256);
     115  entry->info.file[255] = '\0';
    160116  entry->info.line=line;
    161   // the space starts behind the info
    162117  entry->info.location = (char*)res + entrySpace;
    163118
    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
     119  entry->next=0;
     120  entry->prev=Memory::end;
    167121  if(!Memory::begin){
    168     // the list was empty... start a new one
    169122    Memory::begin=entry;
    170123  }
    171124  else {
    172     // other blocks present... we can add to the last one
    173125    Memory::end->next=entry;
    174126  }
    175127  Memory::end=entry;
    176128
    177   // get the checksum...
    178129  entry->checksum = Memory::calcChecksum(&entry->info);
    179   // this will be set to true, when the block is removed from
    180   // the list for any reason
    181130  entry->isIgnored = false;
    182131
    183   // ok, space is prepared... the user can have it.
    184   // the rest (constructor, deleting when something is thrown etc)
    185   // is handled automatically
    186132  return entry->info.location;
    187133}
    188134
    189135void *operator new(size_t nbytes) throw(std::bad_alloc) {
    190   // Just forward to the other operator, when we do not know from
    191   // where the allocation came
    192136  return operator new(nbytes,"Unknown",0);
    193137}
    194138
    195139void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
    196   // The difference between new and new[] is just for compiler bookkeeping.
    197140  return operator new(nbytes,file,line);
    198141}
    199142
    200143void *operator new[] (size_t nbytes) throw(std::bad_alloc) {
    201   // Forward again
    202144  return operator new[] (nbytes,"Unknown",0);
    203145}
    204146
    205147void operator delete(void *ptr) throw() {
    206   // get the size for the entry, including alignment
    207148  static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
    208149
    209   // get the position for the entry from the pointer the user gave us
    210150  Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
    211151
    212   // let's see if the checksum is still matching
    213152  if(Memory::calcChecksum(&entry->info)!=entry->checksum){
    214153    cout << "Possible memory corruption detected!" << endl;
     
    218157  }
    219158
    220   // this will destroy the checksum, so double deletes are caught
    221159  entry->info.isUsed = false;
    222160  Memory::deleteEntry(entry);
    223161
    224   // delete the space reserved by malloc
    225162  free((char*)ptr-entrySpace);
    226163}
    227164
    228 // operator that is called when the constructor throws
    229 // do not call manually
    230165void operator delete(void *ptr,const char*, int) throw() {
    231166  operator delete(ptr);
     
    233168
    234169void operator delete[](void *ptr){
    235   // again difference between delete and delete[] is just in compiler bookkeeping
    236170  operator delete(ptr);
    237171}
    238172
    239 // and another operator that can be called when a constructor throws
    240173void operator delete[](void *ptr,const char*, int) throw(){
    241174  operator delete(ptr);
Note: See TracChangeset for help on using the changeset viewer.