CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
allocator.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_MEMORY_ALLOCATOR_H
10#define CPPCOMMON_MEMORY_ALLOCATOR_H
11
12#include "memory.h"
13
14namespace CppCommon {
15
17
23template <typename T, class TMemoryManager, bool nothrow = false>
25{
26 template <typename U, class UMemoryManager, bool flag>
27 friend class Allocator;
28
29public:
31 typedef T value_type;
33 typedef T* pointer;
35 typedef T& reference;
37 typedef const T* const_pointer;
39 typedef const T& const_reference;
41 typedef size_t size_type;
43 typedef ptrdiff_t difference_type;
44
46
49 explicit Allocator(TMemoryManager& manager) noexcept : _manager(manager) {}
50 template <typename U>
51 Allocator(const Allocator<U, TMemoryManager, nothrow>& alloc) noexcept : _manager(alloc._manager) {}
52 Allocator(const Allocator& alloc) noexcept : _manager(alloc._manager) {}
53 Allocator(Allocator&&) noexcept = default;
54 ~Allocator() noexcept = default;
55
56 template <typename U>
57 Allocator& operator=(const Allocator<U, TMemoryManager, nothrow>& alloc) noexcept
58 { _manager = alloc._manager; return *this; }
59 Allocator& operator=(const Allocator& alloc) noexcept
60 { _manager = alloc._manager; return *this; }
61 Allocator& operator=(Allocator&&) noexcept = default;
62
64
68 pointer address(reference x) const noexcept { return std::addressof(x); }
70
74 const_pointer address(const_reference x) const noexcept { return std::addressof(x); }
75
77
80 size_type max_size() const noexcept { return _manager.max_size(); }
81
83
88 pointer allocate(size_type num, const void* hint = 0);
90
94 void deallocate(pointer ptr, size_type num);
95
97 void reset() { _manager.reset(); }
98
100
104 template <typename U, class... Args>
105 void construct(U* ptr, Args&&... args);
107
110 template <typename U>
111 void destroy(U* ptr);
112
114
117 template <class... Args>
118 T* Create(Args&&... args);
120
123 void Release(T* ptr);
124
126
130 template <class... Args>
131 T* CreateArray(size_t length, Args&&... args);
133
136 void ReleaseArray(T* ptr);
137
139 template <typename TOther> struct rebind { using other = Allocator<TOther, TMemoryManager, nothrow>; };
140
141private:
142 TMemoryManager& _manager;
143};
144
146template <class TMemoryManager, bool nothrow>
147class Allocator<void, TMemoryManager, nothrow>
148{
149public:
151 typedef void value_type;
153 typedef void* pointer;
155 typedef const void* const_pointer;
157 typedef size_t size_type;
159 typedef ptrdiff_t difference_type;
160
161 Allocator(TMemoryManager& manager) noexcept : _manager(manager) {}
162 template <typename U>
163 Allocator(const Allocator<U, TMemoryManager, nothrow>& alloc) noexcept : _manager(alloc._manager) {}
164 Allocator(Allocator&&) noexcept = default;
165 ~Allocator() noexcept = default;
166
167 template <typename U>
168 Allocator& operator=(const Allocator<U, TMemoryManager, nothrow>& alloc) noexcept
169 { _manager = alloc._manager; return *this; }
170 Allocator& operator=(Allocator&&) noexcept = default;
171
173 template <typename TOther> struct rebind { using other = Allocator<TOther, TMemoryManager, nothrow>; };
174
175private:
176 TMemoryManager _manager;
177};
178
180
187{
188public:
189 DefaultMemoryManager() noexcept : _allocated(0), _allocations(0) {}
190 DefaultMemoryManager(const DefaultMemoryManager&) noexcept = default;
192 ~DefaultMemoryManager() noexcept { reset(); }
193
196
198 size_t allocated() const noexcept { return _allocated; }
200 size_t allocations() const noexcept { return _allocations; }
201
203 size_t max_size() const noexcept { return std::numeric_limits<size_t>::max(); }
204
206
211 void* malloc(size_t size, size_t alignment = alignof(std::max_align_t));
213
217 void free(void* ptr, size_t size);
218
220 void reset();
221
222private:
223 // Allocation statistics
224 size_t _allocated;
225 size_t _allocations;
226};
227
229template <typename T, bool nothrow = false>
231
232} // namespace CppCommon
233
234#include "allocator.inl"
235
236#endif // CPPCOMMON_MEMORY_ALLOCATOR_H
Memory allocator inline implementation.
Allocator & operator=(Allocator &&) noexcept=default
ptrdiff_t difference_type
Difference between two pointers.
Definition allocator.h:159
Allocator(const Allocator< U, TMemoryManager, nothrow > &alloc) noexcept
Definition allocator.h:163
const void * const_pointer
Pointer to constant element.
Definition allocator.h:155
Allocator(TMemoryManager &manager) noexcept
Definition allocator.h:161
Memory allocator class.
Definition allocator.h:25
void construct(U *ptr, Args &&... args)
Constructs an element object on the given location pointer.
Definition allocator.inl:50
void ReleaseArray(T *ptr)
Release an array of element objects.
const T & const_reference
Reference to constant element.
Definition allocator.h:39
void Release(T *ptr)
Release a single element object.
Definition allocator.inl:85
T value_type
Element type.
Definition allocator.h:31
T * CreateArray(size_t length, Args &&... args)
Create an array of element objects.
Allocator(Allocator &&) noexcept=default
void deallocate(pointer ptr, size_type num)
Release a block of storage previously allocated.
Definition allocator.inl:43
Allocator & operator=(Allocator &&) noexcept=default
T * Create(Args &&... args)
Create a single element object.
Definition allocator.inl:72
pointer address(reference x) const noexcept
Get the address of the given reference.
Definition allocator.h:68
ptrdiff_t difference_type
Difference between two pointers.
Definition allocator.h:43
Allocator(const Allocator &alloc) noexcept
Definition allocator.h:52
const T * const_pointer
Pointer to constant element.
Definition allocator.h:37
T * pointer
Pointer to element.
Definition allocator.h:33
Allocator(const Allocator< U, TMemoryManager, nothrow > &alloc) noexcept
Definition allocator.h:51
Allocator(TMemoryManager &manager) noexcept
Initialize allocator with a given memory manager.
Definition allocator.h:49
void reset()
Reset the allocator.
Definition allocator.h:97
pointer allocate(size_type num, const void *hint=0)
Allocate a block of storage suitable to contain the given count of elements.
Definition allocator.inl:29
void destroy(U *ptr)
Destroys in-place the object pointed by the given location pointer.
Definition allocator.inl:61
T & reference
Reference to element.
Definition allocator.h:35
size_t size_type
Quantities of elements.
Definition allocator.h:41
Allocator & operator=(const Allocator &alloc) noexcept
Definition allocator.h:59
const_pointer address(const_reference x) const noexcept
Get the constant address of the given constant reference.
Definition allocator.h:74
size_type max_size() const noexcept
Get the maximum number of elements, that could potentially be allocated by the allocator.
Definition allocator.h:80
Default memory manager class.
Definition allocator.h:187
DefaultMemoryManager & operator=(const DefaultMemoryManager &) noexcept=default
size_t allocations() const noexcept
Count of active memory allocations.
Definition allocator.h:200
void free(void *ptr, size_t size)
Free the previously allocated memory block.
DefaultMemoryManager(DefaultMemoryManager &&) noexcept=default
void reset()
Reset the memory manager.
void * malloc(size_t size, size_t alignment=alignof(std::max_align_t))
Allocate a new memory block of the given size.
DefaultMemoryManager & operator=(DefaultMemoryManager &&) noexcept=default
size_t allocated() const noexcept
Allocated memory in bytes.
Definition allocator.h:198
DefaultMemoryManager(const DefaultMemoryManager &) noexcept=default
size_t max_size() const noexcept
Maximum memory block size, that could be allocated by the memory manager.
Definition allocator.h:203
Memory management definition.
C++ Common project definitions.