Nektar++
Loading...
Searching...
No Matches
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.
 
void Deallocate (const void *p)
 Deallocate memory claimed by an earlier call to allocate.
 

Private Attributes

boost::pool * m_pool
 
size_t m_blockSize
 

Detailed Description

Definition at line 84 of file ThreadSpecificPool.hpp.

Constructor & Destructor Documentation

◆ ThreadSpecificPool()

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

Definition at line 87 of file ThreadSpecificPool.hpp.

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

References m_blockSize, and m_pool.

◆ ~ThreadSpecificPool()

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

Definition at line 95 of file ThreadSpecificPool.hpp.

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

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

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

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

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

References m_pool.

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

Member Data Documentation

◆ m_blockSize

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

Definition at line 142 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), and ThreadSpecificPool().

◆ m_pool

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