OnixS C++ CME MDP Conflated UDP Handler 1.1.2
API documentation
Loading...
Searching...
No Matches
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_CONFLATEDUDP_ENABLE_TRACING)
24#include <ostream>
25#endif
26
27#include <cassert>
28
30
32
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
53protected:
59 {
60 }
61
63 virtual ~MemoryPool()
64 {
65 }
66
67public:
69 virtual void release() = 0;
70
72 virtual void* allocate(size_t) = 0;
73
75 virtual void deallocate(void*) = 0;
76
77#if defined (ONIXS_CONFLATEDUDP_ENABLE_TRACING)
78 virtual void trace(std::ostream&) = 0;
79#endif
80};
81
82// Implements STL allocator over MemoryPool.
83template
84<
85 class Object
86>
88{
89 MemoryPool* pool_;
90
91public:
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
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
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
234template
235<
236 class Object,
237 class OtherObject
238>
239inline
240bool
241operator ==(
242 const MemoryPoolAllocator<Object>& left,
244{
245 return (
246 &left.memoryPool() ==
247 &right.memoryPool()
248 );
249}
250
251template
252<
253 class Object,
254 class OtherObject
255>
256inline
257bool
258operator !=(
259 const MemoryPoolAllocator<Object>& left,
261{
262 return (
263 &left.memoryPool() !=
264 &right.memoryPool()
265 );
266}
267
270ONIXS_CONFLATEDUDP_EXPORTED
271MemoryPool*
273 size_t, size_t, size_t, size_t);
274
#define ONIXS_CONFLATEDUDP_EXPORTED_CLASS
Definition Bootstrap.h:55
#define ONIXS_CONFLATEDUDP_NAMESPACE_END
Definition Bootstrap.h:157
#define ONIXS_CONFLATEDUDP_NAMESPACE_BEGIN
Definition Bootstrap.h:153
MemoryPoolAllocator(const MemoryPoolAllocator &other)
Definition MemoryPool.h:124
MemoryPoolAllocator(const MemoryPoolAllocator< Other > &other)
Definition MemoryPool.h:134
const_pointer address(const_reference x) const
Definition MemoryPool.h:157
void deallocate(pointer block, size_type)
Definition MemoryPool.h:179
void construct(pointer block, const value_type &obj)
Definition MemoryPool.h:189
pointer address(reference obj) const
Definition MemoryPool.h:150
virtual void deallocate(void *)=0
Releases previously allocated memory block.
virtual ~MemoryPool()
Destruction is through descendants.
Definition MemoryPool.h:63
virtual void release()=0
Releases given instance.
virtual void * allocate(size_t)=0
Allocates memory block of given size.
ONIXS_CONFLATEDUDP_EXPORTED MemoryPool * makeMemoryPool(size_t, size_t, size_t, size_t)