PSCF v1.5.0
Memory.h
1#ifndef UTIL_MEMORY_H
2#define UTIL_MEMORY_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/global.h>
12#include <stddef.h>
13#include <iostream>
14#include <new>
15
16namespace Util
17{
18
28 class Memory
29 {
30 public:
31
44 template <typename Data>
45 static void allocate(Data*& ptr, size_t size);
46
56 template <typename Data>
57 static void add(size_t size);
58
72 template <typename Data>
73 static void deallocate(Data*& ptr, size_t size);
74
85 template <typename Data>
86 static void sub(size_t size);
87
102 template <typename Data>
103 static void reallocate(Data*& ptr, size_t oldSize, size_t newSize);
104
111 static long int nAllocate();
112
119 static long int nDeallocate();
120
124 static long int total();
125
131 static long int max();
132
133 #ifdef UTIL_MPI
137 static long int max(MPI::Intracomm& communicator);
138 #endif
139
143 static void clearCounters();
144
148 static void initStatic();
149
150 private:
151
153 static long int total_;
154
156 static long int max_;
157
159 static long int nAllocate_;
160
162 static long int nDeallocate_;
163
164 };
165
166 // Member function template definitions
167
168 /*
169 * Allocate an array.
170 */
171 template <typename Data>
172 void Memory::allocate(Data*& ptr, size_t size)
173 {
174 if (ptr) {
175 UTIL_THROW("Attempt to allocate to non-null pointer");
176 }
177 try {
178 ptr = new Data[size];
179 UTIL_CHECK(ptr);
180 } catch (std::bad_alloc&) {
181 std::cout << "Allocation error in Util::Memory" << std::endl;
182 throw;
183 }
184 add<Data>(size);
185 }
186
187 /*
188 * Add size of a newly allocated C-Array to the current total.
189 */
190 template <typename Data>
191 void Memory::add(size_t size)
192 {
193 total_ += size * sizeof(Data);
194 ++nAllocate_;
195 if (total_ > max_) max_ = total_;
196 }
197
198 /*
199 * De-allocate an array that was allocated with Memory::allocate.
200 */
201 template <typename Data>
202 void Memory::deallocate(Data*& ptr, size_t size)
203 {
204 // Preconditions
205 UTIL_CHECK(ptr);
206 UTIL_CHECK(size > 0);
207
208 delete [] ptr;
209 ptr = nullptr;
210 sub<Data>(size);
211 }
212
213 /*
214 * Subtract size of a newly deleted array from current total.
215 */
216 template <typename Data>
217 void Memory::sub(size_t size)
218 {
219 UTIL_CHECK(size > 0);
220 long int change = size * sizeof(Data);
221 UTIL_CHECK(total_ >= change);
222 total_ -= change;
223 ++nDeallocate_;
224 }
225
226 /*
227 * Re-allocate an array (allocate new array, copy, and deallocate old).
228 */
229 template <typename Data>
230 void Memory::reallocate(Data*& ptr, size_t oldSize, size_t newSize)
231 {
232 UTIL_CHECK(newSize > 0);
233 UTIL_CHECK(newSize > oldSize);
234
235 Data* newPtr = nullptr;
236 allocate(newPtr, newSize);
237 if (oldSize > 0) {
238 UTIL_CHECK(ptr);
239 for (size_t i = 0; i < oldSize; ++i) {
240 newPtr[i] = ptr[i];
241 }
242 deallocate(ptr, oldSize);
243 }
244 ptr = newPtr;
245 }
246
247}
248#endif
Provides method to allocate array.
Definition Memory.h:29
static void sub(size_t size)
Subtract size of a newly deleted array in bytes from total.
Definition Memory.h:217
static long int nDeallocate()
Return number of times deallocate() or sub() have been called.
Definition Memory.cpp:40
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
Definition Memory.h:202
static long int nAllocate()
Return number of times reallocate() or add() have been called.
Definition Memory.cpp:34
static long int max()
Return the maximum amount of allocated heap memory thus far.
Definition Memory.cpp:52
static long int total()
Return total amount of memory currently allocated.
Definition Memory.cpp:46
static void reallocate(Data *&ptr, size_t oldSize, size_t newSize)
Reallocate a C++ array.
Definition Memory.h:230
static void initStatic()
Call this just to guarantee initialization of static memory.
Definition Memory.cpp:28
static void clearCounters()
Reset total and max counters to zero.
Definition Memory.cpp:72
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
Definition Memory.h:172
static void add(size_t size)
Add size of a newly allocated array in bytes to total.
Definition Memory.h:191
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
Utility classes for scientific computation.