Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
FIX::shared_array< T > Class Template Reference

Shared array with atomic reference count. More...

#include <SharedArray.h>

Inheritance diagram for FIX::shared_array< T >:
Inheritance graph
[legend]

Public Member Functions

 shared_array ()
 
 shared_array (const shared_array &rhs)
 
 ~shared_array ()
 
shared_arrayoperator= (const shared_array &rhs)
 
std::size_t size () const
 
bool empty () const
 
 operator T* () const
 

Static Public Member Functions

static shared_array create (const std::size_t nSize)
 

Private Member Functions

 shared_array (T *buff, std::size_t nSize, void *pCtr)
 
atomic_countget_counter () const
 
void increment_reference_count () const
 
long decrement_reference_count () const
 
void attach () const
 
void release ()
 

Private Attributes

std::size_t m_size
 
T * m_buffer
 
void * m_pCtr
 

Detailed Description

template<typename T>
class FIX::shared_array< T >

Shared array with atomic reference count.

Definition at line 157 of file SharedArray.h.

Constructor & Destructor Documentation

◆ shared_array() [1/3]

template<typename T >
FIX::shared_array< T >::shared_array ( )
inline

Definition at line 160 of file SharedArray.h.

161  : m_size(0)
162  , m_buffer(0)
163  , m_pCtr(0)
164  {}
std::size_t m_size
Definition: SharedArray.h:277

Referenced by FIX::shared_array< T >::create().

◆ shared_array() [2/3]

template<typename T >
FIX::shared_array< T >::shared_array ( const shared_array< T > &  rhs)
inline

Definition at line 166 of file SharedArray.h.

167  : m_size(rhs.m_size)
168  , m_buffer(rhs.m_buffer)
169  {
170  rhs.attach();
171  }

◆ ~shared_array()

template<typename T >
FIX::shared_array< T >::~shared_array ( )
inline

Definition at line 173 of file SharedArray.h.

174  { release(); }

References FIX::shared_array< T >::release().

◆ shared_array() [3/3]

template<typename T >
FIX::shared_array< T >::shared_array ( T *  buff,
std::size_t  nSize,
void *  pCtr 
)
inlineprivate

Definition at line 225 of file SharedArray.h.

226  : m_size(nSize)
227  , m_buffer(buff)
228  , m_pCtr(pCtr)
229  {
230 
231  }

Member Function Documentation

◆ attach()

template<typename T >
void FIX::shared_array< T >::attach ( ) const
inlineprivate

Definition at line 250 of file SharedArray.h.

251  {
252  if( !empty() )
254  }
void increment_reference_count() const
Definition: SharedArray.h:238
bool empty() const
Definition: SharedArray.h:194

References FIX::shared_array< T >::empty(), and FIX::shared_array< T >::increment_reference_count().

Referenced by FIX::shared_array< T >::operator=().

◆ create()

template<typename T >
static shared_array FIX::shared_array< T >::create ( const std::size_t  nSize)
inlinestatic

Definition at line 201 of file SharedArray.h.

202  {
203  if(nSize <= 0)
204  return shared_array();
205 
206  //verify the needed buffer size to allocate counter object and nSize elements
207  const std::size_t sizeToAllocate = (nSize * sizeof(T)) + sizeof(atomic_count) + 15;
208 
209  //allocate and zero-fill the buffer
210  void * buf = std::malloc(sizeToAllocate);
211  memset(buf, 0, sizeToAllocate);
212 
213  // create the counter object at the end of the storage
214  // with initial reference count set to 1
215  /* round up to multiple of alignment : add (alignment - 1) and then round down by masking */
216  void *ptr = (void *) (((uintptr_t)(buf) + nSize * sizeof(T) + 15) & ~ (uintptr_t)0x0F);
217  new (ptr) atomic_count( 1 );
218 
219  T* storage = reinterpret_cast<T*>(buf);
220  return shared_array(storage, nSize, ptr);
221  }
_W64 unsigned int uintptr_t
Definition: stdint_msvc.h:120

References FIX::shared_array< T >::shared_array().

