#pragma once #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