32#ifndef MOCHI_ALLOCATOR_DEBUG
33#define MOCHI_ALLOCATOR_DEBUG MOCHI_DEBUG
65 void*
allocate(std::size_t sizeInBytes, std::size_t alignment =
alignof(std::max_align_t));
79 deallocate(
void* ptr, std::size_t sizeInBytes, std::size_t alignment =
alignof(std::max_align_t));
91 virtual void*
do_allocate(std::size_t sizeInBytes, std::size_t alignment) = 0;
92 virtual void do_deallocate(
void* ptr, std::size_t sizeInBytes, std::size_t alignment) = 0;
108 void* do_allocate(
size_t sizeInBytes,
size_t alignment)
override;
109 void do_deallocate(
void* ptr,
size_t sizeInBytes,
size_t alignment)
override;
110 bool do_is_equal(
Allocator const& other)
const noexcept override;
137template <
class T,
class A,
class... Args>
138[[nodiscard]]
inline T*
New(A* allocator, Args&&... args) {
139 static_assert(std::is_base_of_v<Allocator, A>,
"Expected an allocator pointer");
140 T* ptr =
static_cast<T*
>(allocator->allocate(
sizeof(T),
alignof(T)));
141 new (ptr) T(std::forward<Args>(args)...);
153template <
class T,
class A>
154inline void Delete(A* allocator, T* ptr) {
155 static_assert(std::is_base_of_v<Allocator, A>,
"Expected an allocator pointer");
157 allocator->deallocate(ptr,
sizeof(T),
alignof(T));
Virtual interface class for memory allocation and deallocation.
virtual void do_deallocate(void *ptr, std::size_t sizeInBytes, std::size_t alignment)=0
void * allocate(std::size_t sizeInBytes, std::size_t alignment=alignof(std::max_align_t))
Allocate memory of the specified size and alignment.
void deallocate(void *ptr, std::size_t sizeInBytes, std::size_t alignment=alignof(std::max_align_t))
Deallocate a block of memory that was previously allocated by this Allocator instance or by a compati...
virtual void * do_allocate(std::size_t sizeInBytes, std::size_t alignment)=0
virtual bool do_is_equal(Allocator const &other) const noexcept=0
bool is_equal(Allocator const &other) const noexcept
Return true if memory allocated by this Allocator instance can be deallocated by the other Allocator ...
virtual ~Allocator()=default
Mochi's default implementation of the Allocator interface.
T * New(A *allocator, Args &&... args)
Create a new object of type T using memory allocated by a polymorphic Allocator.
Allocator * GetDefaultAllocator()
Get an instance of the DefaultAllocator class.
void Delete(A *allocator, T *ptr)
Destroy and deallocate an object that was created via New<T>.