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 |
|
---|
15 | // This struct is added before each memory chunk
|
---|
16 | // and contains tracking information. Anything used
|
---|
17 | // to track memory cannot use any dynamic memory, so
|
---|
18 | // we have to resort to classic C-idioms here.
|
---|
19 | // This struct also contains pointers to the next
|
---|
20 | // an previous chunks to allow fast traversion of
|
---|
21 | // all allocated memory blocks
|
---|
22 | struct entry_t {
|
---|
23 | // we seperate the tracking info from the rest
|
---|
24 | // A checksum will be calculated for this part of
|
---|
25 | // the struct, so the information in here should
|
---|
26 | // not change during the lifetime of the memory
|
---|
27 | struct info_t {
|
---|
28 | enum {length = 64};
|
---|
29 | char file[length];
|
---|
30 | int line;
|
---|
31 | size_t nbytes;
|
---|
32 | bool isUsed;
|
---|
33 | void *location;
|
---|
34 | } info;
|
---|
35 | bool isIgnored;
|
---|
36 | char checksum;
|
---|
37 | entry_t *prev;
|
---|
38 | entry_t *next;
|
---|
39 | };
|
---|
40 |
|
---|
41 | // start and end of the doubly-linked list
|
---|
42 | entry_t *begin=0;
|
---|
43 | entry_t *end=0;
|
---|
44 |
|
---|
45 | // current amount of allocated memory
|
---|
46 | size_t state = 0;
|
---|
47 | // maximum amount of allocated memory
|
---|
48 | size_t max = 0;
|
---|
49 | // number of allocations that have been done so far
|
---|
50 | unsigned int allocs = 0;
|
---|
51 |
|
---|
52 |
|
---|
53 | // this sets the alignment of the returned memory block
|
---|
54 | // malloc guarantees an alignment at the 8 byte border,
|
---|
55 | // so we just do the same
|
---|
56 | const int alignment = 8;
|
---|
57 |
|
---|
58 | // calculates a simple checksum for the info block
|
---|
59 | // the checksum is used to find memory corruptions
|
---|
60 | inline char calcChecksum(entry_t::info_t *info){
|
---|
61 | char *buffer = (char*)info;
|
---|
62 | char checksum =0;
|
---|
63 | for(size_t i=0;i<sizeof(entry_t::info_t);i++){
|
---|
64 | checksum+=buffer[i];
|
---|
65 | }
|
---|
66 | return checksum;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // gets the next alignet point which is greater than nbytes
|
---|
70 | // this function is only called a fixed number of times, so
|
---|
71 | // there is no need to optimize
|
---|
72 | inline size_t doAlign(size_t nbytes){
|
---|
73 | int nonaligned = nbytes % alignment;
|
---|
74 | if(nonaligned) {
|
---|
75 | return(nbytes - nonaligned + alignment);
|
---|
76 | }
|
---|
77 | else{
|
---|
78 | return nbytes;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | // Output some state information
|
---|
83 | void getState(){
|
---|
84 | cout << "Maximum allocated Memory: " << max << " bytes" << endl;
|
---|
85 | cout << "Currently allocated Memory: " << state <<" bytes" << endl;
|
---|
86 | cout << allocs << " allocated chunks total" << endl;
|
---|
87 |
|
---|
88 | // simple traversal of the chunk list
|
---|
89 | for(entry_t *pos=begin;pos;pos=pos->next){
|
---|
90 | cout << "\nChunk of " << pos->info.nbytes << " bytes" << " still available" << endl;
|
---|
91 | cout << "Chunk reserved at: " << pos->info.file << ":" << pos->info.line << endl;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | // Deletes an entry from the linked list
|
---|
96 | void deleteEntry(entry_t *entry){
|
---|
97 | if(entry->isIgnored)
|
---|
98 | return;
|
---|
99 |
|
---|
100 | if(entry->prev){
|
---|
101 | entry->prev->next = entry->next;
|
---|
102 | }
|
---|
103 | else{
|
---|
104 | // this node was the beginning of the list
|
---|
105 | begin = entry->next;
|
---|
106 | }
|
---|
107 |
|
---|
108 | if(entry->next){
|
---|
109 | entry->next->prev = entry->prev;
|
---|
110 | }
|
---|
111 | else{
|
---|
112 | // this node was the end of the list
|
---|
113 | end = entry->prev;
|
---|
114 | }
|
---|
115 | entry->isIgnored = true;
|
---|
116 | Memory::state -= entry->info.nbytes;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void _ignore(void *ptr){
|
---|
120 | // just deletes the node from the list, but leaves the info intact
|
---|
121 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
---|
122 | entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
|
---|
123 | deleteEntry(entry);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | void *operator new(size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
|
---|
128 |
|
---|
129 | // to avoid allocations of 0 bytes if someone screws up
|
---|
130 | // allocation with 0 byte size are undefined behavior, so we are
|
---|
131 | // free to handle it this way
|
---|
132 | if(!nbytes) {
|
---|
133 | nbytes = 1;
|
---|
134 | }
|
---|
135 |
|
---|
136 | // get the size of the entry, including alignment
|
---|
137 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
---|
138 |
|
---|
139 | void *res;
|
---|
140 | if(!(res=malloc(entrySpace + nbytes))){
|
---|
141 | // new must throw, when space is low
|
---|
142 | throw std::bad_alloc();
|
---|
143 | }
|
---|
144 |
|
---|
145 | // we got the space, so update the global info
|
---|
146 | Memory::state += nbytes;
|
---|
147 | if(Memory::state>Memory::max){
|
---|
148 | Memory::max = Memory::state;
|
---|
149 | }
|
---|
150 | Memory::allocs++;
|
---|
151 |
|
---|
152 | // build the entry in front of the space
|
---|
153 | Memory::entry_t *entry = (Memory::entry_t*) res;
|
---|
154 | entry->info.nbytes = nbytes;
|
---|
155 | entry->info.isUsed = true;
|
---|
156 | strncpy(entry->info.file,file,Memory::entry_t::info_t::length);
|
---|
157 | entry->info.file[255] = '\0';
|
---|
158 | entry->info.line=line;
|
---|
159 | // the space starts behind the info
|
---|
160 | entry->info.location = (char*)res + entrySpace;
|
---|
161 |
|
---|
162 | // add the entry at the end of the list
|
---|
163 | entry->next=0; // the created block is last in the list
|
---|
164 | entry->prev=Memory::end; // the created block is last in the list
|
---|
165 | if(!Memory::begin){
|
---|
166 | // the list was empty... start a new one
|
---|
167 | Memory::begin=entry;
|
---|
168 | }
|
---|
169 | else {
|
---|
170 | // other blocks present... we can add to the last one
|
---|
171 | Memory::end->next=entry;
|
---|
172 | }
|
---|
173 | Memory::end=entry;
|
---|
174 |
|
---|
175 | // get the checksum...
|
---|
176 | entry->checksum = Memory::calcChecksum(&entry->info);
|
---|
177 | // this will be set to true, when the block is removed from
|
---|
178 | // the list for any reason
|
---|
179 | entry->isIgnored = false;
|
---|
180 |
|
---|
181 | // ok, space is prepared... the user can have it.
|
---|
182 | // the rest (constructor, deleting when something is thrown etc)
|
---|
183 | // is handled automatically
|
---|
184 | return entry->info.location;
|
---|
185 | }
|
---|
186 |
|
---|
187 | void *operator new(size_t nbytes) throw(std::bad_alloc) {
|
---|
188 | // Just forward to the other operator, when we do not know from
|
---|
189 | // where the allocation came
|
---|
190 | return operator new(nbytes,"Unknown",0);
|
---|
191 | }
|
---|
192 |
|
---|
193 | void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc) {
|
---|
194 | // The difference between new and new[] is just for compiler bookkeeping.
|
---|
195 | return operator new(nbytes,file,line);
|
---|
196 | }
|
---|
197 |
|
---|
198 | void *operator new[] (size_t nbytes) throw(std::bad_alloc) {
|
---|
199 | // Forward again
|
---|
200 | return operator new[] (nbytes,"Unknown",0);
|
---|
201 | }
|
---|
202 |
|
---|
203 | void operator delete(void *ptr) throw() {
|
---|
204 | // get the size for the entry, including alignment
|
---|
205 | static const size_t entrySpace = Memory::doAlign(sizeof(Memory::entry_t));
|
---|
206 |
|
---|
207 | // get the position for the entry from the pointer the user gave us
|
---|
208 | Memory::entry_t *entry = (Memory::entry_t*)((char*)ptr-entrySpace);
|
---|
209 |
|
---|
210 | // let's see if the checksum is still matching
|
---|
211 | if(Memory::calcChecksum(&entry->info)!=entry->checksum){
|
---|
212 | cout << "Possible memory corruption detected!" << endl;
|
---|
213 | cout << "Trying to recover allocation information..." << endl;
|
---|
214 | cout << "Memory was allocated at " << entry->info.file << ":" << entry->info.line << endl;
|
---|
215 | terminate();
|
---|
216 | }
|
---|
217 |
|
---|
218 | // this will destroy the checksum, so double deletes are caught
|
---|
219 | entry->info.isUsed = false;
|
---|
220 | Memory::deleteEntry(entry);
|
---|
221 |
|
---|
222 | // delete the space reserved by malloc
|
---|
223 | free((char*)ptr-entrySpace);
|
---|
224 | }
|
---|
225 |
|
---|
226 | // operator that is called when the constructor throws
|
---|
227 | // do not call manually
|
---|
228 | void operator delete(void *ptr,const char*, int) throw() {
|
---|
229 | operator delete(ptr);
|
---|
230 | }
|
---|
231 |
|
---|
232 | void operator delete[](void *ptr){
|
---|
233 | // again difference between delete and delete[] is just in compiler bookkeeping
|
---|
234 | operator delete(ptr);
|
---|
235 | }
|
---|
236 |
|
---|
237 | // and another operator that can be called when a constructor throws
|
---|
238 | void operator delete[](void *ptr,const char*, int) throw(){
|
---|
239 | operator delete(ptr);
|
---|
240 | }
|
---|