source: src/Helpers/MemDebug.hpp@ 26c242

Last change on this file since 26c242 was 26c242, checked in by Frederik Heber <heber@…>, 14 years ago

FIX: Embraced troublesome includes in MemDebug.hpp with ifdefs.

  • This includes them only when they are present.
  • Note that (this is to fix compile problems with LinearAlgebra).
    • Thread depends on optional and shared.
    • bind was missing.
  • Property mode set to 100644
File size: 3.4 KB
Line 
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#endif
62#ifdef HAVE_BOOST_BIND_HPP
63#include <boost/bind.hpp>
64#endif
65#if defined HAVE_BOOST_FUNCTION_HPP || defined HAVE_BOOST_THREAD_HPP
66#include <boost/function.hpp>
67#endif
68#ifdef HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP
69// serialization has bug with overloaded new, see https://svn.boost.org/trac/boost/ticket/3400
70#include <boost/serialization/access.hpp>
71#endif
72#if defined HAVE_VALARRAY || defined HAVE_BOOST_ARCHIVE_TEXT_OARCHIVE_HPP
73// valarray uses specific new as well
74#include <valarray>
75#endif
76
77
78namespace Memory {
79
80 /**
81 * Displays a short summary of the memory state.
82 */
83 void getState();
84 void dumpMemory(std::ostream&);
85
86 void _ignore(void*);
87
88 /**
89 * Use this to disable memory for a certain pointer on the heap.
90 * This is useful for pointers which should live over the run of the
91 * program, and which are deleted automatically at the end. Usually these
92 * pointers should be wrapped inside some kind of smart pointer to
93 * ensure destruction at the end.
94 */
95 template <typename T>
96 T *ignore(T* ptr){
97 _ignore((void*)ptr);
98 return ptr;
99 }
100}
101#ifdef __GNUC__
102void *operator new (size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc);
103void *operator new[] (size_t nbytes,const char* file, int line, const char* func) throw(std::bad_alloc);
104#else
105void *operator new (size_t nbytes,const char* file, int line) throw(std::bad_alloc);
106void *operator new[] (size_t nbytes,const char* file, int line) throw(std::bad_alloc);
107#endif
108void operator delete (void *ptr,const char*, int) throw();
109void operator delete[] (void *ptr,const char*, int) throw();
110
111
112
113/**
114 * This macro replaces all occurences of the keyword new with a replaced
115 * version that allows tracking.
116 */
117#ifdef __GNUC__
118#define new new(__FILE__,__LINE__,__PRETTY_FUNCTION__)
119#else
120#define new new(__FILE__,__LINE__)
121#endif
122
123#else
124
125#include <iosfwd>
126
127// memory debugging was disabled
128
129namespace Memory {
130 inline void getState(){};
131
132 inline void dumpMemory(std::ostream&){};
133
134 template <typename T>
135 inline T *ignore(T* ptr){
136 return ptr;
137 }
138}
139
140#endif
141
142
143#endif /* MEMDEBUG_HPP_ */
Note: See TracBrowser for help on using the repository browser.