From b70ed765ff6cf51aff20714e3ec4415b06898059 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Thu, 28 Mar 2024 00:02:43 +0100 Subject: Random work --- deimos/core/allocator.cpp | 2 +- deimos/core/allocator.h | 2 +- deimos/core/base.h | 14 ++++++++++-- deimos/core/hash.h | 12 ++++++---- deimos/core/id_name.h | 4 ++-- deimos/core/os.h | 37 ++++++++++++++++++++++++++++++ deimos/core/os_win32.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 deimos/core/os.h create mode 100644 deimos/core/os_win32.cpp (limited to 'deimos/core') diff --git a/deimos/core/allocator.cpp b/deimos/core/allocator.cpp index 642afb8..1478d36 100644 --- a/deimos/core/allocator.cpp +++ b/deimos/core/allocator.cpp @@ -44,7 +44,7 @@ public: system = &m_system; } - Allocator* CreateChild(Allocator* parent, const char* /* description */) override + Allocator* CreateChild(Allocator* parent, gsl::czstring /* description */) override { return parent; } diff --git a/deimos/core/allocator.h b/deimos/core/allocator.h index 911f8e7..967bbb1 100644 --- a/deimos/core/allocator.h +++ b/deimos/core/allocator.h @@ -138,7 +138,7 @@ public: Allocator* system{}; - virtual Allocator* CreateChild(Allocator* parent, const char* description) = 0; + virtual Allocator* CreateChild(Allocator* parent, gsl::czstring description) = 0; virtual void DestroyChild(Allocator*) = 0; }; diff --git a/deimos/core/base.h b/deimos/core/base.h index 0669f07..0cc48b1 100644 --- a/deimos/core/base.h +++ b/deimos/core/base.h @@ -26,6 +26,14 @@ deimos_DEFAULT_COPY(TYPE); \ deimos_DEFAULT_MOVE(TYPE); +namespace gsl +{ + +using zstring = char*; +using czstring = const char*; + +} // namespace gsl + namespace deimos { @@ -42,6 +50,8 @@ using int64 = long long; using float32 = float; using float64 = double; +enum __attribute__((__may_alias__)) byte : uint8 {}; + struct uint128 { uint64 high; @@ -52,11 +62,11 @@ struct uint128 struct SourceLocation { - const char* file; + gsl::czstring file; int32 line; constexpr SourceLocation( // NOLINT - const char* file_ = __builtin_FILE(), + gsl::czstring file_ = __builtin_FILE(), int32 line_ = __builtin_LINE()) : file{file_}, line{line_} diff --git a/deimos/core/hash.h b/deimos/core/hash.h index f1e8958..6ad30b0 100644 --- a/deimos/core/hash.h +++ b/deimos/core/hash.h @@ -5,7 +5,7 @@ namespace deimos { -constexpr uint64 MurmurHash3_GetBlock64(const char* key, uint64 block) +constexpr uint64 MurmurHash3_GetBlock64(const byte* key, uint64 block) { // NOLINTBEGIN key += block * 8; @@ -41,12 +41,11 @@ constexpr uint64 MurmurHash3_Fmix64(uint64 k) return k; } -constexpr uint128 MurmurHash3_x64_128(const char* key) +constexpr uint128 MurmurHash3_x64_128(const byte* key, uint64 len) { if consteval { return { 12, 12 }; } // NOLINTBEGIN - const uint64 len = __builtin_strlen(key); const uint64 nblocks = len / 16; const int64 seed = 0; @@ -73,7 +72,7 @@ constexpr uint128 MurmurHash3_x64_128(const char* key) //---------- // tail - const char* tail = key + nblocks * 16; + const byte* tail = key + nblocks * 16; uint64 k1 = 0; uint64 k2 = 0; @@ -118,5 +117,10 @@ constexpr uint128 MurmurHash3_x64_128(const char* key) return uint128{h1, h2}; } +constexpr uint128 MurmurHash3_x64_128(gsl::czstring str) +{ + return MurmurHash3_x64_128(BitCast(str), __builtin_strlen(str)); +} + } // namespace deimos diff --git a/deimos/core/id_name.h b/deimos/core/id_name.h index 47e7708..6ca5a46 100644 --- a/deimos/core/id_name.h +++ b/deimos/core/id_name.h @@ -9,9 +9,9 @@ namespace deimos struct IdName { uint128 hash; - const char* name; + gsl::czstring name; - explicit constexpr IdName(const char* name_) : + explicit constexpr IdName(gsl::czstring name_) : hash{MurmurHash3_x64_128(name_)}, name{name_} {} diff --git a/deimos/core/os.h b/deimos/core/os.h new file mode 100644 index 0000000..af6a775 --- /dev/null +++ b/deimos/core/os.h @@ -0,0 +1,37 @@ +#pragma once + +#include "deimos/core/base.h" +#include "deimos/core/id_name.h" + +namespace deimos +{ + +enum class OsConsoleType : uint8 +{ + kStdOut, + kStdErr, +}; + +class OsConsoleApi +{ +public: + OsConsoleApi() = default; + + deimos_NO_COPY_MOVE(OsConsoleApi); + + virtual ~OsConsoleApi() = default; + + // @Todo Use span + virtual void Write(OsConsoleType, const char* data, int64 length) = 0; +}; + +class OsApi +{ +public: + static constexpr IdName kApiName{"deimos::OsApi"}; + + OsConsoleApi* console{}; +}; + +} // namespace deimos + diff --git a/deimos/core/os_win32.cpp b/deimos/core/os_win32.cpp new file mode 100644 index 0000000..07d3247 --- /dev/null +++ b/deimos/core/os_win32.cpp @@ -0,0 +1,57 @@ +#include "deimos/core/os.h" +#include "deimos/core/api_registry.h" +#include "deimos/core/allocator.h" + +#define WIN32_LEAN_AND_MEAN +#include + +namespace deimos +{ + +class Win32OsConsoleApiImpl : public OsConsoleApi +{ + HANDLE m_stdout; + HANDLE m_stderr; + + constexpr HANDLE Handle(OsConsoleType type) const + { + switch (type) + { + using enum OsConsoleType; + case kStdOut: return m_stdout; + case kStdErr: return m_stderr; + } + } + +public: + Win32OsConsoleApiImpl() : + m_stdout{::GetStdHandle(STD_OUTPUT_HANDLE)}, + m_stderr{::GetStdHandle(STD_ERROR_HANDLE)} + {} + + void Write(OsConsoleType type, const char* data, int64 length) override + { + ::WriteConsoleA(Handle(type), data, (DWORD)length, nullptr, nullptr); + } +}; + +class Win32OsApiImpl : public OsApi +{ + Win32OsConsoleApiImpl m_console_api; + +public: + Win32OsApiImpl() + { + console = &m_console_api; + } +}; + +void RegisterOsApi(ApiRegistry* api_registry) +{ + auto* allocator_api = api_registry->Get(); + auto* os_api = allocator_api->system->New(); + api_registry->Set(os_api); +} + +} // namespace deimos + -- cgit