From 00c0d78199fcfbbb20828be5e06fd2d271fa4c1e Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sun, 24 Mar 2024 23:49:26 +0100 Subject: Initial commit --- deimos/core/allocator.h | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 deimos/core/allocator.h (limited to 'deimos/core/allocator.h') diff --git a/deimos/core/allocator.h b/deimos/core/allocator.h new file mode 100644 index 0000000..4acdb6d --- /dev/null +++ b/deimos/core/allocator.h @@ -0,0 +1,74 @@ +#pragma once + +#include "deimos/core/base.h" +#include "deimos/core/id_name.h" + +namespace deimos +{ + +struct MemoryScope { uint32 id; }; + +class IAllocator +{ +protected: + const MemoryScope m_scope; + +public: + constexpr explicit IAllocator(MemoryScope scope) : m_scope{scope} {} + + deimos_NO_COPY_MOVE(IAllocator); + + virtual ~IAllocator() = default; + + [[nodiscard]] + virtual void* Reallocate( + void* old_ptr, int64 old_size, int64 new_size, + const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) = 0; + + [[nodiscard]] + constexpr void* Allocate( + int64 new_size, + const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + { + return Reallocate(nullptr, 0, new_size, file, line); + } + + constexpr void Free( + void* old_ptr, int64 old_size, + const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + { + (void)Reallocate(old_ptr, old_size, 0, file, line); + } + + template + T* New( + Args&&... args, + const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + { + void* ptr = Allocate(sizeof(T), file, line); + return new(ptr) T(std::forward(args)...); + } + + template + void Delete( + T* t, + const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + { + if constexpr (!kIsTriviallyDestructible) + { + t->~T(); + } + Free(t, sizeof(T), file, line); + } +}; + +class AllocatorApi +{ +public: + static constexpr IdName kApiName{"deimos::AllocatorApi"}; + + IAllocator* system{}; +}; + +} // namespace deimos + -- cgit