Nektar++
Loading...
Searching...
No Matches
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.
 
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, std::shared_ptr< detail::ThreadSpecificPool > > m_pools
 
size_t m_upperBound
 

Detailed Description

Definition at line 149 of file ThreadSpecificPool.hpp.

Member Typedef Documentation

◆ PoolMapType

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

Definition at line 153 of file ThreadSpecificPool.hpp.

Constructor & Destructor Documentation

◆ MemPool()

Nektar::MemPool::MemPool ( )
inline

Definition at line 156 of file ThreadSpecificPool.hpp.

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

190 {
191 }

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

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

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

234 {
235 if (bytes <= 4)
236 {
238 }
239 else if (bytes > m_upperBound)
240 {
241#ifdef NEKTAR_USE_ALIGNED_MEM
242 free(p);
243#else
244 ::operator delete(p);
245#endif
246 }
247 else
248 {
249 PoolMapType::iterator iter = m_pools.lower_bound(bytes);
250 ASSERTL1(iter != m_pools.end(),
251 "The memory manager is mishandling a memory request for " +
252 std::to_string(bytes) + " bytes of memory.");
253
254 (*iter).second->Deallocate(p);
255 }
256 }
void Deallocate(const void *p)
Deallocate memory claimed by an earlier call to allocate.
std::vector< double > p(NPUPPER)

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

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

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

◆ m_upperBound

size_t Nektar::MemPool::m_upperBound
private

Definition at line 261 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), and Deallocate().