Chombo + EB  3.0
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 
12 #ifndef _MEMTRACK_H_
13 #define _MEMTRACK_H_
14 
15 #ifdef CH_USE_MEMORY_TRACKING
16 
17 #include <iostream>
18 using std::ostream;
19 
20 #include "REAL.H"
21 
22 // NOTE: These were added to avoid macro substitutions of "calloc(a,b)",
23 // "realloc(a,b)", and "free(a)" for member function(s) of the "Arena" class.
24 //
25 // These member function(s) are declared in "Arena.H" and used
26 // "BaseFabImplem.H" (which is included via "BaseFab.H").
27 #include "Arena.H"
28 //#include "BaseFab.H"
29 
30 // Use these macros to include in memory tracking system
31 #define callocMT(a_a,a_b) callocp (a_a, a_b, __FILE__, __LINE__)
32 #define reallocMT(a_a, a_b) reallocp(a_a, a_b, __FILE__, __LINE__)
33 #define mallocMT(a_a) mallocp (a_a, __FILE__, __LINE__)
34 #define freeMT(a_a) freep (a_a)
35 
36 #include "BaseNamespaceHeader.H"
37 
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 };
107 
108 // =========== end user functions.===================================
109 
110 void AddTrack(void* a_addr,
111  size_t a_asize,
112  const char* a_fname,
113  int a_lnum,
114  bool a_malloc);
115 
116 void RemoveTrack(void* a_addr,
117  bool a_malloc);
118 
119 // void* operator new (size_t a_size,
120 // char const* a_file,
121 // int a_line);
122 
123 // void operator delete (void *a_p) throw();
124 
125 // void* operator new[] (size_t a_size,
126 // char const* a_file,
127 // int a_line);
128 
129 // void operator delete[] (void *a_p) throw();
130 
131 // Don't call these directly -- use the callocMT/mallocMT/freeMT macros.
132 // Use these functions to track the source file and line number of particular
133 // memory alloc/frees.
134 void* mallocp(size_t a_size,
135  const char* a_file,
136  int a_line);
137 
138 void* reallocp(void* a_p,
139  size_t a_size,
140  const char* a_file,
141  int a_line);
142 
143 void* callocp(size_t a_nelem,
144  size_t a_elsize,
145  const char* a_file,
146  int a_line);
147 
148 void freep(void* a_p);
149 
150 #include "BaseNamespaceFooter.H"
151 
152 #else // on CH_USE_MEMORY_TRACKING
153 
154 // If not compiling with memory tracking, then just call the LIBC versions
155 #include <malloc.h>
156 #define callocMT(a_a,a_b) calloc (a_a, a_b)
157 #define reallocMT(a_a, a_b) realloc(a_a, a_b)
158 #define mallocMT(a_a) malloc (a_a)
159 #define freeMT(a_a) free (a_a)
160 
161 inline void AddTrack(void* a_addr,
162  size_t a_asize,
163  const char* a_fname,
164  int a_lnum,
165  bool a_malloc)
166 {
167  // null op
168 }
169 
170 inline void RemoveTrack(void* a_addr,
171  bool a_malloc)
172 {
173  // null op
174 }
175 
176 #endif // ifdef CH_USE_MEMORY_TRACKING
177 
178 #endif // include guard
void RemoveTrack(void *a_addr, bool a_malloc)
Definition: memtrack.H:170
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:161
void dumpmemoryatexit()