CppCommon  1.0.4.1
C++ Common Library
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 
14 namespace CppCommon {
15 
17 
23 template <typename T, class TMemoryManager, bool nothrow = false>
24 class Allocator
25 {
26  template <typename U, class UMemoryManager, bool flag>
27  friend class Allocator;
28 
29 public:
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 
141 private:
142  TMemoryManager& _manager;
143 };
144 
146 template <class TMemoryManager, bool nothrow>
147 class Allocator<void, TMemoryManager, nothrow>
148 {
149 public:
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 
175 private:
176  TMemoryManager _manager;
177 };
178 
180 
187 {
188 public:
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 
222 private:
223  // Allocation statistics
224  size_t _allocated;
225  size_t _allocations;
226 };
227 
229 template <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.
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 & operator=(Allocator &&) noexcept=default
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.
Definition: allocator.inl:124
Allocator & operator=(Allocator &&) noexcept=default
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.
Definition: allocator.inl:102
Allocator(Allocator &&) noexcept=default
void deallocate(pointer ptr, size_type num)
Release a block of storage previously allocated.
Definition: allocator.inl:43
T * Create(Args &&... args)
Create a single element object.
Definition: allocator.inl:72
Allocator & operator=(const Allocator &alloc) noexcept
Definition: allocator.h:59
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
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
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.
Definition: allocator.inl:163
DefaultMemoryManager(DefaultMemoryManager &&) noexcept=default
void reset()
Reset the memory manager.
Definition: allocator.inl:177
void * malloc(size_t size, size_t alignment=alignof(std::max_align_t))
Allocate a new memory block of the given size.
Definition: allocator.inl:148
DefaultMemoryManager & operator=(const DefaultMemoryManager &) noexcept=default
size_t allocated() const noexcept
Allocated memory in bytes.
Definition: allocator.h:198
DefaultMemoryManager & operator=(DefaultMemoryManager &&) noexcept=default
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.
Definition: token_bucket.h:15
Rebind allocator.
Definition: allocator.h:139