summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.bazelrc3
-rw-r--r--.clang-tidy1
-rw-r--r--compile_flags.txt2
-rw-r--r--deimos/core/BUILD4
-rw-r--r--deimos/core/allocator.cpp12
-rw-r--r--deimos/core/allocator.h103
-rw-r--r--deimos/core/api_registry.cpp23
-rw-r--r--deimos/core/base.h13
-rw-r--r--deimos/core/hash.h74
-rw-r--r--deimos/core/id_name.h6
10 files changed, 169 insertions, 72 deletions
diff --git a/.bazelrc b/.bazelrc
index e385c65..ed87e07 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -1,4 +1,4 @@
-build --copt=/std:c++20
+build --copt=-Xclang=-std=c++23
build --copt=-Wall
build --copt=-Wno-pre-c++17-compat
build --copt=-Wno-c++98-compat
@@ -12,3 +12,4 @@ build --copt=-Wno-c++20-compat
build --copt=-Wno-extra-semi
build --copt=-Wno-unsafe-buffer-usage
build --copt=-Wno-missing-prototypes
+build --copt=-Wno-switch-default
diff --git a/.clang-tidy b/.clang-tidy
index d1f2bf9..4322089 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -19,3 +19,4 @@ Checks:
- "-*-owning-memory"
- "-*-no-malloc"
- "-*-avoid-c-arrays"
+ - "-*-use-anonymous-namespace"
diff --git a/compile_flags.txt b/compile_flags.txt
index c264aa2..1f37a65 100644
--- a/compile_flags.txt
+++ b/compile_flags.txt
@@ -1,5 +1,5 @@
-I.
-I3rd_party/vulkan/include
-xc++
--std=c++20
+-std=c++23
-Wall
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<typename T, typename... Args>
- 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>(args)...);
}
template<typename T>
- void Delete(
- T* t,
- const char* file = __builtin_FILE(), int32 line = __builtin_LINE())
+ constexpr 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 = {})
+ {
+ return NewInner<T>(source_location, std::forward<A0>(arg0));
+ }
+
+ template<typename T, typename A0, typename A1>
+ constexpr T* New(
+ A0&& arg0, A1&& arg1,
+ const SourceLocation& source_location = {})
+ {
+ return NewInner<T>(source_location,
+ std::forward<A0>(arg0),
+ std::forward<A1>(arg1));
+ }
+
+ template<typename T, typename A0, typename A1, typename A2>
+ constexpr T* New(
+ A0&& arg0, A1&& arg1, A2&& arg2,
+ const SourceLocation& source_location = {})
+ {
+ return NewInner<T>(source_location,
+ std::forward<A0>(arg0),
+ std::forward<A1>(arg1),
+ std::forward<A2>(arg2));
+ }
+
+ template<typename T, typename A0, typename A1, typename A2, typename A3>
+ constexpr T* New(
+ A0&& arg0, A1&& arg1, A2&& arg2, A3&& arg3,
+ const SourceLocation& source_location = {})
+ {
+ return NewInner<T>(source_location,
+ std::forward<A0>(arg0),
+ std::forward<A1>(arg1),
+ std::forward<A2>(arg2),
+ std::forward<A3>(arg3));
+ }
+
+ template<typename T>
+ void Delete(T* t, const SourceLocation& source_location = {})
{
if constexpr (!kIsTriviallyDestructible<T>)
{
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<ApiRegistryImpl>();
+ g_allocator_api = BootstrapAllocatorApi();
+
+ Allocator* allocator = g_allocator_api->system;
+ ApiRegistry* api_registry = allocator->New<ApiRegistryImpl>(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<typename T> struct RemoveReferenceT { using Type = T; };
template<typename T> struct RemoveReferenceT<T&> { using Type = T; };
template<typename T> struct RemoveReferenceT<T&&> { 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_}
{}
};