OnixS C++ CME Streamlined Market Data Handler  1.0.0.4
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_CMESTREAMLINEDMDH_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_CMESTREAMLINEDMDH_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  }
202 
203  size_type max_size() const
204  {
205  return
206  static_cast<size_type>(-1) /
207  sizeof(value_type);
208  }
209 
211  operator =(
212  const MemoryPoolAllocator& other)
213  {
214  pool_ = other.pool_;
215 
216  return *this;
217  }
218 
219  template
220  <
221  class Other
222  >
224  operator =(
225  const MemoryPoolAllocator<Other>& other)
226  {
227  pool_ = &other.memoryPool();
228 
229  return *this;
230  }
231 };
232 
233 template
234 <
235  class Object,
236  class OtherObject
237 >
238 inline
239 bool
241  const MemoryPoolAllocator<Object>& left,
242  const MemoryPoolAllocator<OtherObject>& right)
243 {
244  return
245  (
246  &left.memoryPool() == &right.memoryPool()
247  );
248 }
249 
250 template
251 <
252  class Object,
253  class OtherObject
254 >
255 inline
256 bool
258  const MemoryPoolAllocator<Object>& left,
259  const MemoryPoolAllocator<OtherObject>& right)
260 {
261  return
262  (
263  &left.memoryPool() != &right.memoryPool()
264  );
265 }
266 
267 /// Constructs memory pool instance
268 /// according to given configuration.
269 ONIXS_CMESTREAMLINEDMDH_EXPORTED
270 MemoryPool*
272  size_t, size_t, size_t, size_t);
273 
MemoryPoolAllocator(const MemoryPoolAllocator &other)
Definition: MemoryPool.h:124
bool operator==(const MemoryPoolAllocator< Object > &left, const MemoryPoolAllocator< OtherObject > &right)
Definition: MemoryPool.h:240
ONIXS_CMESTREAMLINEDMDH_EXPORTED MemoryPool * makeMemoryPool(size_t, size_t, size_t, size_t)
virtual void deallocate(void *)=0
Releases previously allocated memory block.
virtual void * allocate(size_t)=0
Allocates memory block of given size.
const_pointer address(const_reference x) const
Definition: MemoryPool.h:157
void construct(pointer block, const value_type &obj)
Definition: MemoryPool.h:189
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_END
Definition: Bootstrap.h:172
#define ONIXS_CMESTREAMLINEDMDH_EXPORTED_CLASS
Definition: Bootstrap.h:62
void deallocate(pointer block, size_type)
Definition: MemoryPool.h:179
virtual ~MemoryPool()
Destruction is through descendants.
Definition: MemoryPool.h:63
MemoryPoolAllocator(const MemoryPoolAllocator< Other > &other)
Definition: MemoryPool.h:134
bool operator!=(const MemoryPoolAllocator< Object > &left, const MemoryPoolAllocator< OtherObject > &right)
Definition: MemoryPool.h:257
#define ONIXS_CMESTREAMLINEDMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:168