Ignore:
Timestamp:
Apr 30, 2010, 8:01:18 AM (15 years ago)
Author:
Frederik Heber <heber@…>
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.
Message:

Merge commit 'jupiter/StructureRefactoring' into StructureRefactoring

Location:
molecuilder/src/Helpers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/Helpers/MemDebug.cpp

    r070651 r1775d3  
    88#include <iostream>
    99#include <cstdlib>
     10#include <cstring>
    1011
    1112using namespace std;
    1213
    1314namespace 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
    1423  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
    1528    struct info_t {
    16       char file[256];
     29      enum {length = 64};
     30      char file[length+1];
    1731      int line;
    1832      size_t nbytes;
     
    2640  };
    2741
     42  // start and end of the doubly-linked list
    2843  entry_t *begin=0;
    2944  entry_t *end=0;
    3045
     46  // current amount of allocated memory
    3147  size_t state = 0;
     48  // maximum amount of allocated memory
    3249  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
    3457  const int alignment = 8;
    3558
     59  // calculates a simple checksum for the info block
     60  // the checksum is used to find memory corruptions
    3661  inline char calcChecksum(entry_t::info_t *info){
    3762    char *buffer = (char*)info;
     
    4368  }
    4469
     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
    4573  inline size_t doAlign(size_t nbytes){
    4674    int nonaligned = nbytes % alignment;
     
    5381  }
    5482
     83  // Output some state information
    5584  void getState(){
    5685    cout << "Maximum allocated Memory: " << max << " bytes" << endl;
    5786    cout << "Currently allocated Memory: " << state <<" bytes" << endl;
    58 
     87    cout << allocs << " allocated chunks total" << endl;
     88
     89    // simple traversal of the chunk list
    5990    for(entry_t *pos=begin;pos;pos=pos->next){
    6091      cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl;
     
    6394  }
    6495
     96  // Deletes an entry from the linked list
    6597  void deleteEntry(entry_t *entry){
    6698    if(entry->isIgnored)
    6799      return;
     100
    68101    if(entry->prev){
    69102      entry->prev->next = entry->next;
    70103    }
    71104    else{
     105      // this node was the beginning of the list
    72106      begin = entry->next;
    73107    }
     
    77111    }
    78112    else{
     113      // this node was the end of the list
    79114      end = entry->prev;
    80115    }
     
    84119
    85120  void _ignore(void *ptr){
     121    // just deletes the node from the list, but leaves the info intact
    86122    static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
    87123    entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
     
    92128void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
    93129
     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
    94133  if(!nbytes) {
    95134    nbytes = 1;
    96135  }
    97136
     137  // get the size of the entry, including alignment
    98138  static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
    99139
    100140  void *res;
    101141  if(!(res=malloc(entrySpace + nbytes))){
     142    // new must throw, when space is low
    102143    throw std::bad_alloc();
    103144  }
    104145
     146  // we got the space, so update the global info
    105147  Memory::state += nbytes;
    106148  if(Memory::state>Memory::max){
    107149    Memory::max = Memory::state;
    108150  }
    109 
     151  Memory::allocs++;
     152
     153  // build the entry in front of the space
    110154  Memory::entry_t *entry = (Memory::entry_t*) res;
    111155  entry->info.nbytes = nbytes;
    112156  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';
    115159  entry->info.line=line;
     160  // the space starts behind the info
    116161  entry->info.location = (char*)res + entrySpace;
    117162
    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
    120166  if(!Memory::begin){
     167    // the list was empty... start a new one
    121168    Memory::begin=entry;
    122169  }
    123170  else {
     171    // other blocks present... we can add to the last one
    124172    Memory::end->next=entry;
    125173  }
    126174  Memory::end=entry;
    127175
     176  // get the checksum...
    128177  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
    129180  entry->isIgnored = false;
    130181
     182  // ok, space is prepared... the user can have it.
     183  // the rest (constructor, deleting when something is thrown etc)
     184  // is handled automatically
    131185  return entry->info.location;
    132186}
    133187
    134188void *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
    135191  return operator new(nbytes,"Unknown",0);
    136192}
    137193
    138194void *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.
    139196  return operator new(nbytes,file,line);
    140197}
    141198
    142199void *operator new[] (size_t nbytes) throw(std::bad_alloc) {
     200  // Forward again
    143201  return operator new[] (nbytes,"Unknown",0);
    144202}
    145203
    146204void operator delete(void *ptr) throw() {
     205  // get the size for the entry, including alignment
    147206  static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
    148207
     208  // get the position for the entry from the pointer the user gave us
    149209  Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
    150210
     211  // let's see if the checksum is still matching
    151212  if(Memory::calcChecksum(&entry->info)!=entry->checksum){
    152213    cout << "Possible memory corruption detected!" << endl;
     
    156217  }
    157218
     219  // this will destroy the checksum, so double deletes are caught
    158220  entry->info.isUsed = false;
    159221  Memory::deleteEntry(entry);
    160222
     223  // delete the space reserved by malloc
    161224  free((char*)ptr-entrySpace);
    162225}
    163226
     227// operator that is called when the constructor throws
     228// do not call manually
    164229void operator delete(void *ptr,const char*, int) throw() {
    165230  operator delete(ptr);
     
    167232
    168233void operator delete[](void *ptr){
     234  // again difference between delete and delete[] is just in compiler bookkeeping
    169235  operator delete(ptr);
    170236}
    171237
     238// and another operator that can be called when a constructor throws
    172239void operator delete[](void *ptr,const char*, int) throw(){
    173240  operator delete(ptr);
  • molecuilder/src/Helpers/MemDebug.hpp

    r070651 r1775d3  
    99#define MEMDEBUG_HPP_
    1010
     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
    1124namespace Memory {
     25
     26  /**
     27   * Displays a short summary of the memory state.
     28   */
    1229  void getState();
    1330
    1431  void _ignore(void*);
    1532
     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   */
    1640  template <typename T>
    1741  T *ignore(T* ptr){
     
    2650void operator delete[] (void *ptr,const char*, int) throw();
    2751
    28 
     52/**
     53 * This macro replaces all occurences of the keyword new with a replaced
     54 * version that allows tracking.
     55 */
    2956#define new new(__FILE__,__LINE__)
    3057
Note: See TracChangeset for help on using the changeset viewer.