Chombo + EB  3.2
memtrack.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _MEMTRACK_H_
12 #define _MEMTRACK_H_
13 
14 #ifdef CH_USE_MEMORY_TRACKING
15 
16 #include <iostream>
17 using std::ostream;
18 
19 #include "REAL.H"
20 
21 // NOTE: These were added to avoid macro substitutions of "calloc(a,b)",
22 // "realloc(a,b)", and "free(a)" for member function(s) of the "Arena" class.
23 //
24 // These member function(s) are declared in "Arena.H" and used
25 // "BaseFabImplem.H" (which is included via "BaseFab.H").
26 #include "Arena.H"
27 //#include "BaseFab.H"
28 
29 // Use these macros to include in memory tracking system
30 #define callocMT(a_a,a_b) callocp (a_a, a_b, __FILE__, __LINE__)
31 #define reallocMT(a_a, a_b) reallocp(a_a, a_b, __FILE__, __LINE__)
32 #define mallocMT(a_a) mallocp (a_a, __FILE__, __LINE__)
33 #define freeMT(a_a) freep (a_a)
34 
35 #include "BaseNamespaceHeader.H"
36 
37 extern unsigned long long int ch_memcount;
38 ///
39 void dumpmemorymap(int a_sig);
40 
41 ///
42 void dumpmemoryatexit();
43 
44 ///
45 void dumpmemoryabort();
46 
47 ///
48 int registerMemorySignals();
49 
50 ///
51 void ReportUnfreedMemory(ostream& a_os);
52 
53 ///
54 void ReportAllocatedMemory(ostream& a_os);
55 
56 ///
57 void ReportMemoryUsage(ostream& a_os);
58 
59 /// calls ReportUnfreedMemory with pout()
60 void UnfreedMemory();
61 
62 /// calls ReportMemoryUsage with pout()
63 void MemoryUsage();
64 
65 ///
66 void memTrackingOn();
67 
68 ///
69 void memtrackingOff();
70 
71 void overallMemoryUsage(long long& a_currentTotal,
72  long long& a_peak);
73 
74 void overallMemoryUsage();
75 
76 void memtrackStamp(Real& a_current,
77  Real& a_peak);
78 
79 /// Memory tracking functions
80 /**
81  Memory tracking functions
82 */
83 
84 class Memtrack
85 {
86 public:
87  /// Produce a formated output onto os of memory usage.
88  /**
89  Memory is reported one a class-by-class basis. When
90  CH_USE_MEMORY_TRACKING is turned on you also get a report from
91  the atexit() function. This can be handy for spotting memory
92  leaks. The memory tracking functionality consumes very little
93  runtime and does not impede optimized performance.
94  */
95  static void ReportUnfreedMemory(ostream& a_os);
96 
97  /// calls ReportUnfreedMemory with pout()
98  static void UnfreedMemory();
99 
100  static void memTrackingOn();
101 
102  static void memtrackingOff();
103 
104  static void overallMemoryUsage(long long& a_currentTotal,
105  long long& a_peak);
106  static std::vector<void (*)()> s_memtrackAtExit;
107 };
108 
109 // =========== end user functions.===================================
110 
111 void AddTrack(void* a_addr,
112  size_t a_asize,
113  const char* a_fname,
114  int a_lnum,
115  bool a_malloc);
116 
117 void RemoveTrack(void* a_addr,
118  bool a_malloc);
119 
120 // void* operator new (size_t a_size,
121 // char const* a_file,
122 // int a_line);
123 
124 // void operator delete (void *a_p) throw();
125 
126 // void* operator new[] (size_t a_size,
127 // char const* a_file,
128 // int a_line);
129 
130 // void operator delete[] (void *a_p) throw();
131 
132 // Don't call these directly -- use the callocMT/mallocMT/freeMT macros.
133 // Use these functions to track the source file and line number of particular
134 // memory alloc/frees.
135 void* mallocp(size_t a_size,
136  const char* a_file,
137  int a_line);
138 
139 void* reallocp(void* a_p,
140  size_t a_size,
141  const char* a_file,
142  int a_line);
143 
144 void* callocp(size_t a_nelem,
145  size_t a_elsize,
146  const char* a_file,
147  int a_line);
148 
149 void freep(void* a_p);
150 
151 #include "BaseNamespaceFooter.H"
152 
153 #else // on CH_USE_MEMORY_TRACKING
154 
155 // If not compiling with memory tracking, then just call the LIBC versions
156 #include <cstdlib>
157 #define callocMT(a_a,a_b) calloc (a_a, a_b)
158 #define reallocMT(a_a, a_b) realloc(a_a, a_b)
159 #define mallocMT(a_a) malloc (a_a)
160 #define freeMT(a_a) free (a_a)
161 
162 inline void AddTrack(void* a_addr,
163  size_t a_asize,
164  const char* a_fname,
165  int a_lnum,
166  bool a_malloc)
167 {
168  // null op
169 }
170 
171 inline void RemoveTrack(void* a_addr,
172  bool a_malloc)
173 {
174  // null op
175 }
176 
177 #endif // ifdef CH_USE_MEMORY_TRACKING
178 
179 #endif // include guard
void RemoveTrack(void *a_addr, bool a_malloc)
Definition: memtrack.H:171
double Real
Definition: REAL.H:33
void AddTrack(void *a_addr, size_t a_asize, const char *a_fname, int a_lnum, bool a_malloc)
Definition: memtrack.H:162
void dumpmemoryatexit()