Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Private Attributes | List of all members
Nektar::MemPool Class Reference

#include <ThreadSpecificPool.hpp>

Collaboration diagram for Nektar::MemPool:
Collaboration graph
[legend]

Public Types

typedef std::map< size_t,
boost::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,
boost::shared_ptr
< detail::ThreadSpecificPool > > 
m_pools
 
size_t m_upperBound
 

Detailed Description

Definition at line 142 of file ThreadSpecificPool.hpp.

Member Typedef Documentation

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

Definition at line 145 of file ThreadSpecificPool.hpp.

Constructor & Destructor Documentation

Nektar::MemPool::MemPool ( )
inline

Definition at line 148 of file ThreadSpecificPool.hpp.

References m_pools.

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

Definition at line 169 of file ThreadSpecificPool.hpp.

170  {
171  }

Member Function Documentation

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

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

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

184  {
185  if( bytes <= 4 )
186  {
187  return m_fourBytePool.Allocate();
188  }
189  else if( bytes > m_upperBound )
190  {
191  return ::operator new(bytes);
192  }
193  else
194  {
195  PoolMapType::iterator iter = m_pools.lower_bound(bytes);
196  ASSERTL1(iter != m_pools.end(), "The memory manager is mishandling a memory request for " +
197  boost::lexical_cast<std::string>(bytes) + " bytes of memory.");
198 
199  return (*iter).second->Allocate();
200  }
201  }
detail::ThreadSpecificPool m_fourBytePool
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs typedef NekMatrix< LhsDataType, StandardMatrixTag >::iterator iterator
std::map< size_t, boost::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:218
void * Allocate()
Allocate a block of memory of size ByteSize.
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 207 of file ThreadSpecificPool.hpp.

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

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

208  {
209  if( bytes <= 4 )
210  {
212  }
213  else if( bytes > m_upperBound )
214  {
215  ::operator delete(p);
216  }
217  else
218  {
219  PoolMapType::iterator iter = m_pools.lower_bound(bytes);
220  ASSERTL1(iter != m_pools.end(), "The memory manager is mishandling a memory request for " +
221  boost::lexical_cast<std::string>(bytes) + " bytes of memory.");
222 
223  (*iter).second->Deallocate(p);
224  }
225  }
detail::ThreadSpecificPool m_fourBytePool
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs typedef NekMatrix< LhsDataType, StandardMatrixTag >::iterator iterator
std::map< size_t, boost::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:218

Member Data Documentation

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

Definition at line 228 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), and Deallocate().

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

Definition at line 229 of file ThreadSpecificPool.hpp.

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

size_t Nektar::MemPool::m_upperBound
private

Definition at line 230 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), and Deallocate().