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.
void Deallocate (void *p, size_t bytes)
 Deallocate memory claimed by an earlier call to allocate.

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.

:
{
// The m_pools data member stores a collection of thread specific pools of varying size. All memory requests
// up to and including the largest pool size will be allocated from a pool (note that this means you may receive
// more memory than you asked for). For example, if there is a pool for 8 bytes and the next largest pool is 32
// bytes, then a request for 10 bytes will return a 32 byte chunk of memory from the 32 byte pool.
typedef PoolMapType::value_type PairType;
m_pools.insert(PairType(8, boost::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(8))));
m_pools.insert(PairType(16, boost::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(16))));
m_pools.insert(PairType(32, boost::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(32))));
m_pools.insert(PairType(64, boost::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(64))));
m_pools.insert(PairType(128, boost::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(128))));
m_pools.insert(PairType(256, boost::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(256))));
m_pools.insert(PairType(512, boost::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(512))));
m_pools.insert(PairType(1024, boost::shared_ptr<detail::ThreadSpecificPool>(new detail::ThreadSpecificPool(1024))));
}
Nektar::MemPool::~MemPool ( )
inline

Definition at line 169 of file ThreadSpecificPool.hpp.

{
}

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().

{
if( bytes <= 4 )
{
}
else if( bytes > m_upperBound )
{
return ::operator new(bytes);
}
else
{
PoolMapType::iterator iter = m_pools.lower_bound(bytes);
ASSERTL1(iter != m_pools.end(), "The memory manager is mishandling a memory request for " +
boost::lexical_cast<std::string>(bytes) + " bytes of memory.");
return (*iter).second->Allocate();
}
}
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().

{
if( bytes <= 4 )
{
}
else if( bytes > m_upperBound )
{
::operator delete(p);
}
else
{
PoolMapType::iterator iter = m_pools.lower_bound(bytes);
ASSERTL1(iter != m_pools.end(), "The memory manager is mishandling a memory request for " +
boost::lexical_cast<std::string>(bytes) + " bytes of memory.");
(*iter).second->Deallocate(p);
}
}

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().