Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Helpers/MemDebug.cpp

    r28c351 r68f03d  
    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
    1523  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
    1628    struct info_t {
    17       char file[256];
     29      enum {length = 64};
     30      char file[length+1];
    1831      int line;
    1932      size_t nbytes;
     
    2740  };
    2841
     42  // start and end of the doubly-linked list
    2943  entry_t *begin=0;
    3044  entry_t *end=0;
    3145
     46  // current amount of allocated memory
    3247  size_t state = 0;
     48  // maximum amount of allocated memory
    3349  size_t max = 0;
    34 
     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
    3557  const int alignment = 8;
    3658
     59  // calculates a simple checksum for the info block
     60  // the checksum is used to find memory corruptions
    3761  inline char calcChecksum(entry_t::info_t *info){
    3862    char *buffer = (char*)info;
     
    4468  }
    4569
     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
    4673  inline size_t doAlign(size_t nbytes){
    4774    int nonaligned = nbytes % alignment;
     
    5481  }
    5582
     83  // Output some state information
    5684  void getState(){
    5785    cout << "Maximum allocated Memory: " << max << " bytes" << endl;
    5886    cout << "Currently allocated Memory: " << state <<" bytes" << endl;
    59 
     87    cout << allocs << " allocated chunks total" << endl;
     88
     89    // simple traversal of the chunk list
    6090    for(entry_t *pos=begin;pos;pos=pos->next){
    6191      cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl;
     
    6494  }
    6595
     96  // Deletes an entry from the linked list
    6697  void deleteEntry(entry_t *entry){
    6798    if(entry->isIgnored)
    6899      return;
     100
    69101    if(entry->prev){
    70102      entry->prev->next = entry->next;
    71103    }
    72104    else{
     105      // this node was the beginning of the list
    73106      begin = entry->next;
    74107    }
     
    78111    }
    79112    else{
     113      // this node was the end of the list
    80114      end = entry->prev;
    81115    }
     
    85119
    86120  void _ignore(void *ptr){
     121    // just deletes the node from the list, but leaves the info intact
    87122    static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
    88123    entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
     
    93128void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
    94129
     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
    95133  if(!nbytes) {
    96134    nbytes = 1;
    97135  }
    98136
     137  // get the size of the entry, including alignment
    99138  static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
    100139
    101140  void *res;
    102141  if(!(res=malloc(entrySpace + nbytes))){
     142    // new must throw, when space is low
    103143    throw std::bad_alloc();
    104144  }
    105145
     146  // we got the space, so update the global info
    106147  Memory::state += nbytes;
    107148  if(Memory::state>Memory::max){
    108149    Memory::max = Memory::state;
    109150  }
    110 
     151  Memory::allocs++;
     152
     153  // build the entry in front of the space
    111154  Memory::entry_t *entry = (Memory::entry_t*) res;
     155  memset(res,0,entrySpace);
    112156  entry->info.nbytes = nbytes;
    113157  entry->info.isUsed = true;
    114   strncpy(entry->info.file,file,256);
    115   entry->info.file[255] = '\0';
     158  strncpy(entry->info.file,file,Memory::entry_t::info_t::length);
     159  entry->info.file[Memory::entry_t::info_t::length] = '\0';
    116160  entry->info.line=line;
     161  // the space starts behind the info
    117162  entry->info.location = (char*)res + entrySpace;
    118163
    119   entry->next=0;
    120   entry->prev=Memory::end;
     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
    121167  if(!Memory::begin){
     168    // the list was empty... start a new one
    122169    Memory::begin=entry;
    123170  }
    124171  else {
     172    // other blocks present... we can add to the last one
    125173    Memory::end->next=entry;
    126174  }
    127175  Memory::end=entry;
    128176
     177  // get the checksum...
    129178  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
    130181  entry->isIgnored = false;
    131182
     183  // ok, space is prepared... the user can have it.
     184  // the rest (constructor, deleting when something is thrown etc)
     185  // is handled automatically
    132186  return entry->info.location;
    133187}
    134188
    135189void *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
    136192  return operator new(nbytes,"Unknown",0);
    137193}
    138194
    139195void *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.
    140197  return operator new(nbytes,file,line);
    141198}
    142199
    143200void *operator new[] (size_t nbytes) throw(std::bad_alloc) {
     201  // Forward again
    144202  return operator new[] (nbytes,"Unknown",0);
    145203}
    146204
    147205void operator delete(void *ptr) throw() {
     206  // get the size for the entry, including alignment
    148207  static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
    149208
     209  // get the position for the entry from the pointer the user gave us
    150210  Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
    151211
     212  // let's see if the checksum is still matching
    152213  if(Memory::calcChecksum(&entry->info)!=entry->checksum){
    153214    cout << "Possible memory corruption detected!" << endl;
     
    157218  }
    158219
     220  // this will destroy the checksum, so double deletes are caught
    159221  entry->info.isUsed = false;
    160222  Memory::deleteEntry(entry);
    161223
     224  // delete the space reserved by malloc
    162225  free((char*)ptr-entrySpace);
    163226}
    164227
     228// operator that is called when the constructor throws
     229// do not call manually
    165230void operator delete(void *ptr,const char*, int) throw() {
    166231  operator delete(ptr);
     
    168233
    169234void operator delete[](void *ptr){
     235  // again difference between delete and delete[] is just in compiler bookkeeping
    170236  operator delete(ptr);
    171237}
    172238
     239// and another operator that can be called when a constructor throws
    173240void operator delete[](void *ptr,const char*, int) throw(){
    174241  operator delete(ptr);
Note: See TracChangeset for help on using the changeset viewer.