Rev | Line | |
---|
[176] | 1 | /* -*- C++ -*- */ |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | #ifndef _DEBUGHEAP_H_ |
---|
| 5 | #define _DEBUGHEAP_H_ |
---|
| 6 | |
---|
| 7 | #include <assert.h> |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * |
---|
| 11 | * |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | namespace HL { |
---|
| 15 | |
---|
| 16 | template <class Super, |
---|
| 17 | char freeChar = 'F'> |
---|
| 18 | class DebugHeap : public Super { |
---|
| 19 | private: |
---|
| 20 | |
---|
| 21 | enum { CANARY = 0xdeadbeef }; |
---|
| 22 | |
---|
| 23 | public: |
---|
| 24 | |
---|
| 25 | // Fill with A's. |
---|
| 26 | inline void * malloc (size_t sz) { |
---|
| 27 | // Add a guard area at the end. |
---|
| 28 | void * ptr; |
---|
| 29 | ptr = Super::malloc (sz + sizeof(unsigned long)); |
---|
| 30 | memset (ptr, 'A', sz); |
---|
| 31 | *((unsigned long *) ((char *) ptr + sz)) = (unsigned long) CANARY; |
---|
| 32 | assert (Super::getSize(ptr) >= sz); |
---|
| 33 | return ptr; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | // Fill with F's. |
---|
| 37 | inline void free (void * ptr) { |
---|
| 38 | char * b = (char *) ptr; |
---|
| 39 | size_t sz = Super::getSize(ptr); |
---|
| 40 | // Check for the canary. |
---|
| 41 | unsigned long storedCanary = *((unsigned long *) b + sz - sizeof(unsigned long)); |
---|
| 42 | if (storedCanary != CANARY) { |
---|
| 43 | abort(); |
---|
| 44 | } |
---|
| 45 | memset (ptr, freeChar, sz); |
---|
| 46 | Super::free (ptr); |
---|
| 47 | } |
---|
| 48 | }; |
---|
| 49 | |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.