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

Constructor & Destructor Documentation

◆ ThreadSpecificPool()

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

Definition at line 81 of file ThreadSpecificPool.hpp.

References m_blockSize, and m_pool.

81  :
82  m_pool(),
83  m_blockSize(ByteSize)
84  {
85  // We can't do the new in the constructor list because the
86  // thread specific pointer doesn't have a supporting
87  // constructor.
88  m_pool = new boost::pool<>(m_blockSize);
89  }

◆ ~ThreadSpecificPool()

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

Definition at line 91 of file ThreadSpecificPool.hpp.

92  {
93  // The documentation isn't particularly clear if delete needs to be called manually
94  // or if the thread specific pointer will call delete for me. Looking through the
95  // boost code doesn't make it any clearer.
96  }

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

References m_blockSize, and m_pool.

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

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

References m_pool.

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

Member Data Documentation

◆ m_blockSize

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

Definition at line 140 of file ThreadSpecificPool.hpp.

Referenced by Allocate(), and ThreadSpecificPool().

◆ m_pool

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

Definition at line 139 of file ThreadSpecificPool.hpp.

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