9 #ifndef CPPCOMMON_MEMORY_ALLOCATOR_H
10 #define CPPCOMMON_MEMORY_ALLOCATOR_H
23 template <
typename T,
class TMemoryManager,
bool nothrow = false>
26 template <
typename U,
class UMemoryManager,
bool flag>
49 explicit Allocator(TMemoryManager& manager) noexcept : _manager(manager) {}
58 { _manager = alloc._manager;
return *
this; }
60 { _manager = alloc._manager;
return *
this; }
97 void reset() { _manager.reset(); }
104 template <
typename U,
class... Args>
110 template <
typename U>
117 template <
class... Args>
118 T*
Create(Args&&... args);
130 template <
class... Args>
142 TMemoryManager& _manager;
146 template <
class TMemoryManager,
bool nothrow>
161 Allocator(TMemoryManager& manager) noexcept : _manager(manager) {}
162 template <
typename U>
167 template <typename U>
169 { _manager = alloc._manager;
return *
this; }
176 TMemoryManager _manager;
198 size_t allocated() const noexcept {
return _allocated; }
203 size_t max_size() const noexcept {
return std::numeric_limits<size_t>::max(); }
211 void*
malloc(
size_t size,
size_t alignment =
alignof(std::max_align_t));
217 void free(
void* ptr,
size_t size);
229 template <
typename T,
bool nothrow = false>
Memory allocator inline implementation.
void value_type
Element type.
ptrdiff_t difference_type
Difference between two pointers.
Allocator(const Allocator< U, TMemoryManager, nothrow > &alloc) noexcept
const void * const_pointer
Pointer to constant element.
void * pointer
Pointer to element.
size_t size_type
Quantities of elements.
Allocator(Allocator &&) noexcept=default
Allocator & operator=(Allocator &&) noexcept=default
Allocator(TMemoryManager &manager) noexcept
void construct(U *ptr, Args &&... args)
Constructs an element object on the given location pointer.
void ReleaseArray(T *ptr)
Release an array of element objects.
Allocator & operator=(Allocator &&) noexcept=default
const T & const_reference
Reference to constant element.
void Release(T *ptr)
Release a single element object.
T value_type
Element type.
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.
T * Create(Args &&... args)
Create a single element object.
Allocator & operator=(const Allocator &alloc) noexcept
pointer address(reference x) const noexcept
Get the address of the given reference.
ptrdiff_t difference_type
Difference between two pointers.
Allocator(const Allocator &alloc) noexcept
const T * const_pointer
Pointer to constant element.
T * pointer
Pointer to element.
Allocator(const Allocator< U, TMemoryManager, nothrow > &alloc) noexcept
Allocator(TMemoryManager &manager) noexcept
Initialize allocator with a given memory manager.
void reset()
Reset the allocator.
pointer allocate(size_type num, const void *hint=0)
Allocate a block of storage suitable to contain the given count of elements.
void destroy(U *ptr)
Destroys in-place the object pointed by the given location pointer.
T & reference
Reference to element.
size_t size_type
Quantities of elements.
const_pointer address(const_reference x) const noexcept
Get the constant address of the given constant reference.
size_type max_size() const noexcept
Get the maximum number of elements, that could potentially be allocated by the allocator.
Default memory manager class.
DefaultMemoryManager() noexcept
size_t allocations() const noexcept
Count of active memory allocations.
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=(const DefaultMemoryManager &) noexcept=default
size_t allocated() const noexcept
Allocated memory in bytes.
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.
Memory management definition.
C++ Common project definitions.