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