11 #pragma warning(disable: 4127)
16 template <
typename T,
typename U,
class TMemoryManager,
bool nothrow>
22 template <
typename T,
typename U,
class TMemoryManager,
bool nothrow>
28 template <
typename T,
class TMemoryManager,
bool nothrow>
31 pointer result = (
pointer)_manager.malloc(num *
sizeof(T),
alignof(T));
32 if (result !=
nullptr)
39 throw std::bad_alloc();
42 template <
typename T,
class TMemoryManager,
bool nothrow>
45 _manager.free(ptr, num *
sizeof(T));
48 template <
typename T,
class TMemoryManager,
bool nothrow>
49 template <
typename U,
class... Args>
52 assert((ptr !=
nullptr) &&
"Constructed element must be valid!");
56 new ((
void*)ptr) U(std::forward<Args>(args)...);
59 template <
typename T,
class TMemoryManager,
bool nothrow>
63 assert((ptr !=
nullptr) &&
"Destroyed element must be valid!");
70 template <
typename T,
class TMemoryManager,
bool nothrow>
71 template <
class... Args>
75 void* ptr = _manager.malloc(
sizeof(T));
79 new (ptr) T(std::forward<Args>(args)...);
84 template <
typename T,
class TMemoryManager,
bool nothrow>
87 assert((ptr !=
nullptr) &&
"Released element must be valid!");
96 _manager.free(ptr,
sizeof(T));
100 template <
typename T,
class TMemoryManager,
bool nothrow>
101 template <
class... Args>
104 assert((length > 0) &&
"Array length must be greater than zero!");
106 size_t header =
sizeof(size_t) /
sizeof(T);
107 if ((
sizeof(
size_t) %
sizeof(T)) > 0)
111 T* ptr = ((T*)_manager.malloc(
sizeof(T) * (length + header),
alignof(T))) + header;
114 *(((
size_t*)ptr) - 1) = length;
117 for (
size_t i = 0; i < length; ++i)
118 new (&ptr[i]) T(std::forward<Args>(args)...);
123 template <
typename T,
class TMemoryManager,
bool nothrow>
126 assert((ptr !=
nullptr) &&
"Released array must be valid!");
132 size_t length = *(((
size_t*)ptr) - 1);
135 for(
size_t i = 0; i < length; ++i)
139 size_t header =
sizeof(size_t) /
sizeof(T);
140 if ((
sizeof(
size_t) %
sizeof(T)) > 0)
144 _manager.free(ptr - header,
sizeof(T) * (length + header));
150 assert((size > 0) &&
"Allocated block size must be greater than zero!");
153 void* result = std::malloc(size);
154 if (result !=
nullptr)
165 assert((ptr !=
nullptr) &&
"Deallocated block must be valid!");
179 assert((_allocated == 0) &&
"Memory leak detected! Allocated memory size must be zero!");
180 assert((_allocations == 0) &&
"Memory leak detected! Count of active memory allocations must be zero!");
185 #if defined(_MSC_VER)
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.
void Release(T *ptr)
Release a single element object.
T * CreateArray(size_t length, Args &&... args)
Create an array of element objects.
void deallocate(pointer ptr, size_type num)
Release a block of storage previously allocated.
T * Create(Args &&... args)
Create a single element object.
T * pointer
Pointer to element.
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.
void free(void *ptr, size_t size)
Free the previously allocated memory block.
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.
static bool IsValidAlignment(size_t alignment) noexcept
Is the given alignment valid?
C++ Common project definitions.
bool operator==(const uint128_t &value1, const uint128_t &value2) noexcept
bool operator!=(const uint128_t &value1, const uint128_t &value2) noexcept