OnixS C++ CME MDP Premium Market Data Handler  5.8.9
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 
28 
30 
31 /// Memory pool abstraction.
32 ///
33 /// Defines interface for memory block
34 /// allocation and disposing.
36 {
37 public:
38  /// Releases given instance.
39  virtual void release() = 0;
40 
41  /// Allocates memory block of given size.
42  virtual void* allocate(size_t) = 0;
43 
44  /// Releases previously allocated memory block.
45  virtual void deallocate(void*) = 0;
46 
47 #if defined(ONIXS_CMEMDH_ENABLE_TRACING)
48  virtual void trace(std::ostream&) = 0;
49 #endif
50 
51 protected:
52  /// Default initialization.
53  ///
54  /// Instance construction is
55  /// assumed through descendants.
57 
58  /// Destruction is through descendants.
59  virtual ~MemoryPool() {}
60 
61 private:
62  // Copies are disallowed.
63  MemoryPool(const MemoryPool&) {}
64 
65  // Assignment is disallowed.
66  MemoryPool& operator=(const MemoryPool&)
67  {
68  return *this;
69  }
70 };
71 
72 // Implements STL allocator over MemoryPool.
73 template <class Object>
75 {
76 public:
77  typedef Object value_type;
78  typedef Object* pointer;
79  typedef const Object* const_pointer;
80 
81  typedef Object& reference;
82  typedef const Object& const_reference;
83 
84  typedef std::size_t size_type;
85  typedef std::ptrdiff_t difference_type;
86 
87  template <class Other>
88  struct rebind
89  {
91  };
92 
94  : pool_(ONIXS_CMEMDH_NULLPTR)
95  {
96  }
97 
99  : pool_(&memoryPool)
100  {
101  }
102 
104  : pool_(other.pool_)
105  {
106  }
107 
108  template <class Other>
110  : pool_(&other.memoryPool())
111  {
112  }
113 
115 
117  {
118  return *pool_;
119  }
120 
121  pointer address(reference obj) const
122  {
123  return &obj;
124  }
125 
126  const_pointer address(const_reference x) const
127  {
128  return &x;
129  }
130 
131  pointer allocate(size_type qty)
132  {
133  assert(ONIXS_CMEMDH_NULLPTR != pool_);
134 
135  return static_cast<pointer>(pool_->allocate(qty * sizeof(value_type)));
136  }
137 
138  void deallocate(pointer block, size_type)
139  {
140  assert(ONIXS_CMEMDH_NULLPTR != pool_);
141 
142  pool_->deallocate(block);
143  }
144 
145  void construct(pointer block, const value_type& obj)
146  {
147  new (block) value_type(obj);
148  }
149 
150  void destroy(pointer obj)
151  {
152  obj->~value_type();
153  (void) obj;
154  }
155 
156  size_type max_size() const
157  {
158  return static_cast<size_type>(-1) / sizeof(value_type);
159  }
160 
162  {
163  pool_ = other.pool_;
164 
165  return *this;
166  }
167 
168  template <class Other>
170  {
171  pool_ = &other.memoryPool();
172 
173  return *this;
174  }
175 
176 private:
177  MemoryPool* pool_;
178 };
179 
180 template <class Object, class OtherObject>
182 {
183  return (&left.memoryPool() == &right.memoryPool());
184 }
185 
186 template <class Object, class OtherObject>
188 {
189  return (&left.memoryPool() != &right.memoryPool());
190 }
191 
192 /// Constructs a memory pool instance
193 /// according to the given configuration.
195 MemoryPool* makeMemoryPool(size_t, size_t, size_t, size_t);
196 
void construct(pointer block, const value_type &obj)
Definition: MemoryPool.h:145
#define ONIXS_CMEMDH_NULLPTR
Definition: Compiler.h:167
Memory pool abstraction.
Definition: MemoryPool.h:35
MemoryPool()
Default initialization.
Definition: MemoryPool.h:56
void deallocate(pointer block, size_type)
Definition: MemoryPool.h:138
pointer address(reference obj) const
Definition: MemoryPool.h:121
MemoryPoolAllocator & operator=(const MemoryPoolAllocator &other)
Definition: MemoryPool.h:161
MemoryPoolAllocator(MemoryPool &memoryPool)
Definition: MemoryPool.h:98
MemoryPoolAllocator & operator=(const MemoryPoolAllocator< Other > &other)
Definition: MemoryPool.h:169
MemoryPool * makeMemoryPool(size_t, size_t, size_t, size_t)
Constructs a memory pool instance according to the given configuration.
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition: Bootstrap.h:67
virtual ~MemoryPool()
Destruction is through descendants.
Definition: MemoryPool.h:59
MemoryPoolAllocator(const MemoryPoolAllocator< Other > &other)
Definition: MemoryPool.h:109
#define ONIXS_CMEMDH_EXPORTED
Definition: Compiler.h:160
MemoryPool & memoryPool() const
Definition: MemoryPool.h:116
MemoryPoolAllocator< Other > other
Definition: MemoryPool.h:90
bool operator==(const MemoryPoolAllocator< Object > &left, const MemoryPoolAllocator< OtherObject > &right)
Definition: MemoryPool.h:181
bool operator!=(const MemoryPoolAllocator< Object > &left, const MemoryPoolAllocator< OtherObject > &right)
Definition: MemoryPool.h:187
pointer allocate(size_type qty)
Definition: MemoryPool.h:131
const_pointer address(const_reference x) const
Definition: MemoryPool.h:126
MemoryPoolAllocator(const MemoryPoolAllocator &other)
Definition: MemoryPool.h:103
#define ONIXS_CMEMDH_NAMESPACE_END
Definition: Bootstrap.h:68