From 5606b4c399404c0b8f745c6702d70f26eff8b371 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 25 Mar 2024 19:32:02 +0100 Subject: Update to Clang 18, C++23, rework allocator --- deimos/core/allocator.h | 103 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 20 deletions(-) (limited to 'deimos/core/allocator.h') diff --git a/deimos/core/allocator.h b/deimos/core/allocator.h index 4acdb6d..b156620 100644 --- a/deimos/core/allocator.h +++ b/deimos/core/allocator.h @@ -8,13 +8,9 @@ namespace deimos struct MemoryScope { uint32 id; }; -class IAllocator +struct IAllocator { -protected: - const MemoryScope m_scope; - -public: - constexpr explicit IAllocator(MemoryScope scope) : m_scope{scope} {} + IAllocator() = default; deimos_NO_COPY_MOVE(IAllocator); @@ -23,42 +19,109 @@ public: [[nodiscard]] virtual void* Reallocate( void* old_ptr, int64 old_size, int64 new_size, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) = 0; + MemoryScope scope, const SourceLocation& source_location = {}) = 0; +}; + + +class Allocator +{ + IAllocator* m_allocator; + const MemoryScope m_scope; + +public: + constexpr explicit Allocator(IAllocator* allocator, MemoryScope scope) : + m_allocator{allocator}, m_scope{scope} + {} + + deimos_NO_COPY_MOVE(Allocator); + + ~Allocator() = default; [[nodiscard]] constexpr void* Allocate( int64 new_size, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + const SourceLocation& source_location = {}) + { + return m_allocator->Reallocate(nullptr, 0, new_size, m_scope, source_location); + } + + [[nodiscard]] + void* Reallocate( + void* old_ptr, int64 old_size, int64 new_size, + const SourceLocation& source_location = {}) { - return Reallocate(nullptr, 0, new_size, file, line); + return m_allocator->Reallocate(old_ptr, old_size, new_size, m_scope, source_location); } constexpr void Free( void* old_ptr, int64 old_size, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + const SourceLocation& source_location = {}) { - (void)Reallocate(old_ptr, old_size, 0, file, line); + (void)m_allocator->Reallocate(old_ptr, old_size, 0, m_scope, source_location); } template - T* New( - Args&&... args, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + T* NewInner( + const SourceLocation& source_location, + Args&&... args) { - void* ptr = Allocate(sizeof(T), file, line); + void* ptr = Allocate(sizeof(T), source_location); return new(ptr) T(std::forward(args)...); } template - void Delete( - T* t, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + constexpr T* New(const SourceLocation& source_location = {}) + { + return NewInner(source_location); + } + + template + constexpr T* New(A0&& arg0, const SourceLocation& source_location = {}) + { + return NewInner(source_location, std::forward(arg0)); + } + + template + constexpr T* New( + A0&& arg0, A1&& arg1, + const SourceLocation& source_location = {}) + { + return NewInner(source_location, + std::forward(arg0), + std::forward(arg1)); + } + + template + constexpr T* New( + A0&& arg0, A1&& arg1, A2&& arg2, + const SourceLocation& source_location = {}) + { + return NewInner(source_location, + std::forward(arg0), + std::forward(arg1), + std::forward(arg2)); + } + + template + constexpr T* New( + A0&& arg0, A1&& arg1, A2&& arg2, A3&& arg3, + const SourceLocation& source_location = {}) + { + return NewInner(source_location, + std::forward(arg0), + std::forward(arg1), + std::forward(arg2), + std::forward(arg3)); + } + + template + void Delete(T* t, const SourceLocation& source_location = {}) { if constexpr (!kIsTriviallyDestructible) { t->~T(); } - Free(t, sizeof(T), file, line); + Free(t, sizeof(T), source_location); } }; @@ -67,7 +130,7 @@ class AllocatorApi public: static constexpr IdName kApiName{"deimos::AllocatorApi"}; - IAllocator* system{}; + Allocator* system{}; }; } // namespace deimos -- cgit