| 1 | /*
|
|---|
| 2 | * MemDebug.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Apr 28, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include <iostream>
|
|---|
| 9 | #include <cstdlib>
|
|---|
| 10 |
|
|---|
| 11 | using namespace std;
|
|---|
| 12 |
|
|---|
| 13 | namespace Memory {
|
|---|
| 14 | struct entry_t {
|
|---|
| 15 | struct info_t {
|
|---|
| 16 | char file[256];
|
|---|
| 17 | int line;
|
|---|
| 18 | size_t nbytes;
|
|---|
| 19 | bool isUsed;
|
|---|
| 20 | void *location;
|
|---|
| 21 | } info;
|
|---|
| 22 | bool isIgnored;
|
|---|
| 23 | char checksum;
|
|---|
| 24 | entry_t *prev;
|
|---|
| 25 | entry_t *next;
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | entry_t *begin=0;
|
|---|
| 29 | entry_t *end=0;
|
|---|
| 30 |
|
|---|
| 31 | size_t state = 0;
|
|---|
| 32 | size_t max = 0;
|
|---|
| 33 |
|
|---|
| 34 | const int alignment = 8;
|
|---|
| 35 |
|
|---|
| 36 | inline char calcChecksum(entry_t::info_t *info){
|
|---|
| 37 | char *buffer = (char*)info;
|
|---|
| 38 | char checksum =0;
|
|---|
| 39 | for(size_t i=0;i<sizeof(entry_t::info_t);i++){
|
|---|
| 40 | checksum+=buffer[i];
|
|---|
| 41 | }
|
|---|
| 42 | return checksum;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | inline size_t doAlign(size_t nbytes){
|
|---|
| 46 | int nonaligned = nbytes % alignment;
|
|---|
| 47 | if(nonaligned) {
|
|---|
| 48 | return(nbytes - nonaligned + alignment);
|
|---|
| 49 | }
|
|---|
| 50 | else{
|
|---|
| 51 | return nbytes;
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | void getState(){
|
|---|
| 56 | cout << "Maximum allocated Memory: " << max << " bytes" << endl;
|
|---|
| 57 | cout << "Currently allocated Memory: " << state <<" bytes" << endl;
|
|---|
| 58 |
|
|---|
| 59 | for(entry_t *pos=begin;pos;pos=pos->next){
|
|---|
| 60 | cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl;
|
|---|
| 61 | cout << "Chunk reserved at: " << pos->info.file << ":" << pos->info.line << endl;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | void deleteEntry(entry_t *entry){
|
|---|
| 66 | if(entry->isIgnored)
|
|---|
| 67 | return;
|
|---|
| 68 | if(entry->prev){
|
|---|
| 69 | entry->prev->next = entry->next;
|
|---|
| 70 | }
|
|---|
| 71 | else{
|
|---|
| 72 | begin = entry->next;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | if(entry->next){
|
|---|
| 76 | entry->next->prev = entry->prev;
|
|---|
| 77 | }
|
|---|
| 78 | else{
|
|---|
| 79 | end = entry->prev;
|
|---|
| 80 | }
|
|---|
| 81 | entry->isIgnored = true;
|
|---|
| 82 | Memory::state -= entry->info.nbytes;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void _ignore(void *ptr){
|
|---|
| 86 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
|---|
| 87 | entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
|
|---|
| 88 | deleteEntry(entry);
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
|
|---|
| 93 |
|
|---|
| 94 | if(!nbytes) {
|
|---|
| 95 | nbytes = 1;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
|---|
| 99 |
|
|---|
| 100 | void *res;
|
|---|
| 101 | if(!(res=malloc(entrySpace + nbytes))){
|
|---|
| 102 | throw std::bad_alloc();
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | Memory::state += nbytes;
|
|---|
| 106 | if(Memory::state>Memory::max){
|
|---|
| 107 | Memory::max = Memory::state;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | Memory::entry_t *entry = (Memory::entry_t*) res;
|
|---|
| 111 | entry->info.nbytes = nbytes;
|
|---|
| 112 | entry->info.isUsed = true;
|
|---|
| 113 | strncpy(entry->info.file,file,256);
|
|---|
| 114 | entry->info.file[255] = '\0';
|
|---|
| 115 | entry->info.line=line;
|
|---|
| 116 | entry->info.location = (char*)res + entrySpace;
|
|---|
| 117 |
|
|---|
| 118 | entry->next=0;
|
|---|
| 119 | entry->prev=Memory::end;
|
|---|
| 120 | if(!Memory::begin){
|
|---|
| 121 | Memory::begin=entry;
|
|---|
| 122 | }
|
|---|
| 123 | else {
|
|---|
| 124 | Memory::end->next=entry;
|
|---|
| 125 | }
|
|---|
| 126 | Memory::end=entry;
|
|---|
| 127 |
|
|---|
| 128 | entry->checksum = Memory::calcChecksum(&entry->info);
|
|---|
| 129 | entry->isIgnored = false;
|
|---|
| 130 |
|
|---|
| 131 | return entry->info.location;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | void *operator new(size_t nbytes) throw(std::bad_alloc) {
|
|---|
| 135 | return operator new(nbytes,"Unknown",0);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
|
|---|
| 139 | return operator new(nbytes,file,line);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | void *operator new[] (size_t nbytes) throw(std::bad_alloc) {
|
|---|
| 143 | return operator new[] (nbytes,"Unknown",0);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | void operator delete(void *ptr) throw() {
|
|---|
| 147 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
|---|
| 148 |
|
|---|
| 149 | Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
|
|---|
| 150 |
|
|---|
| 151 | if(Memory::calcChecksum(&entry->info)!=entry->checksum){
|
|---|
| 152 | cout << "Possible memory corruption detected!" << endl;
|
|---|
| 153 | cout << "Trying to recover allocation information..." << endl;
|
|---|
| 154 | cout << "Memory was allocated at " << entry->info.file << ":" << entry->info.line << endl;
|
|---|
| 155 | terminate();
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | entry->info.isUsed = false;
|
|---|
| 159 | Memory::deleteEntry(entry);
|
|---|
| 160 |
|
|---|
| 161 | free((char*)ptr-entrySpace);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | void operator delete(void *ptr,const char*, int) throw() {
|
|---|
| 165 | operator delete(ptr);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | void operator delete[](void *ptr){
|
|---|
| 169 | operator delete(ptr);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | void operator delete[](void *ptr,const char*, int) throw(){
|
|---|
| 173 | operator delete(ptr);
|
|---|
| 174 | }
|
|---|