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]
Collaboration diagram for FIX::shared_array< T >:
Collaboration 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)
 
atomic_countget_counter () const
 
void increment_reference_count () const
 
long decrement_reference_count ()
 
void attach () const
 
void release ()
 

Private Attributes

std::size_t m_size
 
T * m_buffer
 

Detailed Description

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

Shared array with atomic reference count.

Definition at line 31 of file SharedArray.h.

Constructor & Destructor Documentation

◆ shared_array() [1/3]

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

Definition at line 34 of file SharedArray.h.

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

35  : m_size(0)
36  , m_buffer(0)
37  {}
std::size_t m_size
Definition: SharedArray.h:145

◆ shared_array() [2/3]

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

Definition at line 39 of file SharedArray.h.

40  : m_size(rhs.m_size)
41  , m_buffer(rhs.m_buffer)
42  {
43  rhs.attach();
44  }
std::size_t m_size
Definition: SharedArray.h:145

◆ ~shared_array()

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

Definition at line 46 of file SharedArray.h.

47  { release(); }

◆ shared_array() [3/3]

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

Definition at line 94 of file SharedArray.h.

95  : m_size(nSize)
96  , m_buffer(buff)
97  {
98 
99  }
std::size_t m_size
Definition: SharedArray.h:145

Member Function Documentation

◆ attach()

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

Definition at line 118 of file SharedArray.h.

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

119  {
120  if( !empty() )
122  }
void increment_reference_count() const
Definition: SharedArray.h:106
bool empty() const
Definition: SharedArray.h:66

◆ create()

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

Definition at line 73 of file SharedArray.h.

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

74  {
75  if(nSize <= 0)
76  return shared_array();
77 
78  //verify the needed buffer size to allocate counter object and nSize elements
79  const std::size_t sizeToAllocate = nSize + ( sizeof(atomic_count) / sizeof(T) + 1 );
80 
81  //allocate and zero-fill the buffer
82  T* storage = new T[ sizeToAllocate ];
83  memset(storage, 0, sizeToAllocate * sizeof(T));
84 
85  // create the counter object at the end of the storage
86  // with initial reference count set to 1
87  new (&storage[nSize]) atomic_count( 1 );
88 
89  return shared_array(storage, nSize);
90  }

◆ decrement_reference_count()

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

Definition at line 112 of file SharedArray.h.

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

113  {
114  atomic_count* counter = get_counter();
115  return --(*counter);
116  }
atomic_count * get_counter() const
Definition: SharedArray.h:101

◆ empty()

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

Definition at line 66 of file SharedArray.h.

Referenced by FIX::shared_array< int >::attach(), and FIX::shared_array< int >::release().

67  { return m_buffer == 0; }

◆ get_counter()

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

Definition at line 101 of file SharedArray.h.

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

102  {
103  return reinterpret_cast<atomic_count*>( &m_buffer[ size() ] );
104  }
std::size_t size() const
Definition: SharedArray.h:63

◆ increment_reference_count()

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

Definition at line 106 of file SharedArray.h.

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

107  {
108  atomic_count* counter = get_counter();
109  ++(*counter);
110  }
atomic_count * get_counter() const
Definition: SharedArray.h:101

◆ operator T*()

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

Definition at line 69 of file SharedArray.h.

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

70  { return m_buffer; }

◆ operator=()

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

Definition at line 49 of file SharedArray.h.

50  {
51  if( &rhs == this )
52  return *this;
53 
54  rhs.attach();
55  release();
56 
57  m_size = rhs.m_size;
58  m_buffer = rhs.m_buffer;
59 
60  return *this;
61  }
std::size_t m_size
Definition: SharedArray.h:145

◆ release()

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

Definition at line 124 of file SharedArray.h.

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

125  {
126  if( empty() )
127  return;
128 
129  //free object if reference count has decreased to zero
130  if( decrement_reference_count() == 0)
131  {
132  T * tmpBuff = m_buffer;
133  atomic_count* tmpCounter = get_counter();
134 
135  m_buffer = 0;
136  m_size = 0;
137 
138  //explicitly call destructor for the counter object
139  tmpCounter->~atomic_count();
140 
141  delete [] tmpBuff;
142  }
143  }
atomic_count * get_counter() const
Definition: SharedArray.h:101
long decrement_reference_count()
Definition: SharedArray.h:112
std::size_t m_size
Definition: SharedArray.h:145
bool empty() const
Definition: SharedArray.h:66

◆ size()

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

Definition at line 63 of file SharedArray.h.

Referenced by FIX::shared_array< int >::get_counter().

64  { return m_size; }
std::size_t m_size
Definition: SharedArray.h:145

Member Data Documentation

◆ m_buffer

template<typename T>
T* FIX::shared_array< T >::m_buffer
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 Thu Sep 5 2019 11:07:58 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001