OnixS C++ CME Market Data Handler  5.4.0
API documentation
MemoryPool.h
Go to the documentation of this file.
1 // Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2 //
3 // This software owned by Onix Solutions Limited [OnixS] and is
4 // protected by copyright law and international copyright treaties.
5 //
6 // Access to and use of the software is governed by the terms of the applicable
7 // OnixS Software Services Agreement (the Agreement) and Customer end user license
8 // agreements granting a non-assignable, non-transferable and non-exclusive license
9 // to use the software for it's own data processing purposes under the terms defined
10 // in the Agreement.
11 //
12 // Except as otherwise granted within the terms of the Agreement, copying or
13 // reproduction of any part of this source code or associated reference material
14 // to any other location for further reproduction or redistribution, and any
15 // amendments to this copyright notice, are expressly prohibited.
16 //
17 // Any reproduction or redistribution for sale or hiring of the Software not in
18 // accordance with the terms of the Agreement is a violation of copyright law.
19 //
20 
21 #pragma once
22 
23 #if defined (ONIXS_CMEMDH_ENABLE_TRACING)
24 #include <ostream>
25 #endif
26 
27 #include <cassert>
28 
30 
32 
33 /// Memory pool abstraction.
34 ///
35 /// Defines interface for memory block
36 /// allocation and disposing.
38 {
39  // Copies are disallowed.
40  MemoryPool(
41  const MemoryPool&)
42  {
43  }
44 
45  // Assignment is disallowed.
46  MemoryPool&
47  operator =(
48  const MemoryPool&)
49  {
50  return *this;
51  }
52 
53 protected:
54  /// Default initialization.
55  ///
56  /// Instance construction is
57  /// assumed through descendants.
59  {
60  }
61 
62  /// Destruction is through descendants.
63  virtual ~MemoryPool()
64  {
65  }
66 
67 public:
68  /// Releases given instance.
69  virtual void release() = 0;
70 
71  /// Allocates memory block of given size.
72  virtual void* allocate(size_t) = 0;
73 
74  /// Releases previously allocated memory block.
75  virtual void deallocate(void*) = 0;
76 
77 #if defined (ONIXS_CMEMDH_ENABLE_TRACING)
78  virtual void trace(std::ostream&) = 0;
79 #endif
80 };
81 
82 // Implements STL allocator over MemoryPool.
83 template
84 <
85  class Object
86 >
88 {
89  MemoryPool* pool_;
90 
91 public:
92  typedef Object value_type;
93  typedef Object* pointer;
94  typedef const Object* const_pointer;
95 
96  typedef Object& reference;
97  typedef const Object& const_reference;
98 
99  typedef std::size_t size_type;
100  typedef std::ptrdiff_t difference_type;
101 
102  template
103  <
104  class Other
105  >
106  struct rebind
107  {
108  typedef
111  };
112 
114  : pool_(NULL)
115  {
116  }
117 
119  MemoryPool& memoryPool)
120  : pool_(&memoryPool)
121  {
122  }
123 
125  const MemoryPoolAllocator& other)
126  : pool_(other.pool_)
127  {
128  }
129 
130  template
131  <
132  class Other
133  >
135  const MemoryPoolAllocator<Other>& other)
136  : pool_(&other.memoryPool())
137  {
138  }
139 
141  {
142  }
143 
145  {
146  return *pool_;
147  }
148 
149  pointer
151  reference obj) const
152  {
153  return &obj;
154  }
155 
156  const_pointer
158  const_reference x) const
159  {
160  return &x;
161  }
162 
163  pointer
165  size_type qty)
166  {
167  assert(NULL != pool_);
168 
169  return static_cast<pointer>
170  (
171  pool_->allocate
172  (
173  qty * sizeof(value_type)
174  )
175  );
176  }
177 
178  void
180  pointer block,
181  size_type)
182  {
183  assert(NULL != pool_);
184 
185  pool_->deallocate(block);
186  }
187 
188  void
190  pointer block,
191  const value_type& obj)
192  {
193  new (block) value_type(obj);
194  }
195 
196  void
198  pointer obj)
199  {
200  obj->~value_type();
201  (void) obj;
202  }
203 
204  size_type max_size() const
205  {
206  return
207  static_cast<size_type>(-1) /
208  sizeof(value_type);
209  }
210 
212  operator =(
213  const MemoryPoolAllocator& other)
214  {
215  pool_ = other.pool_;
216 
217  return *this;
218  }
219 
220  template
221  <
222  class Other
223  >
225  operator =(
226  const MemoryPoolAllocator<Other>& other)
227  {
228  pool_ = &other.memoryPool();
229 
230  return *this;
231  }
232 };
233 
234 template
235 <
236  class Object,
237  class OtherObject
238 >
239 inline
240 bool
242  const MemoryPoolAllocator<Object>& left,
243  const MemoryPoolAllocator<OtherObject>& right)
244 {
245  return (
246  &left.memoryPool() ==
247  &right.memoryPool()
248  );
249 }
250 
251 template
252 <
253  class Object,
254  class OtherObject
255 >
256 inline
257 bool
259  const MemoryPoolAllocator<Object>& left,
260  const MemoryPoolAllocator<OtherObject>& right)
261 {
262  return (
263  &left.memoryPool() !=
264  &right.memoryPool()
265  );
266 }
267 
268 /// Constructs a memory pool instance
269 /// according to the given configuration.
270 ONIXS_CMEMDH_EXPORTED
271 MemoryPool*
273  size_t, size_t, size_t, size_t);
274 
void construct(pointer block, const value_type &obj)
Definition: MemoryPool.h:189
ONIXS_CMEMDH_EXPORTED MemoryPool * makeMemoryPool(size_t, size_t, size_t, size_t)
virtual void * allocate(size_t)=0
Allocates memory block of given size.
void deallocate(pointer block, size_type)
Definition: MemoryPool.h:179
pointer address(reference obj) const
Definition: MemoryPool.h:150
MemoryPoolAllocator(MemoryPool &memoryPool)
Definition: MemoryPool.h:118
#define ONIXS_CMEMDH_EXPORTED_CLASS
Definition: Bootstrap.h:54
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:152
virtual ~MemoryPool()
Destruction is through descendants.
Definition: MemoryPool.h:63
MemoryPoolAllocator(const MemoryPoolAllocator< Other > &other)
Definition: MemoryPool.h:134
MemoryPool & memoryPool() const
Definition: MemoryPool.h:144
MemoryPoolAllocator< Other > other
Definition: MemoryPool.h:110
bool operator==(const MemoryPoolAllocator< Object > &left, const MemoryPoolAllocator< OtherObject > &right)
Definition: MemoryPool.h:241
bool operator!=(const MemoryPoolAllocator< Object > &left, const MemoryPoolAllocator< OtherObject > &right)
Definition: MemoryPool.h:258
pointer allocate(size_type qty)
Definition: MemoryPool.h:164
virtual void deallocate(void *)=0
Releases previously allocated memory block.
const_pointer address(const_reference x) const
Definition: MemoryPool.h:157
MemoryPoolAllocator(const MemoryPoolAllocator &other)
Definition: MemoryPool.h:124
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:156