| [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>
 | 
|---|
| [26c242] | 56 | #if defined HAVE_BOOST_OPTIONAL_HPP || defined HAVE_BOOST_THREAD_HPP
 | 
|---|
| [a80f419] | 57 | #include <boost/optional.hpp>
 | 
|---|
| [26c242] | 58 | #endif
 | 
|---|
 | 59 | #if defined HAVE_BOOST_SHARED_PTR_HPP || defined HAVE_BOOST_THREAD_HPP
 | 
|---|
| [a80f419] | 60 | #include <boost/shared_ptr.hpp>
 | 
|---|
| [63e9ca] | 61 | #include <boost/make_shared.hpp>
 | 
|---|
| [26c242] | 62 | #endif
 | 
|---|
 | 63 | #ifdef HAVE_BOOST_BIND_HPP
 | 
|---|
 | 64 | #include <boost/bind.hpp>
 | 
|---|
 | 65 | #endif
 | 
|---|
 | 66 | #if defined HAVE_BOOST_FUNCTION_HPP || defined HAVE_BOOST_THREAD_HPP
 | 
|---|
| [a80f419] | 67 | #include <boost/function.hpp>
 | 
|---|
| [26c242] | 68 | #endif
 | 
|---|
 | 69 | #ifdef HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP
 | 
|---|
| [a2392b] | 70 | // serialization has bug with overloaded new, see https://svn.boost.org/trac/boost/ticket/3400
 | 
|---|
 | 71 | #include <boost/serialization/access.hpp>
 | 
|---|
| [26c242] | 72 | #endif
 | 
|---|
 | 73 | #if defined HAVE_VALARRAY || defined HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP
 | 
|---|
| [cc78ae] | 74 | // valarray uses specific new as well
 | 
|---|
 | 75 | #include <valarray>
 | 
|---|
| [26c242] | 76 | #endif
 | 
|---|
| [e7352a5] | 77 | #if defined HAVE_BOOST_MULTI_ARRAY_HPP
 | 
|---|
 | 78 | // multiarray uses type trait has_new_operator.hpp
 | 
|---|
 | 79 | #include <boost/type_traits/has_new_operator.hpp>
 | 
|---|
 | 80 | #endif
 | 
|---|
| [a80f419] | 81 | 
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 | namespace Memory {
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 |   /**
 | 
|---|
 | 86 |    * Displays a short summary of the memory state.
 | 
|---|
 | 87 |    */
 | 
|---|
 | 88 |   void getState();
 | 
|---|
 | 89 |   void dumpMemory(std::ostream&);
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |   void _ignore(void*);
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 |   /**
 | 
|---|
 | 94 |    * Use this to disable memory for a certain pointer on the heap.
 | 
|---|
 | 95 |    * This is useful for pointers which should live over the run of the
 | 
|---|
 | 96 |    * program, and which are deleted automatically at the end. Usually these
 | 
|---|
 | 97 |    * pointers should be wrapped inside some kind of smart pointer to
 | 
|---|
 | 98 |    * ensure destruction at the end.
 | 
|---|
 | 99 |    */
 | 
|---|
 | 100 |   template <typename T>
 | 
|---|
 | 101 |   T *ignore(T* ptr){
 | 
|---|
 | 102 |     _ignore((void*)ptr);
 | 
|---|
 | 103 |     return ptr;
 | 
|---|
 | 104 |   }
 | 
|---|
 | 105 | }
 | 
|---|
 | 106 | #ifdef __GNUC__
 | 
|---|
 | 107 | void *operator new   (size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc);
 | 
|---|
 | 108 | void *operator new[] (size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc);
 | 
|---|
 | 109 | #else
 | 
|---|
 | 110 | void *operator new   (size_t nbytes,const char* file, int line) throw(std::bad_alloc);
 | 
|---|
 | 111 | void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc);
 | 
|---|
 | 112 | #endif
 | 
|---|
 | 113 | void operator delete   (void *ptr,const char*, int) throw();
 | 
|---|
 | 114 | void operator delete[] (void *ptr,const char*, int) throw();
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | /**
 | 
|---|
 | 119 |  * This macro replaces all occurences of the keyword new with a replaced
 | 
|---|
 | 120 |  * version that allows tracking.
 | 
|---|
 | 121 |  */
 | 
|---|
 | 122 | #ifdef __GNUC__
 | 
|---|
 | 123 | #define new new(__FILE__,__LINE__,__PRETTY_FUNCTION__)
 | 
|---|
 | 124 | #else
 | 
|---|
 | 125 | #define new new(__FILE__,__LINE__)
 | 
|---|
 | 126 | #endif
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 | #else
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | #include <iosfwd>
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | // memory debugging was disabled
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 | namespace Memory {
 | 
|---|
 | 135 |   inline void getState(){};
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 |   inline void dumpMemory(std::ostream&){};
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |   template <typename T>
 | 
|---|
 | 140 |   inline T *ignore(T* ptr){
 | 
|---|
 | 141 |     return ptr;
 | 
|---|
 | 142 |   }
 | 
|---|
 | 143 | }
 | 
|---|
 | 144 | 
 | 
|---|
 | 145 | #endif
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 | 
 | 
|---|
 | 148 | #endif /* MEMDEBUG_HPP_ */
 | 
|---|