CppServer 1.0.4.0
C++ Server Library
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1
9#ifndef CPPSERVER_ASIO_MEMORY_H
10#define CPPSERVER_ASIO_MEMORY_H
11
12#include <memory>
13
14namespace CppServer {
15namespace Asio {
16
18
27{
28public:
29 HandlerStorage() noexcept : _in_use(false) {}
32 ~HandlerStorage() noexcept = default;
33
34 HandlerStorage& operator=(const HandlerStorage&) = delete;
35 HandlerStorage& operator=(HandlerStorage&&) = delete;
36
38
42 void* allocate(size_t size);
44
47 void deallocate(void* ptr);
48
49private:
50 // Whether the handler-based custom allocation storage has been used
51 bool _in_use;
52 // Storage space used for handler-based custom memory allocation
53 std::byte _storage[1024];
54};
55
57
63template <typename T>
65{
66 template <typename>
67 friend class HandlerAllocator;
68
69public:
71 typedef T value_type;
73 typedef T* pointer;
75 typedef T& reference;
77 typedef const T* const_pointer;
79 typedef const T& const_reference;
81 typedef size_t size_type;
83 typedef ptrdiff_t difference_type;
84
86
89 explicit HandlerAllocator(HandlerStorage& storage) noexcept : _storage(storage) {}
90 template <typename U>
91 HandlerAllocator(const HandlerAllocator<U>& alloc) noexcept : _storage(alloc._storage) {}
92 HandlerAllocator(const HandlerAllocator& alloc) noexcept : _storage(alloc._storage) {}
93 HandlerAllocator(HandlerAllocator&&) noexcept = default;
94 ~HandlerAllocator() noexcept = default;
95
96 template <typename U>
97 HandlerAllocator& operator=(const HandlerAllocator<U>& alloc) noexcept
98 { _storage = alloc._storage; return *this; }
100 { _storage = alloc._storage; return *this; }
102
104
109 pointer allocate(size_type num, const void* hint = 0) { return (pointer)_storage.allocate(num * sizeof(T)); }
111
115 void deallocate(pointer ptr, size_type num) { return _storage.deallocate(ptr); }
116
117private:
118 // The underlying handler storage
119 HandlerStorage& _storage;
120};
121
123
131template <typename THandler>
133{
134public:
137
139
143 AllocateHandler(HandlerStorage& storage, THandler handler) noexcept : _storage(storage), _handler(handler) {}
144 AllocateHandler(const AllocateHandler&) noexcept = default;
145 AllocateHandler(AllocateHandler&&) noexcept = default;
146 ~AllocateHandler() noexcept = default;
147
148 AllocateHandler& operator=(const AllocateHandler&) noexcept = default;
149 AllocateHandler& operator=(AllocateHandler&&) noexcept = default;
150
152 allocator_type get_allocator() const noexcept { return allocator_type(_storage); }
153
155 template <typename ...Args>
156 void operator()(Args&&... args) { _handler(std::forward<Args>(args)...); }
157
158private:
159 HandlerStorage& _storage;
160 THandler _handler;
161};
162
164
168template <typename THandler>
169AllocateHandler<THandler> make_alloc_handler(HandlerStorage& storage, THandler handler);
170
171} // namespace Asio
172} // namespace CppServer
173
174#include "memory.inl"
175
176#endif // CPPSERVER_ASIO_MEMORY_H
Asio allocate handler wrapper.
Definition memory.h:133
AllocateHandler(const AllocateHandler &) noexcept=default
HandlerAllocator< THandler > allocator_type
Allocator type.
Definition memory.h:136
AllocateHandler(HandlerStorage &storage, THandler handler) noexcept
Initialize allocate handler wrapper with a given memory storage and handler.
Definition memory.h:143
void operator()(Args &&... args)
Wrap the handler.
Definition memory.h:156
AllocateHandler(AllocateHandler &&) noexcept=default
Asio handler allocator.
Definition memory.h:65
size_t size_type
Quantities of elements.
Definition memory.h:81
HandlerAllocator & operator=(HandlerAllocator &&) noexcept=default
HandlerAllocator(HandlerStorage &storage) noexcept
Initialize allocator with a given memory storage.
Definition memory.h:89
T & reference
Reference to element.
Definition memory.h:75
HandlerAllocator(const HandlerAllocator< U > &alloc) noexcept
Definition memory.h:91
HandlerAllocator(HandlerAllocator &&) noexcept=default
HandlerAllocator(const HandlerAllocator &alloc) noexcept
Definition memory.h:92
ptrdiff_t difference_type
Difference between two pointers.
Definition memory.h:83
HandlerAllocator & operator=(const HandlerAllocator &alloc) noexcept
Definition memory.h:99
const T * const_pointer
Pointer to constant element.
Definition memory.h:77
void deallocate(pointer ptr, size_type num)
Release a block of storage previously allocated.
Definition memory.h:115
pointer allocate(size_type num, const void *hint=0)
Allocate a block of storage suitable to contain the given count of elements.
Definition memory.h:109
const T & const_reference
Reference to constant element.
Definition memory.h:79
T * pointer
Pointer to element.
Definition memory.h:73
Asio handler storage.
Definition memory.h:27
void deallocate(void *ptr)
Deallocate memory buffer.
Definition memory.inl:25
HandlerStorage(HandlerStorage &&)=delete
HandlerStorage(const HandlerStorage &)=delete
~HandlerStorage() noexcept=default
void * allocate(size_t size)
Allocate memory buffer.
Definition memory.inl:12
Asio memory manager inline implementation.
AllocateHandler< THandler > make_alloc_handler(HandlerStorage &storage, THandler handler)
Helper function to wrap a handler object to add custom allocation.
Definition memory.inl:39
C++ Server project definitions.
Definition asio.h:56