35#include "core_api.hpp"
41 HeapAllocator(
const HeapAllocator&) =
delete;
42 HeapAllocator& operator=(
const HeapAllocator&) =
delete;
44 HeapAllocator() =
default;
45 ~HeapAllocator() =
default;
47 [[nodiscard]]
inline size_t get_allocated_size()
const {
return _allocated_size; }
48 [[nodiscard]]
inline size_t get_deallocated_size()
const {
return _deallocated_size; }
49 [[nodiscard]]
inline size_t get_total_size()
const {
return _allocated_size - _deallocated_size; }
51 static HeapAllocator* get_instance();
54 T* allocate(size_t count = 1, size_t alignment = 1) {
55 size_t desired_size = count *
sizeof(T);
56 _allocated_size += desired_size;
62 ptr = _aligned_malloc(desired_size, alignment);
64 posix_memalign(&ptr, alignment, desired_size);
68 obj =
reinterpret_cast<T*>(ptr);
69 for (uint32_t i = 0; i < count; ++i) {
70 new (
reinterpret_cast<T*>(ptr) + i) T;
79 void deallocate(T* ptr, size_t count = 1) {
81 size_t desired_size = count *
sizeof(T);
82 _deallocated_size += desired_size;
83 for (uint32_t i = 0; i < count; ++i) {
90 static std::unique_ptr<HeapAllocator> instance;
92 size_t _allocated_size = 0;
93 size_t _deallocated_size = 0;
Definition heap_allocator.hpp:39
#define ensure(condition)
Definition kn_assert.hpp:43