11 template <
class TAuxMemoryManager>
15 _auxiliary(auxiliary),
26 template <
class TAuxMemoryManager>
30 _auxiliary(auxiliary),
41 template <
class TAuxMemoryManager>
44 assert((size > 0) &&
"Allocated block size must be greater than zero!");
50 uint8_t* buffer = _buffer + _size;
52 size_t aligned_size = size + (aligned - buffer);
55 if (aligned_size <= (_capacity - _size))
58 _size += aligned_size;
68 void* result = _auxiliary.malloc(size, alignment);
69 if (result !=
nullptr)
82 if (_current !=
nullptr)
85 uint8_t* buffer = _current->buffer + _current->size;
87 size_t aligned_size = size + (aligned - buffer);
90 if (aligned_size <= (_current->capacity - _current->size))
93 _current->size += aligned_size;
104 size_t next_reserved = 2 * _reserved;
105 while (next_reserved < size)
109 Page* current = AllocateArena(next_reserved, _current);
110 if (current !=
nullptr)
116 _reserved = next_reserved;
119 uint8_t* buffer = _current->buffer + _current->size;
121 size_t aligned_size = size + (aligned - buffer);
124 _current->size += aligned_size;
138 template <
class TAuxMemoryManager>
141 assert((ptr !=
nullptr) &&
"Deallocated block must be valid!");
146 if ((ptr < _buffer) || (ptr >= (_buffer + _size)))
147 _auxiliary.free(ptr, size);
152 if (_current ==
nullptr)
153 _auxiliary.free(ptr, size);
161 template <
class TAuxMemoryManager>
164 assert((_allocated == 0) &&
"Memory leak detected! Allocated memory size must be zero!");
165 assert((_allocations == 0) &&
"Memory leak detected! Count of active memory allocations must be zero!");
174 template <
class TAuxMemoryManager>
177 assert((capacity > 0) &&
"Arena capacity must be greater than zero!");
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!");
186 Page* current = AllocateArena(capacity, _current);
187 if (current !=
nullptr)
191 _reserved = capacity;
194 template <
class TAuxMemoryManager>
197 assert((buffer !=
nullptr) &&
"Arena buffer must be valid!");
198 assert((capacity > 0) &&
"Arena buffer capacity must be greater than zero!");
200 assert((_allocated == 0) &&
"Memory leak detected! Allocated memory size must be zero!");
201 assert((_allocations == 0) &&
"Memory leak detected! Count of active memory allocations must be zero!");
208 _buffer = (uint8_t*)buffer;
209 _capacity = capacity;
213 _reserved = capacity;
216 template <
class TAuxMemoryManager>
219 assert((_allocated == 0) &&
"Memory leak detected! Allocated memory size must be zero!");
220 assert((_allocations == 0) &&
"Memory leak detected! Count of active memory allocations must be zero!");
232 template <
class TAuxMemoryManager>
236 uint8_t* buffer = (uint8_t*)_auxiliary.
malloc(
sizeof(Page) + capacity +
alignof(std::max_align_t));
237 Page* page = (Page*)buffer;
241 page->buffer = buffer +
sizeof(Page);
242 page->capacity = capacity;
252 template <
class TAuxMemoryManager>
253 inline void ArenaMemoryManager<TAuxMemoryManager>::ClearArena()
258 while (_current !=
nullptr)
260 Page* prev = _current->prev;
261 _auxiliary.free(_current,
sizeof(Page) + _current->capacity +
alignof(std::max_align_t));
Arena memory manager class.
void clear()
Clear arena memory allocator.
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.
size_t capacity() const noexcept
Arena capacity.
void free(void *ptr, size_t size)
Free the previously allocated memory block.
ArenaMemoryManager(TAuxMemoryManager &auxiliary)
Initialize arena memory manager with an auxiliary memory manager.
const uint8_t * buffer() const noexcept
Arena buffer.
static bool IsValidAlignment(size_t alignment) noexcept
Is the given alignment valid?
static T * Align(const T *address, size_t alignment=alignof(T), bool upwards=true) noexcept
Align pointer (upwards or downwards)
C++ Common project definitions.