Referenced by FIX::message_order::message_order(), and FIX::message_order::setOrder().

◆ decrement_reference_count()

template<typename T >
long FIX::shared_array< T >::decrement_reference_count ( ) const
inlineprivate

Definition at line 244 of file SharedArray.h.

245  {
246  atomic_count* counter = get_counter();
247  return --(*counter);
248  }
atomic_count * get_counter() const
Definition: SharedArray.h:233

References FIX::shared_array< T >::get_counter().

Referenced by FIX::shared_array< T >::release().

◆ empty()

template<typename T >
bool FIX::shared_array< T >::empty ( ) const
inline

◆ get_counter()

template<typename T >
atomic_count* FIX::shared_array< T >::get_counter ( ) const
inlineprivate

Definition at line 233 of file SharedArray.h.

234  {
235  return reinterpret_cast<atomic_count*>( m_pCtr );
236  }

References FIX::shared_array< T >::m_pCtr.

Referenced by FIX::shared_array< T >::decrement_reference_count(), FIX::shared_array< T >::increment_reference_count(), and FIX::shared_array< T >::release().

◆ increment_reference_count()

template<typename T >
void FIX::shared_array< T >::increment_reference_count ( ) const
inlineprivate

Definition at line 238 of file SharedArray.h.

239  {
240  atomic_count* counter = get_counter();
241  ++(*counter);
242  }

References FIX::shared_array< T >::get_counter().

Referenced by FIX::shared_array< T >::attach().

◆ operator T*()

template<typename T >
FIX::shared_array< T >::operator T* ( ) const
inline

Definition at line 197 of file SharedArray.h.

198  { return m_buffer; }

◆ operator=()

template<typename T >
shared_array& FIX::shared_array< T >::operator= ( const shared_array< T > &  rhs)
inline

Definition at line 176 of file SharedArray.h.

177  {
178  if( &rhs == this )
179  return *this;
180 
181  rhs.attach();
182  release();
183 
184  m_size = rhs.m_size;
185  m_buffer = rhs.m_buffer;
186  m_pCtr = rhs.m_pCtr;
187 
188  return *this;
189  }

References FIX::shared_array< T >::attach(), FIX::shared_array< T >::m_buffer, FIX::shared_array< T >::m_pCtr, FIX::shared_array< T >::m_size, and FIX::shared_array< T >::release().

◆ release()

template<typename T >
void FIX::shared_array< T >::release ( )
inlineprivate

Definition at line 256 of file SharedArray.h.

257  {
258  if( empty() )
259  return;
260 
261  //free object if reference count has decreased to zero
262  if( decrement_reference_count() == 0)
263  {
264  T * tmpBuff = m_buffer;
265  atomic_count* tmpCounter = get_counter();
266 
267  m_buffer = 0;
268  m_size = 0;
269 
270  //explicitly call destructor for the counter object
271  tmpCounter->~atomic_count();
272 
273  std::free(tmpBuff);
274  }
275  }
long decrement_reference_count() const
Definition: SharedArray.h:244

References FIX::shared_array< T >::decrement_reference_count(), FIX::shared_array< T >::empty(), FIX::shared_array< T >::get_counter(), FIX::shared_array< T >::m_buffer, and FIX::shared_array< T >::m_size.

Referenced by FIX::shared_array< T >::operator=(), and FIX::shared_array< T >::~shared_array().

◆ size()

template<typename T >
std::size_t FIX::shared_array< T >::size ( ) const
inline

Definition at line 191 of file SharedArray.h.

192  { return m_size; }

References FIX::shared_array< T >::m_size.

Member Data Documentation

◆ m_buffer

template<typename T >
T* FIX::shared_array< T >::m_buffer
private

◆ m_pCtr

template<typename T >
void* FIX::shared_array< T >::m_pCtr
private

◆ m_size

template<typename T >
std::size_t FIX::shared_array< T >::m_size
private

The documentation for this class was generated from the following file:

Generated on Wed Nov 24 2021 09:55:53 for QuickFIX by doxygen 1.9.1 written by Dimitri van Heesch, © 1997-2001