00001
00002
00003
00004 #ifndef _DEBUGHEAP_H_
00005 #define _DEBUGHEAP_H_
00006
00007 #include <assert.h>
00008
00014 namespace HL {
00015
00016 template <class Super,
00017 char freeChar = 'F'>
00018 class DebugHeap : public Super {
00019 private:
00020
00021 enum { CANARY = 0xdeadbeef };
00022
00023 public:
00024
00025
00026 inline void * malloc (size_t sz) {
00027
00028 void * ptr;
00029 ptr = Super::malloc (sz + sizeof(unsigned long));
00030 for (int i = 0; i < sz; i++) {
00031 ((char *) ptr)[i] = 'A';
00032 }
00033 *((unsigned long *) ((char *) ptr + sz)) = (unsigned long) CANARY;
00034 assert (Super::getSize(ptr) >= sz);
00035 return ptr;
00036 }
00037
00038
00039 inline void free (void * ptr) {
00040 char * b = (char *) ptr;
00041 size_t sz = Super::getSize(ptr);
00042
00043 unsigned long storedCanary = *((unsigned long *) b + sz - sizeof(unsigned long));
00044 if (storedCanary != CANARY) {
00045 abort();
00046 }
00047 for (int i = 0; i < sz; i++) {
00048 ((char *) ptr)[i] = freeChar;
00049 }
00050 Super::free (ptr);
00051 }
00052 };
00053
00054 }
00055
00056 #endif