From c147cb2a949d2a5c75804613c45e46c1a2ec8ab1 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Sat, 20 Apr 2024 01:22:04 +0200 Subject: Temporary allocator --- deimos/core/temp_allocator.h | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'deimos/core/temp_allocator.h') diff --git a/deimos/core/temp_allocator.h b/deimos/core/temp_allocator.h index 4f80192..6d93a77 100644 --- a/deimos/core/temp_allocator.h +++ b/deimos/core/temp_allocator.h @@ -2,9 +2,74 @@ #include "deimos/core/base.h" #include "deimos/core/id_name.h" +#include "deimos/core/allocator.h" namespace deimos { +struct TempAllocatorTag { uintptr_t tag; }; + +class TempAllocatorApi; + +class ITempAllocator +{ +public: + ITempAllocator() = default; + deimos_NO_COPY_MOVE(ITempAllocator); + virtual ~ITempAllocator() = default; + + virtual gsl::owner Reallocate(TempAllocatorTag tag, + gsl::owner old_ptr, int64_t old_size, int64_t new_size) = 0; + + virtual void Release(TempAllocatorTag tag) = 0; +}; + +class TempAllocator : public IAllocator +{ + ITempAllocator* m_inner; + TempAllocatorTag m_tag; + +public: + TempAllocator(ITempAllocator* inner, TempAllocatorTag tag) : + m_inner{inner}, m_tag{tag} + {} + + deimos_NO_COPY_MOVE(TempAllocator); + + ~TempAllocator() override; + + [[nodiscard]] + gsl::owner Reallocate( + gsl::owner old_ptr, int64_t old_size, int64_t new_size, + MemoryScope scope, const SourceLocation& source_location = {}) override; + + constexpr Allocator allocator() { return Allocator(this, MemoryScope{0}); } +}; + +class TempAllocatorApi +{ +public: + TempAllocatorApi() = default; + deimos_NO_COPY_MOVE(TempAllocatorApi); + virtual ~TempAllocatorApi() = default; + + static constexpr IdName kApiName{"deimos::TempAllocatorApi"}; + + [[nodiscard]] virtual TempAllocator Acquire() = 0; +}; + +[[nodiscard]] +inline gsl::owner TempAllocator::Reallocate( + gsl::owner old_ptr, int64_t old_size, int64_t new_size, + MemoryScope, const SourceLocation&) +{ + return m_inner->Reallocate(m_tag, old_ptr, old_size, new_size); +} + +inline TempAllocator::~TempAllocator() +{ + m_inner->Release(m_tag); +} + } // namespace deimos -- cgit