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 --- .clang-tidy | 1 + deimos/core/allocator.cpp | 12 +++++++----- deimos/core/allocator.h | 36 +++++++++++++++++++----------------- deimos/core/api_registry.cpp | 10 +++++----- deimos/core/base.h | 1 - deimos/core/format.cpp | 2 +- deimos/core/gsl.h | 2 ++ deimos/core/io.cpp | 2 +- deimos/core/log.cpp | 10 +++++----- deimos/core/log.h | 2 +- deimos/core/os.h | 2 +- 11 files changed, 43 insertions(+), 37 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 4322089..84322ba 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -20,3 +20,4 @@ Checks: - "-*-no-malloc" - "-*-avoid-c-arrays" - "-*-use-anonymous-namespace" + - "-*-reinterpret-cast" diff --git a/deimos/core/allocator.cpp b/deimos/core/allocator.cpp index 1a823c1..b42861c 100644 --- a/deimos/core/allocator.cpp +++ b/deimos/core/allocator.cpp @@ -8,8 +8,8 @@ namespace deimos class SystemAllocatorImpl : public IAllocator { public: - 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, MemoryScope /* scope */, const SourceLocation& /* source_location */) override { if (old_ptr == nullptr) @@ -44,13 +44,15 @@ public: system = &m_system; } - Allocator* CreateChild(Allocator* parent, gsl::czstring /* description */) override + gsl::owner CreateChild(Allocator* parent, gsl::czstring /* description */) override { - return parent; + return parent->New(parent->allocator(), MemoryScope{}); } - void DestroyChild(Allocator*) override + void DestroyChild(gsl::owner allocator) override { + deimos_StaticAssert(std::is_trivially_destructible_v); + allocator->Delete(allocator); } }; 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 diff --git a/deimos/core/api_registry.cpp b/deimos/core/api_registry.cpp index 24ea201..81dabf0 100644 --- a/deimos/core/api_registry.cpp +++ b/deimos/core/api_registry.cpp @@ -12,7 +12,7 @@ void RegisterLogApi(ApiRegistry*); struct ApiEntry { - const ApiEntry* next{}; + gsl::owner next{}; IdName name; void* impl; @@ -24,7 +24,7 @@ struct ApiEntry class ApiRegistryImpl: public ApiRegistry { Allocator* m_allocator; - const ApiEntry* m_head{}; + gsl::owner m_head{}; public: explicit ApiRegistryImpl(Allocator* allocator) : @@ -33,7 +33,7 @@ public: void Set(const IdName& name, void* impl) final { - auto* entry = m_allocator->New(name, impl); + gsl::owner entry = m_allocator->New(name, impl); entry->next = std::exchange(m_head, entry); } @@ -51,8 +51,8 @@ ApiRegistry* InitializeGlobalApiRegistry() { g_allocator_api = BootstrapAllocatorApi(); - Allocator* allocator = g_allocator_api->CreateChild(g_allocator_api->system, "API Registry"); - ApiRegistry* api_registry = allocator->New(allocator); + gsl::owner allocator = g_allocator_api->CreateChild(g_allocator_api->system, "API Registry"); + gsl::owner api_registry = allocator->New(allocator); api_registry->Set(g_allocator_api); diff --git a/deimos/core/base.h b/deimos/core/base.h index 4bcb7c7..64d1ed9 100644 --- a/deimos/core/base.h +++ b/deimos/core/base.h @@ -101,7 +101,6 @@ public: template inline Span AsBytes(Span span) { - // NOLINTNEXTLINE return { reinterpret_cast(span.data()), span.size() * (int64_t)sizeof(T) }; } diff --git a/deimos/core/format.cpp b/deimos/core/format.cpp index f4f4e83..b9a0ca3 100644 --- a/deimos/core/format.cpp +++ b/deimos/core/format.cpp @@ -9,7 +9,7 @@ namespace deimos class FormatContext { - IWriter* m_writer; + IWriter* m_writer; public: explicit FormatContext(IWriter* writer) : m_writer{writer} {} diff --git a/deimos/core/gsl.h b/deimos/core/gsl.h index 2000f6a..b54fc65 100644 --- a/deimos/core/gsl.h +++ b/deimos/core/gsl.h @@ -6,6 +6,8 @@ namespace gsl using zstring = char*; using czstring = const char*; +template using owner = T; + } // namespace gsl #define Expects(EXPR) do { if (!(EXPR)) { __builtin_trap(); } } while (0) diff --git a/deimos/core/io.cpp b/deimos/core/io.cpp index 7764ee2..2a47a0d 100644 --- a/deimos/core/io.cpp +++ b/deimos/core/io.cpp @@ -7,7 +7,7 @@ void BufferWriter::Write(Span to_write) { Expects(m_written <= m_size); - int64_t n_to_write = Min(to_write.size(), m_size - m_written); + const int64_t n_to_write = Min(to_write.size(), m_size - m_written); MemoryCopy(m_buffer + m_written, to_write.data(), n_to_write); // NOLINT m_written += n_to_write; diff --git a/deimos/core/log.cpp b/deimos/core/log.cpp index befe4b7..3ba4fbc 100644 --- a/deimos/core/log.cpp +++ b/deimos/core/log.cpp @@ -45,10 +45,10 @@ public: class LogApiImpl : public LogApi { - ILogger* m_default_logger; + gsl::owner m_default_logger; public: - explicit LogApiImpl(ILogger* default_logger) : + explicit LogApiImpl(gsl::owner default_logger) : m_default_logger{default_logger} {} @@ -63,7 +63,7 @@ public: FormatVa(&message_writer, msg, args); auto written = message_writer.GetWritten(); - m_default_logger->Log(severity, location, {(const char*)written.data(), written.size()}); + m_default_logger->Log(severity, location, {reinterpret_cast(written.data()), written.size()}); } }; @@ -72,8 +72,8 @@ void RegisterLogApi(ApiRegistry* api_registry) auto* allocator = api_registry->Get()->system; auto* os_console_api = api_registry->Get()->console; - auto* default_logger = allocator->New(os_console_api); - auto* log_api = allocator->New(default_logger); + gsl::owner default_logger = allocator->New(os_console_api); + gsl::owner log_api = allocator->New(default_logger); api_registry->Set(log_api); } diff --git a/deimos/core/log.h b/deimos/core/log.h index cf92391..323d246 100644 --- a/deimos/core/log.h +++ b/deimos/core/log.h @@ -7,7 +7,7 @@ namespace deimos { -enum class LogSeverity +enum class LogSeverity : uint8_t { kInfo, kDebug, diff --git a/deimos/core/os.h b/deimos/core/os.h index 3cff6ba..fa1762a 100644 --- a/deimos/core/os.h +++ b/deimos/core/os.h @@ -32,7 +32,7 @@ public: deimos_NO_COPY_MOVE(OsDllApi); virtual ~OsDllApi() = default; - virtual OsDll* Open(gsl::czstring) = 0; + virtual gsl::owner Open(gsl::czstring) = 0; virtual void* GetSymbol(OsDll*, gsl::czstring) = 0; }; -- cgit