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