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 154 of file ThreadSpecificPool.hpp.

Member Typedef Documentation

◆ PoolMapType

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

Definition at line 157 of file ThreadSpecificPool.hpp.

Constructor & Destructor Documentation

◆ MemPool()

Nektar::MemPool::MemPool ( )
inline

Definition at line 160 of file ThreadSpecificPool.hpp.

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

References m_pools.

◆ ~MemPool()

Nektar::MemPool::~MemPool ( )
inline

Definition at line 181 of file ThreadSpecificPool.hpp.

182  {
183  }

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 195 of file ThreadSpecificPool.hpp.

196  {
197  if( bytes <= 4 )
198  {
199  return m_fourBytePool.Allocate();
200  }
201  else if( bytes > m_upperBound )
202  {
203 #ifdef NEKTAR_USE_ALIGNED_MEM
204  return boost::alignment::aligned_alloc(tinysimd::simd<NekDouble>::alignment, bytes);
205 #else
206  return ::operator new(bytes);
207 #endif
208  }
209  else
210  {
211  PoolMapType::iterator iter = m_pools.lower_bound(bytes);
212  ASSERTL1(iter != m_pools.end(), "The memory manager is mishandling a memory request for " +
213  std::to_string(bytes) + " bytes of memory.");
214 
215  return (*iter).second->Allocate();
216  }
217  }
#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.
typename abi< ScalarType >::type simd
Definition: tinysimd.hpp:83

References Nektar::detail::ThreadSpecificPool::Allocate(), ASSERTL1, m_fourBytePool, m_pools, and m_upperBound.

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

◆ 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 223 of file ThreadSpecificPool.hpp.

224  {
225  if( bytes <= 4 )
226  {
228  }
229  else if( bytes > m_upperBound )
230  {
231 #ifdef NEKTAR_USE_ALIGNED_MEM
232  boost::alignment::aligned_free(p);
233 #else
234  ::operator delete(p);
235 #endif
236  }
237  else
238  {
239  PoolMapType::iterator iter = m_pools.lower_bound(bytes);
240  ASSERTL1(iter != m_pools.end(), "The memory manager is mishandling a memory request for " +
241  std::to_string(bytes) + " bytes of memory.");
242 
243  (*iter).second->Deallocate(p);
244  }
245  }
void Deallocate(const void *p)
Deallocate memory claimed by an earlier call to allocate.

References ASSERTL1, Nektar::detail::ThreadSpecificPool::Deallocate(), m_fourBytePool, m_pools, m_upperBound, and CellMLToNektar.cellml_metadata::p.

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

Member Data Documentation

◆ m_fourBytePool

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

Definition at line 248 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), and Deallocate().

◆ m_pools

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

Definition at line 249 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), Deallocate(), and MemPool().

◆ m_upperBound

size_t Nektar::MemPool::m_upperBound
private

Definition at line 250 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), and Deallocate().