Chombo + EB  3.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
Pool Class Reference

Pool is a class to optimize memory allocation. More...

#include <Pool.H>

Public Member Functions

 Pool (int a_ptrSize, const char *a_name="unnamed", int a_poolSize=100, int a_alignment=sizeof(int), bool a_allowUnalignedAlloc=false)
 
 ~Pool ()
 
void * getPtr ()
 request a section of memory of ptrSize_ contiguous bytes. More...
 
void returnPtr (void *a_ptr)
 return memory previous acquired with the getPtr() function. More...
 
long memUsage () const
 report how much memory this Pool is currently using. More...
 
void clear ()
 

Public Attributes

char m_name_ [64]
 

Static Public Attributes

static PoolListm_poolList_
 

Private Member Functions

void * getMoreMemory ()
 
void * getMore ()
 
 Pool (const Pool &a_rhs)
 Not implemented. Compiler will not let you copy a Pool. More...
 
const Pooloperator= (const Pool &a_rhs)
 Not implemented. Compiler will not let you copy a Pool. More...
 

Static Private Member Functions

static void clearAllPools ()
 

Private Attributes

std::vector< char * > m_pool_
 
int m_ptrSize_
 
int m_poolSize_
 
int m_alignment_
 
bool m_allowUnalignedAlloc
 
void * m_next_
 

Friends

void dumpmemoryatexit ()
 

Detailed Description

Pool is a class to optimize memory allocation.

Pool is a class to optimize memory allocation. It is specialized to allocate fixed size chunks of memory specified by ptrSize in the constructor. Its operation is analogous to malloc, not new. It does not initialize the memory in any way. The constructor can optionally specify an initial pool size, and memory alignment. The pool size will grow as needed by calling ::new. The pool size never shrinks. Memory will be reclaimed at ~Pool(). The units of poolSize are number-of-ptrSize-chunks. The units of alignment are bytes.

Pool can only be used for objects of fixed size. Typically used in situations where a class or struct is being constantly constructed and deleted. Objects returned by value from functions, elements in a database, etc. Pool's tend to be allocated statically.

(from Copier.cpp)

// static data member s_motionIemPool getting constructed
Pool Copier::s_motionItemPool(sizeof(MotionItem), "Copier::MotionItem");
// note the use of a 'placement new'
MotionItem* item = new (s_motionItemPool.getPtr()) MotionItem(fromdi, todi, box);
// if your object does requires it's destructor to be called.
item->~MotionItem();
// then return the memory chunk to your pool
s_motionItemPool.returnPtr(item)

Technical note In the event of multi-threading Chombo, we will have to make pool access serialized (locks, single thread access, etc) or implement a fast lock-free version, or go back to new/delete and let the OS be clever again about memory management.

Constructor & Destructor Documentation

◆ Pool() [1/2]

Pool::Pool ( int  a_ptrSize,
const char *  a_name = "unnamed",
int  a_poolSize = 100,
int  a_alignment = sizeof(int),
bool  a_allowUnalignedAlloc = false 
)
Parameters
a_ptrSizeSize of fixed memory allocations needed
a_poolSizeSize of allocations (a_ptrSize*a_poolSize) made of the operating system. This controls the granularity of the Pool allocations. Smaller values result in less wasted memory, at the cost of more calls to the operating system for heap memory.
a_nameoptional name for this Pool. Used in reporting memory by the memory tracking system in Chombo.

◆ ~Pool()

Pool::~Pool ( )

◆ Pool() [2/2]

Pool::Pool ( const Pool a_rhs)
private

Not implemented. Compiler will not let you copy a Pool.

Member Function Documentation

◆ getPtr()

void * Pool::getPtr ( )
inline

request a section of memory of ptrSize_ contiguous bytes.

getPtr and returnPtr must be as fast as possible!

References dumpmemoryatexit, getMoreMemory(), m_next_, and m_ptrSize_.

Referenced by List< T >::addAfter(), and List< T >::addBefore().

◆ returnPtr()

void Pool::returnPtr ( void *  a_ptr)

return memory previous acquired with the getPtr() function.

Referenced by List< T >::clear(), and List< T >::remove().

◆ memUsage()

long Pool::memUsage ( ) const

report how much memory this Pool is currently using.

memUsage for a Pool only grows until Pool destruction. The Pool object has no knowledge of what pieces of memory it has parcelled out to a user, so it keeps it all available. The user is responsible for not leaking Pool memory.

◆ clear()

void Pool::clear ( )

undocumented function, call this at own risk. You must be absolutely positive that you have returned all the ptr's you've asked for, or you can have major seg faults.

◆ getMoreMemory()

void* Pool::getMoreMemory ( )
private

Referenced by getPtr().

◆ getMore()

void* Pool::getMore ( )
private

◆ operator=()

const Pool& Pool::operator= ( const Pool a_rhs)
private

Not implemented. Compiler will not let you copy a Pool.

◆ clearAllPools()

static void Pool::clearAllPools ( )
staticprivate

Friends And Related Function Documentation

◆ dumpmemoryatexit

void dumpmemoryatexit ( )
friend

Referenced by getPtr().

Member Data Documentation

◆ m_poolList_

PoolList* Pool::m_poolList_
static

◆ m_name_

char Pool::m_name_[64]

◆ m_pool_

std::vector<char*> Pool::m_pool_
private

◆ m_ptrSize_

int Pool::m_ptrSize_
private

Referenced by getPtr().

◆ m_poolSize_

int Pool::m_poolSize_
private

◆ m_alignment_

int Pool::m_alignment_
private

◆ m_allowUnalignedAlloc

bool Pool::m_allowUnalignedAlloc
private

T - Allows unaligned allocations (> sizof(double) if it is not known how to allocate aligned memory F - Otherwise abort

◆ m_next_

void* Pool::m_next_
private

Referenced by getPtr().


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