Nektar++
Public Member Functions | Private Attributes | List of all members
Nektar::detail::ThreadSpecificPool Class Reference

#include <ThreadSpecificPool.hpp>

Public Member Functions

 ThreadSpecificPool (size_t ByteSize)
 
 ~ThreadSpecificPool ()
 
voidAllocate ()
 Allocate a block of memory of size ByteSize. More...
 
void Deallocate (const void *p)
 Deallocate memory claimed by an earlier call to allocate. More...
 

Private Attributes

boost::pool * m_pool
 
size_t m_blockSize
 

Detailed Description

Definition at line 85 of file ThreadSpecificPool.hpp.

Constructor & Destructor Documentation

◆ ThreadSpecificPool()

Nektar::detail::ThreadSpecificPool::ThreadSpecificPool ( size_t  ByteSize)
inline

Definition at line 88 of file ThreadSpecificPool.hpp.

88 : m_pool(), m_blockSize(ByteSize)
89 {
90 // We can't do the new in the constructor list because the
91 // thread specific pointer doesn't have a supporting
92 // constructor.
93 m_pool = new boost::pool<>(m_blockSize);
94 }

References m_blockSize, and m_pool.

◆ ~ThreadSpecificPool()

Nektar::detail::ThreadSpecificPool::~ThreadSpecificPool ( )
inline

Definition at line 96 of file ThreadSpecificPool.hpp.

97 {
98 // Need to call delete manually, otherwise memory is leaking.
99 delete m_pool;
100 }

References m_pool.

Member Function Documentation

◆ Allocate()

void * Nektar::detail::ThreadSpecificPool::Allocate ( )
inline

Allocate a block of memory of size ByteSize.

Exceptions
std::bad_allocif memory is exhausted.

Definition at line 104 of file ThreadSpecificPool.hpp.

105 {
106#ifdef NEKTAR_USE_THREAD_SAFETY
107 std::scoped_lock l(m_mutex);
108#endif
109 void *result = m_pool->malloc();
110
111#if defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
112 memset(result, 0, m_blockSize);
113#endif // defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
114
115 return result;
116 }

References m_blockSize, and m_pool.

Referenced by Nektar::MemPool::Allocate().

◆ Deallocate()

void Nektar::detail::ThreadSpecificPool::Deallocate ( const void p)
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 122 of file ThreadSpecificPool.hpp.

123 {
124#ifdef NEKTAR_USE_THREAD_SAFETY
125 std::scoped_lock l(m_mutex);
126#endif
127#if defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
128 // The idea here is to fill the returned memory with some known
129 // pattern, then detect that pattern on the allocate. If the
130 // pattern is no longer there then some memory corruption has
131 // occurred. However, I'm not sure how to distinguish between first
132 // time allocations and repeat allocations.
133
134 // memset(p, '+', m_pool->get_requested_size());
135#endif // defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
136
137 m_pool->free(const_cast<void *>(p));
138 }

References m_pool, and CellMLToNektar.cellml_metadata::p.

Referenced by Nektar::MemPool::Deallocate().

Member Data Documentation

◆ m_blockSize

size_t Nektar::detail::ThreadSpecificPool::m_blockSize
private

Definition at line 143 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), and ThreadSpecificPool().

◆ m_pool

boost::pool* Nektar::detail::ThreadSpecificPool::m_pool
private