PSCF v1.4.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, The Regents of the University of Minnesota
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
41 template <typename Data>
42 static void allocate(Data*& ptr, size_t size);
43
56 template <typename Data>
57 static void deallocate(Data*& ptr, size_t size);
58
73 template <typename Data>
74 static void reallocate(Data*& ptr, size_t oldSize, size_t newSize);
75
82 static long int nAllocate();
83
90 static long int nDeallocate();
91
95 static long int total();
96
102 static long int max();
103
104 #ifdef UTIL_MPI
108 static long int max(MPI::Intracomm& communicator);
109 #endif
110
114 static void initStatic();
115
116 private:
117
119 static long int total_;
120
122 static long int max_;
123
125 static long int nAllocate_;
126
128 static long int nDeallocate_;
129
130 };
131
132 /*
133 * Allocate a C array.
134 */
135 template <typename Data>
136 void Memory::allocate(Data*& ptr, size_t size)
137 {
138 if (ptr) {
139 UTIL_THROW("Attempt to allocate to non-null pointer");
140 }
141 try {
142 ptr = new Data[size];
143 UTIL_CHECK(ptr);
144 total_ += size * sizeof(Data);
145 ++nAllocate_;
146 if (total_ > max_) max_ = total_;
147 } catch (std::bad_alloc&) {
148 std::cout << "Allocation error in Util::Memory" << std::endl;
149 throw;
150 }
151 }
152
153 /*
154 * De-allocate a C array that was allocated with Memory::allocate.
155 */
156 template <typename Data>
157 void Memory::deallocate(Data*& ptr, size_t size)
158 {
159 // Preconditions
160 UTIL_CHECK(ptr);
161 UTIL_CHECK(size > 0);
162
163 delete [] ptr;
164 ptr = nullptr;
165 long int change = size * sizeof(Data);
166 UTIL_CHECK(total_ >= change);
167 total_ -= change;
168 ++nDeallocate_;
169 }
170
171 /*
172 * Re-allocate a C array (allocate new memory and copy).
173 */
174 template <typename Data>
175 void Memory::reallocate(Data*& ptr, size_t oldSize, size_t newSize)
176 {
177 UTIL_CHECK(newSize > 0);
178 UTIL_CHECK(newSize > oldSize);
179
180 Data* newPtr = nullptr;
181 allocate(newPtr, newSize);
182 if (oldSize > 0) {
183 UTIL_CHECK(ptr);
184 for (size_t i = 0; i < oldSize; ++i) {
185 newPtr[i] = ptr[i];
186 }
187 deallocate(ptr, oldSize);
188 }
189 ptr = newPtr;
190 }
191
192}
193#endif
Provides method to allocate array.
Definition Memory.h:29
static long int nDeallocate()
Return number of times deallocate() was called.
Definition Memory.cpp:40
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
Definition Memory.h:157
static long int nAllocate()
Return number of times allocate() was 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:175
static void initStatic()
Call this just to guarantee initialization of static memory.
Definition Memory.cpp:28
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
Definition Memory.h:136
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.