| 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 | 
 | 
|---|
| 11 | // include config.h
 | 
|---|
| 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 13 | #include <config.h>
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 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 | #if defined HAVE_BOOST_OPTIONAL_HPP || defined HAVE_BOOST_THREAD_HPP
 | 
|---|
| 57 | #include <boost/optional.hpp>
 | 
|---|
| 58 | #endif
 | 
|---|
| 59 | #if defined HAVE_BOOST_SHARED_PTR_HPP || defined HAVE_BOOST_THREAD_HPP
 | 
|---|
| 60 | #include <boost/shared_ptr.hpp>
 | 
|---|
| 61 | #include <boost/make_shared.hpp>
 | 
|---|
| 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
 | 
|---|
| 67 | #include <boost/function.hpp>
 | 
|---|
| 68 | #endif
 | 
|---|
| 69 | #ifdef HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP
 | 
|---|
| 70 | // serialization has bug with overloaded new, see https://svn.boost.org/trac/boost/ticket/3400
 | 
|---|
| 71 | #include <boost/serialization/access.hpp>
 | 
|---|
| 72 | #include <boost/archive/detail/iserializer.hpp>
 | 
|---|
| 73 | #endif
 | 
|---|
| 74 | #if defined HAVE_VALARRAY || defined HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP
 | 
|---|
| 75 | // valarray uses specific new as well
 | 
|---|
| 76 | #include <valarray>
 | 
|---|
| 77 | #endif
 | 
|---|
| 78 | #if defined HAVE_BOOST_MULTI_ARRAY_HPP || defined HAVE_BOOST_ASIO_HPP
 | 
|---|
| 79 | // multiarray uses type trait has_new_operator.hpp
 | 
|---|
| 80 | #include <boost/type_traits/has_new_operator.hpp>
 | 
|---|
| 81 | #endif
 | 
|---|
| 82 | #if defined HAVE_BOOST_ASIO_HPP || defined HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP
 | 
|---|
| 83 | // boost asio and its archives use own allocator
 | 
|---|
| 84 | #include <boost/serialization/shared_ptr_132.hpp>
 | 
|---|
| 85 | #endif
 | 
|---|
| 86 | #if defined HAVE_BOOST_MPL_FOR_EACH_HPP
 | 
|---|
| 87 | // boost mpl requires the value_init which uses placement new
 | 
|---|
| 88 | #include <boost/utility/value_init.hpp>
 | 
|---|
| 89 | #endif
 | 
|---|
| 90 | #if defined HAVE_BOOST_EXCEPTION_ALL_HPP
 | 
|---|
| 91 | // boost exceptions has two inaccessible static objects for bad_alloc and bad_exception
 | 
|---|
| 92 | #include <boost/exception/detail/exception_ptr.hpp>
 | 
|---|
| 93 | #endif
 | 
|---|
| 94 | #if defined HAVE_BOOST_FOREACH_HPP
 | 
|---|
| 95 | // boost foreach uses placement new
 | 
|---|
| 96 | #include <boost/foreach.hpp>
 | 
|---|
| 97 | #endif
 | 
|---|
| 98 | #if defined HAVE_BOOST_BIMAP_HPP
 | 
|---|
| 99 | // boost bimap uses placement new
 | 
|---|
| 100 | #include <boost/detail/allocator_utilities.hpp>
 | 
|---|
| 101 | #endif
 | 
|---|
| 102 | #if defined HAVE_BOOST_MULTI_INDEX_CONTAINER_HPP
 | 
|---|
| 103 | // multi_index with vartempl uses placement new
 | 
|---|
| 104 | #include <boost/multi_index_container.hpp>
 | 
|---|
| 105 | #endif
 | 
|---|
| 106 | 
 | 
|---|
| 107 | 
 | 
|---|
| 108 | namespace Memory {
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   /**
 | 
|---|
| 111 |    * Displays a short summary of the memory state.
 | 
|---|
| 112 |    */
 | 
|---|
| 113 |   void getState();
 | 
|---|
| 114 |   void dumpMemory(std::ostream&);
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   void _ignore(void*);
 | 
|---|
| 117 | 
 | 
|---|
| 118 |   /**
 | 
|---|
| 119 |    * Use this to disable memory for a certain pointer on the heap.
 | 
|---|
| 120 |    * This is useful for pointers which should live over the run of the
 | 
|---|
| 121 |    * program, and which are deleted automatically at the end. Usually these
 | 
|---|
| 122 |    * pointers should be wrapped inside some kind of smart pointer to
 | 
|---|
| 123 |    * ensure destruction at the end.
 | 
|---|
| 124 |    */
 | 
|---|
| 125 |   template <typename T>
 | 
|---|
| 126 |   T *ignore(T* ptr){
 | 
|---|
| 127 |     _ignore((void*)ptr);
 | 
|---|
| 128 |     return ptr;
 | 
|---|
| 129 |   }
 | 
|---|
| 130 | }
 | 
|---|
| 131 | #ifdef __GNUC__
 | 
|---|
| 132 | void *operator new   (size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc);
 | 
|---|
| 133 | void *operator new[] (size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc);
 | 
|---|
| 134 | #else
 | 
|---|
| 135 | void *operator new   (size_t nbytes,const char* file, int line) throw(std::bad_alloc);
 | 
|---|
| 136 | void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc);
 | 
|---|
| 137 | #endif
 | 
|---|
| 138 | void operator delete   (void *ptr,const char*, int) throw();
 | 
|---|
| 139 | void operator delete[] (void *ptr,const char*, int) throw();
 | 
|---|
| 140 | 
 | 
|---|
| 141 | 
 | 
|---|
| 142 | 
 | 
|---|
| 143 | /**
 | 
|---|
| 144 |  * This macro replaces all occurences of the keyword new with a replaced
 | 
|---|
| 145 |  * version that allows tracking.
 | 
|---|
| 146 |  */
 | 
|---|
| 147 | #ifdef __GNUC__
 | 
|---|
| 148 | #define new new(__FILE__,__LINE__,__PRETTY_FUNCTION__)
 | 
|---|
| 149 | #else
 | 
|---|
| 150 | #define new new(__FILE__,__LINE__)
 | 
|---|
| 151 | #endif
 | 
|---|
| 152 | 
 | 
|---|
| 153 | #else
 | 
|---|
| 154 | 
 | 
|---|
| 155 | #include <iosfwd>
 | 
|---|
| 156 | 
 | 
|---|
| 157 | // memory debugging was disabled
 | 
|---|
| 158 | 
 | 
|---|
| 159 | namespace Memory {
 | 
|---|
| 160 |   inline void getState(){};
 | 
|---|
| 161 | 
 | 
|---|
| 162 |   inline void dumpMemory(std::ostream&){};
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   template <typename T>
 | 
|---|
| 165 |   inline T *ignore(T* ptr){
 | 
|---|
| 166 |     return ptr;
 | 
|---|
| 167 |   }
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | #endif
 | 
|---|
| 171 | 
 | 
|---|
| 172 | 
 | 
|---|
| 173 | #endif /* MEMDEBUG_HPP_ */
 | 
|---|