PSCF v1.5.0
Util::Memory Class Reference

Provides method to allocate array. More...

#include <Memory.h>

Static Public Member Functions

template<typename Data>
static void allocate (Data *&ptr, size_t size)
 Allocate a C++ array.
template<typename Data>
static void add (size_t size)
 Add size of a newly allocated array in bytes to total.
template<typename Data>
static void deallocate (Data *&ptr, size_t size)
 Deallocate a C++ array.
template<typename Data>
static void sub (size_t size)
 Subtract size of a newly deleted array in bytes from total.
template<typename Data>
static void reallocate (Data *&ptr, size_t oldSize, size_t newSize)
 Reallocate a C++ array.
static long int nAllocate ()
 Return number of times reallocate() or add() have been called.
static long int nDeallocate ()
 Return number of times deallocate() or sub() have been called.
static long int total ()
 Return total amount of memory currently allocated.
static long int max ()
 Return the maximum amount of allocated heap memory thus far.
static void clearCounters ()
 Reset total and max counters to zero.
static void initStatic ()
 Call this just to guarantee initialization of static memory.

Detailed Description

Provides method to allocate array.

The Memory::allocate() method invokes the new operator within a try-catch block, and keeps track of the total memory allocated via this class.

Definition at line 28 of file Memory.h.

Member Function Documentation

◆ allocate()

template<typename Data>
void Util::Memory::allocate ( Data *& ptr,
size_t size )
static

◆ add()

template<typename Data>
void Util::Memory::add ( size_t size)
static

Add size of a newly allocated array in bytes to total.

This function accounts for the size of an array that was allocated externally. It adds size * sizeof(Data) to the current counter and updates max if the resulting total exceeds the previous maximum.

Parameters
sizenumber of elements

Definition at line 191 of file Memory.h.

Referenced by Pscf::FftwDRArray< Data >::allocate(), and allocate().

◆ deallocate()

template<typename Data>
void Util::Memory::deallocate ( Data *& ptr,
size_t size )
static

Deallocate a C++ array.

Uses the C++ delete [] operator to deallocate a Data array of size elements of type Data that was allocated by the Memory::allocate member function. On input, ptr must be the pointer to an array of elements of type Data, and size must be the number of elements. On output, the array is deleted, ptr is set to nullptr, and the total counter is decremented by size * sizeof(Data).

Parameters
ptrreference to pointer (ptr is set to nullptr on output)
sizenumber of elements in existing array

Definition at line 202 of file Memory.h.

References sub(), and UTIL_CHECK.

Referenced by Util::GArray< Data >::append(), Util::GPArray< Data >::append(), Pscf::HostRecvArray< Data, CUT >::deallocate(), Util::DArray< Data >::deallocate(), Util::DMatrix< Data >::deallocate(), Util::DRArray< Data >::deallocate(), Util::GArray< Data >::deallocate(), Util::GPArray< Data >::deallocate(), Util::GStack< Data >::deallocate(), Util::GStack< Data >::push(), reallocate(), Util::GArray< Data >::reserve(), Util::GPArray< Data >::reserve(), Util::GStack< Data >::reserve(), Util::GArray< Data >::resize(), Util::ArraySet< Data >::~ArraySet(), Util::ArrayStack< Data >::~ArrayStack(), Util::DArray< Data >::~DArray(), Util::DMatrix< Data >::~DMatrix(), Util::DPArray< Data >::~DPArray(), Util::DRaggedMatrix< Data >::~DRaggedMatrix(), Util::DRArray< Data >::~DRArray(), Util::DSArray< Data >::~DSArray(), Util::GArray< Data >::~GArray(), Util::GPArray< Data >::~GPArray(), Util::GridArray< Data >::~GridArray(), Util::GStack< Data >::~GStack(), Pscf::HostRecvArray< Data, CUT >::~HostRecvArray(), Util::ListArray< Data >::~ListArray(), Util::MemoryIArchive::~MemoryIArchive(), Util::MemoryOArchive::~MemoryOArchive(), and Util::RingBuffer< Data >::~RingBuffer().

◆ sub()

template<typename Data>
void Util::Memory::sub ( size_t size)
static

Subtract size of a newly deleted array in bytes from total.

This function accounts for the size of an array that was deleted externally. It subtracts size * sizeof(Data) from the current counter, or throws an Exception if the resulting total would be negative.

Parameters
sizenumber of elements

Definition at line 217 of file Memory.h.

References UTIL_CHECK.

Referenced by Pscf::FftwDRArray< Data >::deallocate(), deallocate(), and Pscf::FftwDRArray< Data >::~FftwDRArray().

◆ reallocate()

template<typename Data>
void Util::Memory::reallocate ( Data *& ptr,
size_t oldSize,
size_t newSize )
static

Reallocate a C++ array.

This function calls allocate to allocate a new array of size newSize. If oldSize > 0, it copies all existing elements, and then deallocates the old array. On return, ptr is the address of the new array.

Precondition: On input, newSize > 0 and newSize > oldSize.

Parameters
ptrreference to pointer (input/output)
oldSizenumber of elements in existing array
newSizenumber of elements in new array

Definition at line 230 of file Memory.h.

References allocate(), deallocate(), and UTIL_CHECK.

Referenced by Util::DArray< Data >::reallocate().

◆ nAllocate()

long int Util::Memory::nAllocate ( )
static

Return number of times reallocate() or add() have been called.

Each call to reallocate() also increments nAllocate(), because allocate() is called internally.

Definition at line 34 of file Memory.cpp.

◆ nDeallocate()

long int Util::Memory::nDeallocate ( )
static

Return number of times deallocate() or sub() have been called.

Each call to reallocate() also increments nDeallocate(), because deallocate() is called internally.

Definition at line 40 of file Memory.cpp.

◆ total()

long int Util::Memory::total ( )
static

Return total amount of memory currently allocated.

Definition at line 46 of file Memory.cpp.

◆ max()

long int Util::Memory::max ( )
static

Return the maximum amount of allocated heap memory thus far.

This function returns the temporal maximum of total().

Definition at line 52 of file Memory.cpp.

◆ clearCounters()

void Util::Memory::clearCounters ( )
static

Reset total and max counters to zero.

Definition at line 72 of file Memory.cpp.

◆ initStatic()

void Util::Memory::initStatic ( )
static

Call this just to guarantee initialization of static memory.

Definition at line 28 of file Memory.cpp.

Referenced by Util::initStatic().


The documentation for this class was generated from the following files: