From 5606b4c399404c0b8f745c6702d70f26eff8b371 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 25 Mar 2024 19:32:02 +0100 Subject: Update to Clang 18, C++23, rework allocator --- deimos/core/BUILD | 4 +- deimos/core/allocator.cpp | 12 ++--- deimos/core/allocator.h | 103 ++++++++++++++++++++++++++++++++++--------- deimos/core/api_registry.cpp | 23 ++++++---- deimos/core/base.h | 13 ++++++ deimos/core/hash.h | 74 ++++++++++++++++++------------- deimos/core/id_name.h | 6 +-- 7 files changed, 165 insertions(+), 70 deletions(-) (limited to 'deimos/core') diff --git a/deimos/core/BUILD b/deimos/core/BUILD index 2093642..cb76f90 100644 --- a/deimos/core/BUILD +++ b/deimos/core/BUILD @@ -1,9 +1,11 @@ cc_library( name = "core", hdrs = [ - "base.h", "allocator.h", "api_registry.h", + "base.h", + "hash.h", + "id_name.h", ], srcs = [ "allocator.cpp", diff --git a/deimos/core/allocator.cpp b/deimos/core/allocator.cpp index a525bfd..f7217c8 100644 --- a/deimos/core/allocator.cpp +++ b/deimos/core/allocator.cpp @@ -8,11 +8,9 @@ namespace deimos class SystemAllocatorImpl : public IAllocator { public: - SystemAllocatorImpl() : IAllocator(MemoryScope{0}) {} - void* Reallocate( void* old_ptr, int64 /* old_size */, int64 new_size, - const char* /* file */, int32 /* line */) override + MemoryScope /* scope */, const SourceLocation& /* source_location */) override { if (old_ptr == nullptr) { @@ -37,17 +35,19 @@ public: class AllocatorApiImpl : public AllocatorApi { SystemAllocatorImpl m_system_impl; + Allocator m_system; public: - AllocatorApiImpl() + constexpr AllocatorApiImpl() : + m_system{ &m_system_impl, {0} } { - system = &m_system_impl; + system = &m_system; } }; AllocatorApi* BootstrapAllocatorApi() { - static AllocatorApiImpl g_instance{}; + static constinit AllocatorApiImpl g_instance{}; return &g_instance; } diff --git a/deimos/core/allocator.h b/deimos/core/allocator.h index 4acdb6d..b156620 100644 --- a/deimos/core/allocator.h +++ b/deimos/core/allocator.h @@ -8,13 +8,9 @@ namespace deimos struct MemoryScope { uint32 id; }; -class IAllocator +struct IAllocator { -protected: - const MemoryScope m_scope; - -public: - constexpr explicit IAllocator(MemoryScope scope) : m_scope{scope} {} + IAllocator() = default; deimos_NO_COPY_MOVE(IAllocator); @@ -23,42 +19,109 @@ public: [[nodiscard]] virtual void* Reallocate( void* old_ptr, int64 old_size, int64 new_size, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) = 0; + MemoryScope scope, const SourceLocation& source_location = {}) = 0; +}; + + +class Allocator +{ + IAllocator* m_allocator; + const MemoryScope m_scope; + +public: + constexpr explicit Allocator(IAllocator* allocator, MemoryScope scope) : + m_allocator{allocator}, m_scope{scope} + {} + + deimos_NO_COPY_MOVE(Allocator); + + ~Allocator() = default; [[nodiscard]] constexpr void* Allocate( int64 new_size, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + const SourceLocation& source_location = {}) + { + return m_allocator->Reallocate(nullptr, 0, new_size, m_scope, source_location); + } + + [[nodiscard]] + void* Reallocate( + void* old_ptr, int64 old_size, int64 new_size, + const SourceLocation& source_location = {}) { - return Reallocate(nullptr, 0, new_size, file, line); + return m_allocator->Reallocate(old_ptr, old_size, new_size, m_scope, source_location); } constexpr void Free( void* old_ptr, int64 old_size, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + const SourceLocation& source_location = {}) { - (void)Reallocate(old_ptr, old_size, 0, file, line); + (void)m_allocator->Reallocate(old_ptr, old_size, 0, m_scope, source_location); } template - T* New( - Args&&... args, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + T* NewInner( + const SourceLocation& source_location, + Args&&... args) { - void* ptr = Allocate(sizeof(T), file, line); + void* ptr = Allocate(sizeof(T), source_location); return new(ptr) T(std::forward(args)...); } template - void Delete( - T* t, - const char* file = __builtin_FILE(), int32 line = __builtin_LINE()) + constexpr T* New(const SourceLocation& source_location = {}) + { + return NewInner(source_location); + } + + template + constexpr T* New(A0&& arg0, const SourceLocation& source_location = {}) + { + return NewInner(source_location, std::forward(arg0)); + } + + template + constexpr T* New( + A0&& arg0, A1&& arg1, + const SourceLocation& source_location = {}) + { + return NewInner(source_location, + std::forward(arg0), + std::forward(arg1)); + } + + template + constexpr T* New( + A0&& arg0, A1&& arg1, A2&& arg2, + const SourceLocation& source_location = {}) + { + return NewInner(source_location, + std::forward(arg0), + std::forward(arg1), + std::forward(arg2)); + } + + template + constexpr T* New( + A0&& arg0, A1&& arg1, A2&& arg2, A3&& arg3, + const SourceLocation& source_location = {}) + { + return NewInner(source_location, + std::forward(arg0), + std::forward(arg1), + std::forward(arg2), + std::forward(arg3)); + } + + template + void Delete(T* t, const SourceLocation& source_location = {}) { if constexpr (!kIsTriviallyDestructible) { t->~T(); } - Free(t, sizeof(T), file, line); + Free(t, sizeof(T), source_location); } }; @@ -67,7 +130,7 @@ class AllocatorApi public: static constexpr IdName kApiName{"deimos::AllocatorApi"}; - IAllocator* system{}; + Allocator* system{}; }; } // namespace deimos diff --git a/deimos/core/api_registry.cpp b/deimos/core/api_registry.cpp index aa993e0..a070473 100644 --- a/deimos/core/api_registry.cpp +++ b/deimos/core/api_registry.cpp @@ -1,6 +1,7 @@ #include "deimos/core/api_registry.h" #include "deimos/core/allocator.h" -#include "deimos/core/hash.h" + +static deimos::AllocatorApi* g_allocator_api; namespace deimos { @@ -13,32 +14,36 @@ struct ApiEntry IdName name; void* impl; - ApiEntry(const IdName& name, void* impl) : - name{name}, impl{impl} + ApiEntry(const IdName& name_, void* impl_) : + name{name_}, impl{impl_} {} }; class ApiRegistryImpl: public ApiRegistry { - IAllocator* m_allocator; + Allocator* m_allocator; public: - explicit ApiRegistryImpl(IAllocator* allocator) : + explicit ApiRegistryImpl(Allocator* allocator) : m_allocator{allocator} {} void Set(const IdName& name, void* impl) final { + (void)name; + (void)impl; + (void)m_allocator; } }; ApiRegistry* InitializeGlobalApiRegistry() { - AllocatorApi* allocator_api = BootstrapAllocatorApi(); - IAllocator* allocator = allocator_api->system; - ApiRegistry* api_registry = allocator->New(); + g_allocator_api = BootstrapAllocatorApi(); + + Allocator* allocator = g_allocator_api->system; + ApiRegistry* api_registry = allocator->New(allocator); - api_registry->Set(allocator_api); + api_registry->Set(g_allocator_api); return api_registry; } diff --git a/deimos/core/base.h b/deimos/core/base.h index 0530db7..ea14610 100644 --- a/deimos/core/base.h +++ b/deimos/core/base.h @@ -48,6 +48,19 @@ struct uint128 uint64 low; }; +struct SourceLocation +{ + const char* file; + int32 line; + + constexpr SourceLocation( // NOLINT + const char* file_ = __builtin_FILE(), + int32 line_ = __builtin_LINE()) : + file{file_}, + line{line_} + {} +}; + template struct RemoveReferenceT { using Type = T; }; template struct RemoveReferenceT { using Type = T; }; template struct RemoveReferenceT { using Type = T; }; diff --git a/deimos/core/hash.h b/deimos/core/hash.h index 3f8935c..f1e8958 100644 --- a/deimos/core/hash.h +++ b/deimos/core/hash.h @@ -9,15 +9,25 @@ constexpr uint64 MurmurHash3_GetBlock64(const char* key, uint64 block) { // NOLINTBEGIN key += block * 8; - return - (uint64)((uint8)key[0]) - | ((uint64)((uint8)key[1]) << 8) - | ((uint64)((uint8)key[2]) << 16) - | ((uint64)((uint8)key[3]) << 24) - | ((uint64)((uint8)key[4]) << 32) - | ((uint64)((uint8)key[5]) << 40) - | ((uint64)((uint8)key[6]) << 48) - | ((uint64)((uint8)key[7]) << 56); + + if consteval + { + return + (uint64)((uint8)key[0]) + | ((uint64)((uint8)key[1]) << 8) + | ((uint64)((uint8)key[2]) << 16) + | ((uint64)((uint8)key[3]) << 24) + | ((uint64)((uint8)key[4]) << 32) + | ((uint64)((uint8)key[5]) << 40) + | ((uint64)((uint8)key[6]) << 48) + | ((uint64)((uint8)key[7]) << 56); + } + else + { + uint64 value{}; + __builtin_memcpy(&value, key, 8); + return value; + } // NOLINTEND } @@ -33,6 +43,8 @@ constexpr uint64 MurmurHash3_Fmix64(uint64 k) constexpr uint128 MurmurHash3_x64_128(const char* key) { + if consteval { return { 12, 12 }; } + // NOLINTBEGIN const uint64 len = __builtin_strlen(key); const uint64 nblocks = len / 16; @@ -49,13 +61,13 @@ constexpr uint128 MurmurHash3_x64_128(const char* key) for(uint64 i = 0; i < nblocks; i++) { - uint64 k1 = MurmurHash3_GetBlock64(key, i * 2 + 0); - uint64 k2 = MurmurHash3_GetBlock64(key, i * 2 + 1); + uint64 k1 = MurmurHash3_GetBlock64(key, i * 2 + 0); + uint64 k2 = MurmurHash3_GetBlock64(key, i * 2 + 1); - k1 *= c1; k1 = _rotl64(k1, 31); k1 *= c2; h1 ^= k1; - h1 = _rotl64(h1, 27); h1 += h2; h1 = h1*5+0x52dce729; - k2 *= c2; k2 = _rotl64(k2, 33); k2 *= c1; h2 ^= k2; - h2 = _rotl64(h2, 31); h2 += h1; h2 = h2*5+0x38495ab5; + k1 *= c1; k1 = _rotl64(k1, 31); k1 *= c2; h1 ^= k1; + h1 = _rotl64(h1, 27); h1 += h2; h1 = h1*5+0x52dce729; + k2 *= c2; k2 = _rotl64(k2, 33); k2 *= c1; h2 ^= k2; + h2 = _rotl64(h2, 31); h2 += h1; h2 = h2*5+0x38495ab5; } //---------- @@ -68,24 +80,24 @@ constexpr uint128 MurmurHash3_x64_128(const char* key) switch (len & 15ULL) { - case 15: k2 ^= ((uint64)(uint8)tail[14]) << 48U; - case 14: k2 ^= ((uint64)(uint8)tail[13]) << 40U; - case 13: k2 ^= ((uint64)(uint8)tail[12]) << 32U; - case 12: k2 ^= ((uint64)(uint8)tail[11]) << 24U; - case 11: k2 ^= ((uint64)(uint8)tail[10]) << 16U; - case 10: k2 ^= ((uint64)(uint8)tail[ 9]) << 8U; + case 15: k2 ^= ((uint64)(uint8)tail[14]) << 48U; [[fallthrough]]; + case 14: k2 ^= ((uint64)(uint8)tail[13]) << 40U; [[fallthrough]]; + case 13: k2 ^= ((uint64)(uint8)tail[12]) << 32U; [[fallthrough]]; + case 12: k2 ^= ((uint64)(uint8)tail[11]) << 24U; [[fallthrough]]; + case 11: k2 ^= ((uint64)(uint8)tail[10]) << 16U; [[fallthrough]]; + case 10: k2 ^= ((uint64)(uint8)tail[ 9]) << 8U; [[fallthrough]]; case 9: k2 ^= ((uint64)(uint8)tail[ 8]) << 0U; - k2 *= c2; k2 = _rotl64(k2,33); k2 *= c1; h2 ^= k2; - - case 8: k1 ^= ((uint64)(uint8)tail[ 7]) << 56U; - case 7: k1 ^= ((uint64)(uint8)tail[ 6]) << 48U; - case 6: k1 ^= ((uint64)(uint8)tail[ 5]) << 40U; - case 5: k1 ^= ((uint64)(uint8)tail[ 4]) << 32U; - case 4: k1 ^= ((uint64)(uint8)tail[ 3]) << 24U; - case 3: k1 ^= ((uint64)(uint8)tail[ 2]) << 16U; - case 2: k1 ^= ((uint64)(uint8)tail[ 1]) << 8U; + k2 *= c2; k2 = _rotl64(k2,33); k2 *= c1; h2 ^= k2; + [[fallthrough]]; + case 8: k1 ^= ((uint64)(uint8)tail[ 7]) << 56U; [[fallthrough]]; + case 7: k1 ^= ((uint64)(uint8)tail[ 6]) << 48U; [[fallthrough]]; + case 6: k1 ^= ((uint64)(uint8)tail[ 5]) << 40U; [[fallthrough]]; + case 5: k1 ^= ((uint64)(uint8)tail[ 4]) << 32U; [[fallthrough]]; + case 4: k1 ^= ((uint64)(uint8)tail[ 3]) << 24U; [[fallthrough]]; + case 3: k1 ^= ((uint64)(uint8)tail[ 2]) << 16U; [[fallthrough]]; + case 2: k1 ^= ((uint64)(uint8)tail[ 1]) << 8U; [[fallthrough]]; case 1: k1 ^= ((uint64)(uint8)tail[ 0]) << 0U; - k1 *= c1; k1 = _rotl64(k1,31); k1 *= c2; h1 ^= k1; + k1 *= c1; k1 = _rotl64(k1,31); k1 *= c2; h1 ^= k1; }; //---------- diff --git a/deimos/core/id_name.h b/deimos/core/id_name.h index eb961ca..0fb793b 100644 --- a/deimos/core/id_name.h +++ b/deimos/core/id_name.h @@ -11,9 +11,9 @@ struct IdName uint128 hash; const char* name; - explicit constexpr IdName(const char* name) : - hash{MurmurHash3_x64_128(name)}, - name{name} + explicit constexpr IdName(const char* name_) : + hash{MurmurHash3_x64_128(name_)}, + name{name_} {} }; -- cgit