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

Member Typedef Documentation

◆ PoolMapType

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

Definition at line 154 of file ThreadSpecificPool.hpp.

Constructor & Destructor Documentation

◆ MemPool()

Nektar::MemPool::MemPool ( )
inline

Definition at line 157 of file ThreadSpecificPool.hpp.

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

191 {
192 }

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

206 {
207 if (bytes <= 4)
208 {
209 return m_fourBytePool.Allocate();
210 }
211 else if (bytes > m_upperBound)
212 {
213#ifdef NEKTAR_USE_ALIGNED_MEM
214 return boost::alignment::aligned_alloc(
216#else
217 return ::operator new(bytes);
218#endif
219 }
220 else
221 {
222 PoolMapType::iterator iter = m_pools.lower_bound(bytes);
223 ASSERTL1(iter != m_pools.end(),
224 "The memory manager is mishandling a memory request for " +
225 std::to_string(bytes) + " bytes of memory.");
226
227 return (*iter).second->Allocate();
228 }
229 }
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:242
void * Allocate()
Allocate a block of memory of size ByteSize.
typename abi< ScalarType, width >::type simd
Definition: tinysimd.hpp:80

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

236 {
237 if (bytes <= 4)
238 {
240 }
241 else if (bytes > m_upperBound)
242 {
243#ifdef NEKTAR_USE_ALIGNED_MEM
244 boost::alignment::aligned_free(p);
245#else
246 ::operator delete(p);
247#endif
248 }
249 else
250 {
251 PoolMapType::iterator iter = m_pools.lower_bound(bytes);
252 ASSERTL1(iter != m_pools.end(),
253 "The memory manager is mishandling a memory request for " +
254 std::to_string(bytes) + " bytes of memory.");
255
256 (*iter).second->Deallocate(p);
257 }
258 }
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 261 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 262 of file ThreadSpecificPool.hpp.

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

◆ m_upperBound

size_t Nektar::MemPool::m_upperBound
private

Definition at line 263 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), and Deallocate().