Nektar++
Public Types | Public Member Functions | Private Attributes | List of all members
Nektar::MemPool Class Reference

#include <ThreadSpecificPool.hpp>

Public Types

typedef std::map< size_t, std::shared_ptr< detail::ThreadSpecificPool > > PoolMapType
 

Public Member Functions

 MemPool ()
 
 ~MemPool ()
 
voidAllocate (size_t bytes)
 Allocate a block of memory of size ByteSize. More...
 
void Deallocate (void *p, size_t bytes)
 Deallocate memory claimed by an earlier call to allocate. More...
 

Private Attributes

detail::ThreadSpecificPool m_fourBytePool
 
std::map< size_t, std::shared_ptr< detail::ThreadSpecificPool > > m_pools
 
size_t m_upperBound
 

Detailed Description

Definition at line 147 of file ThreadSpecificPool.hpp.

Member Typedef Documentation

◆ PoolMapType

typedef std::map<size_t, std::shared_ptr<detail::ThreadSpecificPool> > Nektar::MemPool::PoolMapType

Definition at line 150 of file ThreadSpecificPool.hpp.

Constructor & Destructor Documentation

◆ MemPool()

Nektar::MemPool::MemPool ( )
inline

Definition at line 153 of file ThreadSpecificPool.hpp.

153  :
154  m_fourBytePool(4),
155  m_pools(),
156  m_upperBound(1024)
157  {
158  // The m_pools data member stores a collection of thread specific pools of varying size. All memory requests
159  // up to and including the largest pool size will be allocated from a pool (note that this means you may receive
160  // more memory than you asked for). For example, if there is a pool for 8 bytes and the next largest pool is 32
161  // bytes, then a request for 10 bytes will return a 32 byte chunk of memory from the 32 byte pool.
162 
163  typedef PoolMapType::value_type PairType;
164  m_pools.insert(PairType(8, std::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(8))));
165  m_pools.insert(PairType(16, std::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(16))));
166  m_pools.insert(PairType(32, std::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(32))));
167  m_pools.insert(PairType(64, std::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(64))));
168  m_pools.insert(PairType(128, std::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(128))));
169  m_pools.insert(PairType(256, std::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(256))));
170  m_pools.insert(PairType(512, std::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(512))));
171  m_pools.insert(PairType(1024, std::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(1024))));
172  }
detail::ThreadSpecificPool m_fourBytePool
std::map< size_t, std::shared_ptr< detail::ThreadSpecificPool > > m_pools

◆ ~MemPool()

Nektar::MemPool::~MemPool ( )
inline

Definition at line 174 of file ThreadSpecificPool.hpp.

175  {
176  }

Member Function Documentation

◆ Allocate()

void* Nektar::MemPool::Allocate ( size_t  bytes)
inline

Allocate a block of memory of size ByteSize.

Exceptions
std::bad_allocif memory is exhausted.
Parameters
bytesThe number of bytes to allocate.

If the bytes parameter specifies a size that is handled by memory pools then the memory is allocated from the pool. Otherwise the memory is allocated with a call to new.

Important: All memory allocated from this method must be returned to the pool via the Deallocate method. Deleting pointers allocated from the memory pool with the delete operator will result in undefined behavior.

Definition at line 188 of file ThreadSpecificPool.hpp.

References ASSERTL1.

Referenced by Nektar::MemoryManager< DataType >::Deallocate(), and Nektar::MemoryManager< DataType >::RawAllocate().

189  {
190  if( bytes <= 4 )
191  {
192  return m_fourBytePool.Allocate();
193  }
194  else if( bytes > m_upperBound )
195  {
196  return ::operator new(bytes);
197  }
198  else
199  {
200  PoolMapType::iterator iter = m_pools.lower_bound(bytes);
201  ASSERTL1(iter != m_pools.end(), "The memory manager is mishandling a memory request for " +
202  std::to_string(bytes) + " bytes of memory.");
203 
204  return (*iter).second->Allocate();
205  }
206  }
detail::ThreadSpecificPool m_fourBytePool
std::map< size_t, std::shared_ptr< detail::ThreadSpecificPool > > m_pools
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:250
void * Allocate()
Allocate a block of memory of size ByteSize.

◆ Deallocate()

void Nektar::MemPool::Deallocate ( void p,
size_t  bytes 
)
inline

Deallocate memory claimed by an earlier call to allocate.

Attention
It is an error to deallocate memory not allocated from this pool. Doing this will result in undefined behavior.

Definition at line 212 of file ThreadSpecificPool.hpp.

References ASSERTL1, and CellMLToNektar.cellml_metadata::p.

Referenced by Nektar::MemoryManager< DataType >::Deallocate(), and Nektar::MemoryManager< DataType >::RawDeallocate().

213  {
214  if( bytes <= 4 )
215  {
217  }
218  else if( bytes > m_upperBound )
219  {
220  ::operator delete(p);
221  }
222  else
223  {
224  PoolMapType::iterator iter = m_pools.lower_bound(bytes);
225  ASSERTL1(iter != m_pools.end(), "The memory manager is mishandling a memory request for " +
226  std::to_string(bytes) + " bytes of memory.");
227 
228  (*iter).second->Deallocate(p);
229  }
230  }
detail::ThreadSpecificPool m_fourBytePool
std::map< size_t, std::shared_ptr< detail::ThreadSpecificPool > > m_pools
void Deallocate(const void *p)
Deallocate memory claimed by an earlier call to allocate.
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:250

Member Data Documentation

◆ m_fourBytePool

detail::ThreadSpecificPool Nektar::MemPool::m_fourBytePool
private

Definition at line 233 of file ThreadSpecificPool.hpp.

◆ m_pools

std::map<size_t, std::shared_ptr<detail::ThreadSpecificPool> > Nektar::MemPool::m_pools
private

Definition at line 234 of file ThreadSpecificPool.hpp.

◆ m_upperBound

size_t Nektar::MemPool::m_upperBound
private

Definition at line 235 of file ThreadSpecificPool.hpp.