From f46b019cb0a2f451234fdb4f20620b7e443da136 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Fri, 5 Apr 2024 00:00:33 +0200 Subject: Add gsl::owner --- deimos/core/allocator.h | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'deimos/core/allocator.h') diff --git a/deimos/core/allocator.h b/deimos/core/allocator.h index 677501d..e65f316 100644 --- a/deimos/core/allocator.h +++ b/deimos/core/allocator.h @@ -17,15 +17,15 @@ struct IAllocator virtual ~IAllocator() = default; [[nodiscard]] - virtual void* Reallocate( - void* old_ptr, int64_t old_size, int64_t new_size, + virtual gsl::owner Reallocate( + gsl::owner old_ptr, int64_t old_size, int64_t new_size, MemoryScope scope, const SourceLocation& source_location = {}) = 0; }; class Allocator { - IAllocator* m_allocator; + gsl::owner m_allocator; const MemoryScope m_scope; public: @@ -37,8 +37,10 @@ public: ~Allocator() = default; + constexpr IAllocator* allocator() const { return m_allocator; } + [[nodiscard]] - constexpr void* Allocate( + constexpr gsl::owner Allocate( int64_t new_size, const SourceLocation& source_location = {}) { @@ -46,43 +48,43 @@ public: } [[nodiscard]] - void* Reallocate( - void* old_ptr, int64_t old_size, int64_t new_size, + gsl::owner Reallocate( + gsl::owner old_ptr, int64_t old_size, int64_t new_size, const SourceLocation& source_location = {}) { return m_allocator->Reallocate(old_ptr, old_size, new_size, m_scope, source_location); } constexpr void Free( - void* old_ptr, int64_t old_size, + gsl::owner old_ptr, int64_t old_size, const SourceLocation& source_location = {}) { (void)m_allocator->Reallocate(old_ptr, old_size, 0, m_scope, source_location); } template - T* NewInner( + gsl::owner NewInner( const SourceLocation& source_location, Args&&... args) { - void* ptr = Allocate(sizeof(T), source_location); + gsl::owner ptr = Allocate(sizeof(T), source_location); return new(ptr) T(std::forward(args)...); } template - constexpr T* New(const SourceLocation& source_location = {}) + constexpr gsl::owner New(const SourceLocation& source_location = {}) { return NewInner(source_location); } template - constexpr T* New(A0&& arg0, const SourceLocation& source_location = {}) + constexpr gsl::owner New(A0&& arg0, const SourceLocation& source_location = {}) { return NewInner(source_location, std::forward(arg0)); } template - constexpr T* New( + constexpr gsl::owner New( A0&& arg0, A1&& arg1, const SourceLocation& source_location = {}) { @@ -92,7 +94,7 @@ public: } template - constexpr T* New( + constexpr gsl::owner New( A0&& arg0, A1&& arg1, A2&& arg2, const SourceLocation& source_location = {}) { @@ -103,7 +105,7 @@ public: } template - constexpr T* New( + constexpr gsl::owner New( A0&& arg0, A1&& arg1, A2&& arg2, A3&& arg3, const SourceLocation& source_location = {}) { @@ -115,7 +117,7 @@ public: } template - void Delete(T* t, const SourceLocation& source_location = {}) + void Delete(gsl::owner t, const SourceLocation& source_location = {}) { if constexpr (!std::is_trivially_destructible_v) { @@ -138,8 +140,8 @@ public: Allocator* system{}; - virtual Allocator* CreateChild(Allocator* parent, gsl::czstring description) = 0; - virtual void DestroyChild(Allocator*) = 0; + virtual gsl::owner CreateChild(Allocator* parent, gsl::czstring description) = 0; + virtual void DestroyChild(gsl::owner) = 0; }; } // namespace deimos -- cgit