SharedArray.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 /****************************************************************************
4 ** Copyright (c) 2001-2014
5 **
6 ** This file is part of the QuickFIX FIX Engine
7 **
8 ** This file may be distributed under the terms of the quickfixengine.org
9 ** license as defined by quickfixengine.org and appearing in the file
10 ** LICENSE included in the packaging of this file.
11 **
12 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
13 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 **
15 ** See http://www.quickfixengine.org/LICENSE for licensing information.
16 **
17 ** Contact ask@quickfixengine.org if any conditions of this licensing are
18 ** not clear to you.
19 **
20 ****************************************************************************/
21 
22 #ifndef SHARED_ARRAY
23 #define SHARED_ARRAY
24 
25 #include "AtomicCount.h"
26 
27 namespace FIX
28 {
30  template<typename T>
32  {
33  public:
35  : m_size(0)
36  , m_buffer(0)
37  {}
38 
40  : m_size(rhs.m_size)
41  , m_buffer(rhs.m_buffer)
42  {
43  rhs.attach();
44  }
45 
47  { release(); }
48 
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  }
62 
63  std::size_t size() const
64  { return m_size; }
65 
66  bool empty() const
67  { return m_buffer == 0; }
68 
69  operator T* () const
70  { return m_buffer; }
71 
72  //optimized function to allocate storage for buffer and counter object at once
73  static shared_array create(const std::size_t nSize)
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  }
91 
92  private:
93 
94  shared_array( T * buff, std::size_t nSize )
95  : m_size(nSize)
96  , m_buffer(buff)
97  {
98 
99  }
100 
102  {
103  return reinterpret_cast<atomic_count*>( &m_buffer[ size() ] );
104  }
105 
107  {
108  atomic_count* counter = get_counter();
109  ++(*counter);
110  }
111 
113  {
114  atomic_count* counter = get_counter();
115  return --(*counter);
116  }
117 
118  void attach() const
119  {
120  if( !empty() )
122  }
123 
124  void release()
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  }
144 
145  std::size_t m_size;
146  T * m_buffer;
147  };
148 }
149 
150 #endif
void increment_reference_count() const
Definition: SharedArray.h:106
Atomic count class - consider using interlocked functions.
Definition: AtomicCount.h:71
shared_array & operator=(const shared_array &rhs)
Definition: SharedArray.h:49
atomic_count * get_counter() const
Definition: SharedArray.h:101
long decrement_reference_count()
Definition: SharedArray.h:112
Definition: Acceptor.cpp:34
std::size_t m_size
Definition: SharedArray.h:145
shared_array(T *buff, std::size_t nSize)
Definition: SharedArray.h:94
void attach() const
Definition: SharedArray.h:118
Shared array with atomic reference count.
Definition: SharedArray.h:31
std::size_t size() const
Definition: SharedArray.h:63
bool empty() const
Definition: SharedArray.h:66
shared_array(const shared_array &rhs)
Definition: SharedArray.h:39
static shared_array create(const std::size_t nSize)
Definition: SharedArray.h:73

Generated on Thu Sep 5 2019 11:07:58 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001