PSCF v1.1
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
52 template <typename Data>
53 static void deallocate(Data*& ptr, size_t size);
54
69 template <typename Data>
70 static void reallocate(Data*& ptr, size_t oldSize, size_t newSize);
71
78 static int nAllocate();
79
86 static int nDeallocate();
87
91 static int total();
92
98 static int max();
99
100 #ifdef UTIL_MPI
104 static int max(MPI::Intracomm& communicator);
105 #endif
106
110 static void initStatic();
111
112 private:
113
115 static int total_;
116
118 static int max_;
119
121 static int nAllocate_;
122
124 static int nDeallocate_;
125
126 };
127
128 /*
129 * Allocate a C array.
130 */
131 template <typename Data>
132 void Memory::allocate(Data*& ptr, size_t size)
133 {
134 if (ptr) {
135 UTIL_THROW("Attempt to allocate to non-null pointer");
136 }
137 try {
138 ptr = new Data[size];
139 total_ += (size*sizeof(Data));
140 ++nAllocate_;
141 if (total_ > max_) max_ = total_;
142 } catch (std::bad_alloc&) {
143 std::cout << "Allocation error" << std::endl;
144 throw;
145 }
146 }
147
148 /*
149 * De-allocate a C array.
150 */
151 template <typename Data>
152 void Memory::deallocate(Data*& ptr, size_t size)
153 {
154 // Preconditions
155 UTIL_CHECK(ptr);
156 UTIL_CHECK(size > 0);
157
158 delete [] ptr;
159 ptr = 0;
160 total_ -= size*sizeof(Data);
161 ++nDeallocate_;
162 }
163
164 /*
165 * Re-allocate a C array (allocate and copy).
166 */
167 template <typename Data>
168 void Memory::reallocate(Data*& ptr, size_t oldSize, size_t newSize)
169 {
170 UTIL_CHECK(newSize > 0);
171 UTIL_CHECK(newSize > oldSize);
172
173 Data* newPtr = 0;
174 allocate(newPtr, newSize);
175 if (oldSize > 0) {
176 UTIL_CHECK(ptr);
177 for (size_t i = 0; i < oldSize; ++i) {
178 newPtr[i] = ptr[i];
179 }
180 Data* oldPtr = ptr;
181 deallocate(oldPtr, oldSize);
182 }
183 ptr = newPtr;
184 }
185
186}
187#endif
Provides method to allocate array.
Definition: Memory.h:29
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
Definition: Memory.h:152
static int max()
Return the maximum amount of allocated heap memory thus far.
Definition: Memory.cpp:52
static int nAllocate()
Return number of times allocate() was called.
Definition: Memory.cpp:34
static void reallocate(Data *&ptr, size_t oldSize, size_t newSize)
Reallocate a C++ array.
Definition: Memory.h:168
static void initStatic()
Call this just to guarantee initialization of static memory.
Definition: Memory.cpp:28
static int total()
Return total amount of memory currently allocated.
Definition: Memory.cpp:46
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
Definition: Memory.h:132
static int nDeallocate()
Return number of times deallocate() was called.
Definition: Memory.cpp:40
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:51
Utility classes for scientific computation.
Definition: accumulators.mod:1