diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-04-05 00:00:33 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-04-05 00:00:33 +0200 |
commit | f46b019cb0a2f451234fdb4f20620b7e443da136 (patch) | |
tree | 29a3b0f1441cef9932e0770b155294be55d11e9e /deimos | |
parent | 8970c40ce9a9a4e5f582b48f69b77bd90d8e678e (diff) |
Add gsl::owner
Diffstat (limited to 'deimos')
-rw-r--r-- | deimos/core/allocator.cpp | 12 | ||||
-rw-r--r-- | deimos/core/allocator.h | 36 | ||||
-rw-r--r-- | deimos/core/api_registry.cpp | 10 | ||||
-rw-r--r-- | deimos/core/base.h | 1 | ||||
-rw-r--r-- | deimos/core/format.cpp | 2 | ||||
-rw-r--r-- | deimos/core/gsl.h | 2 | ||||
-rw-r--r-- | deimos/core/io.cpp | 2 | ||||
-rw-r--r-- | deimos/core/log.cpp | 10 | ||||
-rw-r--r-- | deimos/core/log.h | 2 | ||||
-rw-r--r-- | deimos/core/os.h | 2 |
10 files changed, 42 insertions, 37 deletions
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<void*> Reallocate(
+ gsl::owner<void*> 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<Allocator*> CreateChild(Allocator* parent, gsl::czstring /* description */) override
{
- return parent;
+ return parent->New<Allocator>(parent->allocator(), MemoryScope{});
}
- void DestroyChild(Allocator*) override
+ void DestroyChild(gsl::owner<Allocator*> allocator) override
{
+ deimos_StaticAssert(std::is_trivially_destructible_v<Allocator>);
+ 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<void*> Reallocate(
+ gsl::owner<void*> old_ptr, int64_t old_size, int64_t new_size,
MemoryScope scope, const SourceLocation& source_location = {}) = 0;
};
class Allocator
{
- IAllocator* m_allocator;
+ gsl::owner<IAllocator*> 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<void*> 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<void*> Reallocate(
+ gsl::owner<void*> 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<void*> old_ptr, int64_t old_size,
const SourceLocation& source_location = {})
{
(void)m_allocator->Reallocate(old_ptr, old_size, 0, m_scope, source_location);
}
template<typename T, typename... Args>
- T* NewInner(
+ gsl::owner<T*> NewInner(
const SourceLocation& source_location,
Args&&... args)
{
- void* ptr = Allocate(sizeof(T), source_location);
+ gsl::owner<void*> ptr = Allocate(sizeof(T), source_location);
return new(ptr) T(std::forward<Args>(args)...);
}
template<typename T>
- constexpr T* New(const SourceLocation& source_location = {})
+ constexpr gsl::owner<T*> New(const SourceLocation& source_location = {})
{
return NewInner<T>(source_location);
}
template<typename T, typename A0>
- constexpr T* New(A0&& arg0, const SourceLocation& source_location = {})
+ constexpr gsl::owner<T*> New(A0&& arg0, const SourceLocation& source_location = {})
{
return NewInner<T>(source_location, std::forward<A0>(arg0));
}
template<typename T, typename A0, typename A1>
- constexpr T* New(
+ constexpr gsl::owner<T*> New(
A0&& arg0, A1&& arg1,
const SourceLocation& source_location = {})
{
@@ -92,7 +94,7 @@ public: }
template<typename T, typename A0, typename A1, typename A2>
- constexpr T* New(
+ constexpr gsl::owner<T*> New(
A0&& arg0, A1&& arg1, A2&& arg2,
const SourceLocation& source_location = {})
{
@@ -103,7 +105,7 @@ public: }
template<typename T, typename A0, typename A1, typename A2, typename A3>
- constexpr T* New(
+ constexpr gsl::owner<T*> New(
A0&& arg0, A1&& arg1, A2&& arg2, A3&& arg3,
const SourceLocation& source_location = {})
{
@@ -115,7 +117,7 @@ public: }
template<typename T>
- void Delete(T* t, const SourceLocation& source_location = {})
+ void Delete(gsl::owner<T*> t, const SourceLocation& source_location = {})
{
if constexpr (!std::is_trivially_destructible_v<T>)
{
@@ -138,8 +140,8 @@ public: Allocator* system{};
- virtual Allocator* CreateChild(Allocator* parent, gsl::czstring description) = 0;
- virtual void DestroyChild(Allocator*) = 0;
+ virtual gsl::owner<Allocator*> CreateChild(Allocator* parent, gsl::czstring description) = 0;
+ virtual void DestroyChild(gsl::owner<Allocator*>) = 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<const ApiEntry*> next{};
IdName name;
void* impl;
@@ -24,7 +24,7 @@ struct ApiEntry class ApiRegistryImpl: public ApiRegistry
{
Allocator* m_allocator;
- const ApiEntry* m_head{};
+ gsl::owner<const ApiEntry*> 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<ApiEntry>(name, impl);
+ gsl::owner<ApiEntry*> entry = m_allocator->New<ApiEntry>(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<ApiRegistryImpl>(allocator);
+ gsl::owner<Allocator*> allocator = g_allocator_api->CreateChild(g_allocator_api->system, "API Registry");
+ gsl::owner<ApiRegistry*> api_registry = allocator->New<ApiRegistryImpl>(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<typename T>
inline Span<const std::byte> AsBytes(Span<T> span)
{
- // NOLINTNEXTLINE
return { reinterpret_cast<const std::byte*>(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<typename T> 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<const std::byte> 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<ILogger*> m_default_logger;
public:
- explicit LogApiImpl(ILogger* default_logger) :
+ explicit LogApiImpl(gsl::owner<ILogger*> 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<const char*>(written.data()), written.size()});
}
};
@@ -72,8 +72,8 @@ void RegisterLogApi(ApiRegistry* api_registry) auto* allocator = api_registry->Get<AllocatorApi>()->system;
auto* os_console_api = api_registry->Get<OsApi>()->console;
- auto* default_logger = allocator->New<DefaultLogger>(os_console_api);
- auto* log_api = allocator->New<LogApiImpl>(default_logger);
+ gsl::owner<ILogger*> default_logger = allocator->New<DefaultLogger>(os_console_api);
+ gsl::owner<LogApi*> log_api = allocator->New<LogApiImpl>(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<OsDll*> Open(gsl::czstring) = 0;
virtual void* GetSymbol(OsDll*, gsl::czstring) = 0;
};
|