From a141c401f78467bc15f62882fca5d55a007cacbb Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 17 Feb 2025 00:21:48 +0100 Subject: Reorganize everything --- asl/memory/allocator.hpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 asl/memory/allocator.hpp (limited to 'asl/memory/allocator.hpp') diff --git a/asl/memory/allocator.hpp b/asl/memory/allocator.hpp new file mode 100644 index 0000000..d39fa23 --- /dev/null +++ b/asl/memory/allocator.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include "asl/base/meta.hpp" +#include "asl/memory/layout.hpp" +#include "asl/memory/memory.hpp" + +namespace asl +{ + +template +concept allocator = moveable && equality_comparable && + requires(T& alloc, layout layout, void* ptr) + { + { alloc.alloc(layout) } -> same_as; + { alloc.realloc(ptr, layout, layout) } -> same_as; + alloc.dealloc(ptr, layout); + }; + +class GlobalHeap +{ +public: + static void* alloc(const layout&); + static void* realloc(void* ptr, const layout& old, const layout& new_layout); + static void dealloc(void* ptr, const layout&); + + constexpr bool operator==(const GlobalHeap&) const { return true; } +}; +static_assert(allocator); + +using DefaultAllocator = GlobalHeap; + +template +T* alloc_new(allocator auto& a, auto&&... args) +{ + void* ptr = a.alloc(layout::of()); + return construct_at(ptr, ASL_FWD(args)...); +} + +template +void alloc_delete(allocator auto& a, T* ptr) +{ + destroy(ptr); + a.dealloc(ptr, layout::of()); +} + +template +constexpr T* alloc_new_default(auto&&... args) +{ + return alloc_new(DefaultAllocator{}, ASL_FWD(args)...); +} + +template +void alloc_delete_default(T* ptr) +{ + alloc_delete(DefaultAllocator{}, ptr); +} + +} // namespace asl -- cgit