FIX::shared_array< T > Class Template Reference

Shared array with atomic reference count. More...

`#include <SharedArray.h>`

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

## Public Member Functions

| shared_array ()
| shared_array (const shared_array &rhs)
| ~shared_array ()
shared_array & | operator= (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_count * | get_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.

: m_size(0)
, m_buffer(0)
, m_pCtr(0)
{}
FIX::shared_array::m_size
std::size_t m_size
Definition SharedArray.h:277
FIX::shared_array::m_pCtr
void * m_pCtr
Definition SharedArray.h:279
FIX::shared_array::m_buffer
T * m_buffer
Definition SharedArray.h:278

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.

: m_size(rhs.m_size)
, m_buffer(rhs.m_buffer)
{
rhs.attach();
}

## ~shared_array()

template<typename T >

FIX::shared_array< T >::~shared_array | ( |  | ) |

inline

Definition at line 173 of file SharedArray.h.

{ release(); }
FIX::shared_array::release
void release()
Definition SharedArray.h:256

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
| ) |  |

inline private

Definition at line 225 of file SharedArray.h.

: m_size(nSize)
, m_buffer(buff)
, m_pCtr(pCtr)
{
}

## Member Function Documentation

## attach()

template<typename T >

void FIX::shared_array< T >::attach | ( |  | ) | const

inline private

Definition at line 250 of file SharedArray.h.

{
if( !empty() )
increment_reference_count();
}
FIX::shared_array::increment_reference_count
void increment_reference_count() const
Definition SharedArray.h:238
FIX::shared_array::empty
bool empty() const
Definition SharedArray.h:194

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

## create()

template<typename T >

static shared_array FIX::shared_array< T >::create | ( | const std::size_t | nSize | ) |

inline static

Definition at line 201 of file SharedArray.h.

{
if(nSize <= 0)
return shared_array();
//verify the needed buffer size to allocate counter object and nSize elements
const std::size_t sizeToAllocate = (nSize * sizeof(T)) + sizeof(atomic_count) + 15;
//allocate and zero-fill the buffer
void * buf = std::malloc(sizeToAllocate);
memset(buf, 0, sizeToAllocate);
// create the counter object at the end of the storage
// with initial reference count set to 1
/* round up to multiple of alignment : add (alignment - 1) and then round down by masking */
void *ptr = (void *) (((uintptr_t)(buf) + nSize * sizeof(T) + 15) & ~ (uintptr_t)0x0F);
new (ptr) atomic_count( 1 );
T* storage = reinterpret_cast<T*>(buf);
return shared_array(storage, nSize, ptr);
}
FIX::shared_array::shared_array
shared_array()
Definition SharedArray.h:160
uintptr_t
_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

inline private

Definition at line 244 of file SharedArray.h.

{
atomic_count* counter = get_counter();
return --(*counter);
}
FIX::shared_array::get_counter
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

Definition at line 194 of file SharedArray.h.

{ return m_buffer == 0; }

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

Referenced by FIX::shared_array< T >::attach(), FIX::message_order::operator bool(), and FIX::shared_array< T >::release().

## get_counter()

template<typename T >

atomic_count * FIX::shared_array< T >::get_counter | ( |  | ) | const

inline private

Definition at line 233 of file SharedArray.h.

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

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

inline private

Definition at line 238 of file SharedArray.h.

{
atomic_count* counter = get_counter();
++(*counter);
}

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.

{ 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.

{
if( &rhs == this )
return *this;
rhs.attach();
release();
m_size = rhs.m_size;
m_buffer = rhs.m_buffer;
m_pCtr = rhs.m_pCtr;
return *this;
}

References 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 | ( |  | ) |

inline private

Definition at line 256 of file SharedArray.h.

{
if( empty() )
return;
//free object if reference count has decreased to zero
if( decrement_reference_count() == 0)
{
T * tmpBuff = m_buffer;
atomic_count* tmpCounter = get_counter();
m_buffer = 0;
m_size = 0;
//explicitly call destructor for the counter object
tmpCounter->~atomic_count();
std::free(tmpBuff);
}
}
FIX::shared_array::decrement_reference_count
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.

{ 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

Definition at line 278 of file SharedArray.h.

Referenced by FIX::shared_array< T >::empty(), FIX::shared_array< int >::operator int *(), FIX::shared_array< T >::operator=(), and FIX::shared_array< T >::release().

## m_pCtr

template<typename T >

void* FIX::shared_array< T >::m_pCtr

private

Definition at line 279 of file SharedArray.h.

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

## m_size

template<typename T >

std::size_t FIX::shared_array< T >::m_size

private

Definition at line 277 of file SharedArray.h.

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

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

- SharedArray.h