| [a80f419] | 1 | /*
 | 
|---|
 | 2 |  * MemDebug.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Apr 28, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef MEMDEBUG_HPP_
 | 
|---|
 | 9 | #define MEMDEBUG_HPP_
 | 
|---|
 | 10 | 
 | 
|---|
| [70672e3] | 11 | // include config.h
 | 
|---|
 | 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 13 | #include <config.h>
 | 
|---|
 | 14 | #endif
 | 
|---|
 | 15 | 
 | 
|---|
| [a80f419] | 16 | /**
 | 
|---|
 | 17 |  * @file
 | 
|---|
 | 18 |  * This module allows easy automatic memory tracking. Link this module to replace the
 | 
|---|
 | 19 |  * operators new, new[], delete and delete[] with custom versions that add tracking
 | 
|---|
 | 20 |  * information to allocated blocks.
 | 
|---|
 | 21 |  *
 | 
|---|
 | 22 |  * All tracking is done in O(1) for new and delete operators. Summaries for the
 | 
|---|
 | 23 |  * used memory take O(N), where N is the number of currently allocated memory chunks.
 | 
|---|
 | 24 |  *
 | 
|---|
 | 25 |  * To use full tracking including file name and line number include this file in
 | 
|---|
 | 26 |  * your sourcefiles.
 | 
|---|
 | 27 |  */
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | // Set all flags in a way that makes sense
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | // NDEBUG implies NO_MEMDEBUG
 | 
|---|
 | 32 | #ifdef NDEBUG
 | 
|---|
 | 33 | # ifndef NO_MEMDEBUG
 | 
|---|
 | 34 | #   define NO_MEMDEBUG
 | 
|---|
 | 35 | # endif
 | 
|---|
 | 36 | #endif
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | // NO_MEMDEBUG and MEMDEBUG are mutually exclusive, but at least one must be set
 | 
|---|
 | 39 | #ifdef NO_MEMDEBUG
 | 
|---|
 | 40 | # ifdef MEMDEBUG
 | 
|---|
 | 41 | #   undef MEMDEBUG
 | 
|---|
 | 42 | # endif
 | 
|---|
 | 43 | #else
 | 
|---|
 | 44 | # ifndef MEMDEBUG
 | 
|---|
 | 45 | #   define MEMDEBUG
 | 
|---|
 | 46 | # endif
 | 
|---|
 | 47 | #endif
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | #ifdef MEMDEBUG
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | #include <new>
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 | // some light header files, that do weird new stuff and therefore need
 | 
|---|
 | 54 | // to be loaded before the define
 | 
|---|
 | 55 | #include <string>
 | 
|---|
 | 56 | #include <boost/optional.hpp>
 | 
|---|
 | 57 | #include <boost/shared_ptr.hpp>
 | 
|---|
 | 58 | #include <boost/function.hpp>
 | 
|---|
 | 59 | 
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 | namespace Memory {
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 |   /**
 | 
|---|
 | 64 |    * Displays a short summary of the memory state.
 | 
|---|
 | 65 |    */
 | 
|---|
 | 66 |   void getState();
 | 
|---|
 | 67 |   void dumpMemory(std::ostream&);
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 |   void _ignore(void*);
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 |   /**
 | 
|---|
 | 72 |    * Use this to disable memory for a certain pointer on the heap.
 | 
|---|
 | 73 |    * This is useful for pointers which should live over the run of the
 | 
|---|
 | 74 |    * program, and which are deleted automatically at the end. Usually these
 | 
|---|
 | 75 |    * pointers should be wrapped inside some kind of smart pointer to
 | 
|---|
 | 76 |    * ensure destruction at the end.
 | 
|---|
 | 77 |    */
 | 
|---|
 | 78 |   template <typename T>
 | 
|---|
 | 79 |   T *ignore(T* ptr){
 | 
|---|
 | 80 |     _ignore((void*)ptr);
 | 
|---|
 | 81 |     return ptr;
 | 
|---|
 | 82 |   }
 | 
|---|
 | 83 | }
 | 
|---|
 | 84 | #ifdef __GNUC__
 | 
|---|
 | 85 | void *operator new   (size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc);
 | 
|---|
 | 86 | void *operator new[] (size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc);
 | 
|---|
 | 87 | #else
 | 
|---|
 | 88 | void *operator new   (size_t nbytes,const char* file, int line) throw(std::bad_alloc);
 | 
|---|
 | 89 | void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc);
 | 
|---|
 | 90 | #endif
 | 
|---|
 | 91 | void operator delete   (void *ptr,const char*, int) throw();
 | 
|---|
 | 92 | void operator delete[] (void *ptr,const char*, int) throw();
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | /**
 | 
|---|
 | 97 |  * This macro replaces all occurences of the keyword new with a replaced
 | 
|---|
 | 98 |  * version that allows tracking.
 | 
|---|
 | 99 |  */
 | 
|---|
 | 100 | #ifdef __GNUC__
 | 
|---|
 | 101 | #define new new(__FILE__,__LINE__,__PRETTY_FUNCTION__)
 | 
|---|
 | 102 | #else
 | 
|---|
 | 103 | #define new new(__FILE__,__LINE__)
 | 
|---|
 | 104 | #endif
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | #else
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | #include <iosfwd>
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 | // memory debugging was disabled
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 | namespace Memory {
 | 
|---|
 | 113 |   inline void getState(){};
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 |   inline void dumpMemory(std::ostream&){};
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 |   template <typename T>
 | 
|---|
 | 118 |   inline T *ignore(T* ptr){
 | 
|---|
 | 119 |     return ptr;
 | 
|---|
 | 120 |   }
 | 
|---|
 | 121 | }
 | 
|---|
 | 122 | 
 | 
|---|
 | 123 | #endif
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 | #endif /* MEMDEBUG_HPP_ */
 | 
|---|