OnixS C++ CME MDP Premium Market Data Handler 5.11.1
Users' manual and 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_CMEMDH_ENABLE_TRACING)
24# include <ostream>
25#endif
26
27#include <cstddef>
28#include <new>
29#include <type_traits>
30#include <utility>
31
34#include <OnixS/CME/MDH/Utils.h>
35
37
43
52{
53public:
57 virtual void release() noexcept = 0;
58
65 virtual void* allocate(size_t size) = 0;
66
70 virtual void deallocate(void* block) noexcept = 0;
71
72#if defined(ONIXS_CMEMDH_ENABLE_TRACING)
76 virtual void trace(std::ostream& stream) = 0;
77#endif
78
79protected:
81 MemoryPool() noexcept = default;
82
85 virtual ~MemoryPool() noexcept = default;
86
87private:
88 // Copies are disallowed.
89 MemoryPool(const MemoryPool&) noexcept = default;
90
91 // Assignment is disallowed.
92 MemoryPool& operator=(const MemoryPool&) noexcept = default;
93};
94
101template <class Object>
103{
104public:
106 using value_type = Object;
107
109 using pointer = Object*;
110
112 using const_pointer = const Object*;
113
115 using reference = Object&;
116
118 using const_reference = const Object&;
119
121 using size_type = std::size_t;
122
124 using difference_type = std::ptrdiff_t;
125
128 template <class Other>
129 struct rebind
130 {
133 };
134
139 : pool_(nullptr)
140 {
141 }
142
147 : pool_(&memoryPool)
148 {
149 }
150
153 MemoryPoolAllocator(const MemoryPoolAllocator& other) noexcept = default;
154
158 template <class Other>
160 : pool_(other.pool_)
161 {
162 }
163
165 ~MemoryPoolAllocator() noexcept = default;
166
171 MemoryPool& memoryPool() const noexcept
172 {
173 return *pool_;
174 }
175
180 pointer address(reference obj) const noexcept
181 {
182 return &obj;
183 }
184
190 {
191 return &x;
192 }
193
204 {
205 static_assert(
206 alignof(value_type) <= alignof(std::max_align_t),
207 "value_type must not require extended alignment"
208 );
209
210 assert(nullptr != pool_);
211
212 if ONIXS_CMEMDH_UNLIKELY(qty > max_size())
213 ::ONIXS_CMEMDH_NAMESPACE::details::throwMemoryPoolAllocatorSizeTooLarge();
214
215 return static_cast<pointer>(pool_->allocate(qty * sizeof(value_type)));
216 }
217
223 void deallocate(pointer block, size_type qty) noexcept
224 {
225 Utils::unusedArgs(qty);
226
227 assert(nullptr != pool_);
228
229 pool_->deallocate(block);
230 }
231
238 template <class Item, class... Args>
239 void construct(Item* block, Args&&... args)
240 {
241 ::new (static_cast<void*>(block)) Item(std::forward<Args>(args)...);
242 }
243
247 void destroy(pointer obj) noexcept
248 {
249 static_assert(
250 std::is_nothrow_destructible<value_type>::value,
251 "value_type must be nothrow-destructible"
252 );
253
254 obj->~value_type();
255 Utils::unusedArgs(obj);
256 }
257
261 size_type max_size() const noexcept
262 {
263 return static_cast<size_type>(-1) / sizeof(value_type);
264 }
265
269 MemoryPoolAllocator& operator=(const MemoryPoolAllocator& other) noexcept = default;
270
275 template <class Other>
277 {
278 pool_ = other.pool_;
279
280 return *this;
281 }
282
283private:
284 template <class> friend class MemoryPoolAllocator;
285
286 template <class LeftObject, class RightObject>
287 friend bool operator==(
288 const MemoryPoolAllocator<LeftObject>& left, const MemoryPoolAllocator<RightObject>& right) noexcept;
289
290 MemoryPool* pool_;
291};
292
300template <class Object, class OtherObject>
302inline bool operator==(const MemoryPoolAllocator<Object>& left, const MemoryPoolAllocator<OtherObject>& right) noexcept
303{
304 return (left.pool_ == right.pool_);
305}
306
314template <class Object, class OtherObject>
316inline bool operator!=(const MemoryPoolAllocator<Object>& left, const MemoryPoolAllocator<OtherObject>& right) noexcept
317{
318 return !(left == right);
319}
320
329
340MemoryPool* makeMemoryPool(size_t blockIncrease, size_t increaseQty, size_t chunkSize, size_t chunkScaling)
341{
342 return makeMemoryPool(MemoryPoolSettings(blockIncrease, increaseQty, chunkSize, chunkScaling));
343}
344
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
Definition Bootstrap.h:69
#define ONIXS_CMEMDH_NAMESPACE_END
Definition Bootstrap.h:73
#define ONIXS_CMEMDH_COLDPATH
Definition Compiler.h:168
#define ONIXS_CMEMDH_EXPORTED
Definition Compiler.h:163
#define ONIXS_CMEMDH_NORETURN
Definition Compiler.h:164
#define ONIXS_CMEMDH_NODISCARD
Definition Compiler.h:165
Adapts a MemoryPool for use as a standard allocator.
Definition MemoryPool.h:103
MemoryPoolAllocator & operator=(const MemoryPoolAllocator< Other > &other) noexcept
Replaces this allocator's pool association with a compatible source association.
Definition MemoryPool.h:276
void deallocate(pointer block, size_type qty) noexcept
Returns storage to the associated pool.
Definition MemoryPool.h:223
MemoryPoolAllocator(const MemoryPoolAllocator &other) noexcept=default
Copies the source allocator's pool association.
void destroy(pointer obj) noexcept
Destroys an object without deallocating its storage.
Definition MemoryPool.h:247
const_pointer address(const_reference x) const noexcept
Obtains the address of a read-only object.
Definition MemoryPool.h:189
MemoryPoolAllocator(MemoryPool &memoryPool) noexcept
Associates the allocator with a pool.
Definition MemoryPool.h:146
pointer address(reference obj) const noexcept
Obtains the address of a mutable object.
Definition MemoryPool.h:180
pointer allocate(size_type qty)
Allocates uninitialized storage for a sequence of objects.
Definition MemoryPool.h:203
MemoryPool & memoryPool() const noexcept
Definition MemoryPool.h:171
MemoryPoolAllocator(const MemoryPoolAllocator< Other > &other) noexcept
Copies a compatible allocator's pool association.
Definition MemoryPool.h:159
friend bool operator==(const MemoryPoolAllocator< LeftObject > &left, const MemoryPoolAllocator< RightObject > &right) noexcept
~MemoryPoolAllocator() noexcept=default
Destroys the adapter without releasing its pool or outstanding storage.
void construct(Item *block, Args &&... args)
Constructs an object in allocated storage.
Definition MemoryPool.h:239
MemoryPoolAllocator() noexcept
Creates an allocator that is not associated with a pool.
Definition MemoryPool.h:138
MemoryPoolAllocator & operator=(const MemoryPoolAllocator &other) noexcept=default
Replaces this allocator's pool association with the source association.
Configures allocation and caching behavior of the handler's memory pool.
Allocates and reclaims raw memory blocks.
Definition MemoryPool.h:52
virtual void release() noexcept=0
Destroys this pool and releases its storage.
MemoryPool() noexcept=default
Initializes the base of a concrete pool implementation.
virtual void * allocate(size_t size)=0
Obtains a raw memory block.
virtual void deallocate(void *block) noexcept=0
Returns a previously allocated block to its owning pool.
void throwMemoryPoolAllocatorSizeTooLarge()
MemoryPool * makeMemoryPool(const MemoryPoolSettings &settings)
Creates a memory pool using the supplied settings.
bool operator==(const MemoryPoolAllocator< Object > &left, const MemoryPoolAllocator< OtherObject > &right) noexcept
Tests whether two allocators use the same pool.
Definition MemoryPool.h:302
bool operator!=(const MemoryPoolAllocator< Object > &left, const MemoryPoolAllocator< OtherObject > &right) noexcept
Tests whether two allocators use different pools.
Definition MemoryPool.h:316
Produces an allocator for another object type using the same pool.
Definition MemoryPool.h:130
MemoryPoolAllocator< Other > other
Allocator type for Other.
Definition MemoryPool.h:132