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.
// 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.
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)|
|void *||getPtr ()|
|request a section of memory of ptrSize_ contiguous bytes. |
|void||returnPtr (void *a_ptr)|
|return memory previous acquired with the getPtr() function. |
|long||memUsage () const|
|report how much memory this Pool is currently using. |
Static Public Attributes
|static PoolList *||m_poolList_|
Private Member Functions
|void *||getMoreMemory ()|
|void *||getMore ()|
|Pool (const Pool &a_rhs)|
|const Pool &||operator= (const Pool &a_rhs)|
|Not implemented. Compiler will not let you copy a Pool. |
Static Private Member Functions
|static void||clearAllPools ()|
|Vector< char * >||m_pool_|
|const char *|| a_name =
|int|| a_poolSize =
|int|| a_alignment =
|bool|| a_allowUnalignedAlloc = |
|a_ptrSize||Size of fixed memory allocations needed|
|a_poolSize||Size 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_name||optional name for this Pool. Used in reporting memory by the memory tracking system in Chombo.|
|void Pool::returnPtr||(||void *||a_ptr||)|
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.
Not implemented. Compiler will not let you copy a Pool.
|static void Pool::clearAllPools||(||)||
T - Allows unaligned allocations (> sizof(double) if it is not known how to allocate aligned memory F - Otherwise abort