From 1f24912ddd16d8830af7b4e5011800db04091b04 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 28 Nov 2021 21:03:29 -0600 Subject: [PATCH] [ox] Correct bad bit_cast uses and improve constexpr-ness --- deps/ox/CMakeLists.txt | 2 +- deps/ox/src/ox/claw/read.hpp | 4 +- deps/ox/src/ox/claw/test/tests.cpp | 12 ++--- deps/ox/src/ox/event/signal.hpp | 19 ++++--- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 33 +++++++++---- deps/ox/src/ox/fs/filesystem/filelocation.hpp | 24 +++++++-- deps/ox/src/ox/fs/filesystem/filesystem.hpp | 2 +- .../ox/src/ox/fs/filesystem/passthroughfs.cpp | 2 +- .../ox/src/ox/fs/filesystem/passthroughfs.hpp | 3 +- deps/ox/src/ox/mc/CMakeLists.txt | 4 ++ deps/ox/src/ox/mc/intops.hpp | 6 +-- deps/ox/src/ox/mc/presenceindicator.cpp | 16 ------ deps/ox/src/ox/mc/presenceindicator.hpp | 49 ++++++++++++------- deps/ox/src/ox/mc/read.cpp | 12 ++--- deps/ox/src/ox/mc/read.hpp | 35 +++++++------ deps/ox/src/ox/mc/test/tests.cpp | 18 ++----- deps/ox/src/ox/mc/write.cpp | 10 ++-- deps/ox/src/ox/mc/write.hpp | 16 +++--- deps/ox/src/ox/model/desctypes.hpp | 14 +++--- deps/ox/src/ox/model/modelops.hpp | 3 +- deps/ox/src/ox/model/optype.hpp | 2 + deps/ox/src/ox/model/typenamecatcher.hpp | 10 +++- deps/ox/src/ox/model/walk.hpp | 4 +- deps/ox/src/ox/oc/read.cpp | 2 +- deps/ox/src/ox/oc/read.hpp | 10 +++- deps/ox/src/ox/std/bit.hpp | 47 ++++++------------ deps/ox/src/ox/std/fmt.hpp | 7 ++- deps/ox/src/ox/std/hashmap.hpp | 4 +- deps/ox/src/ox/std/heapmgr.cpp | 18 +++---- deps/ox/src/ox/std/heapmgr.hpp | 2 +- deps/ox/src/ox/std/random.cpp | 22 --------- deps/ox/src/ox/std/random.hpp | 23 +++++++-- deps/ox/src/ox/std/stacktrace.cpp | 20 ++++---- deps/ox/src/ox/std/test/tests.cpp | 4 +- deps/ox/src/ox/std/trace.hpp | 2 +- 35 files changed, 247 insertions(+), 214 deletions(-) diff --git a/deps/ox/CMakeLists.txt b/deps/ox/CMakeLists.txt index 1d1c23ea..be2e3b22 100644 --- a/deps/ox/CMakeLists.txt +++ b/deps/ox/CMakeLists.txt @@ -34,7 +34,7 @@ if(NOT OX_BUILD_EXEC OR NOT OX_USE_STDLIB) set(OX_RUN_TESTS OFF) endif() -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) diff --git a/deps/ox/src/ox/claw/read.hpp b/deps/ox/src/ox/claw/read.hpp index 5917b9ec..271b76fd 100644 --- a/deps/ox/src/ox/claw/read.hpp +++ b/deps/ox/src/ox/claw/read.hpp @@ -41,13 +41,13 @@ Error readClaw(const char *buff, std::size_t buffLen, T *val) { switch (header.fmt) { case ClawFormat::Metal: { - MetalClawReader reader(bit_cast(header.data), buffLen); + MetalClawReader reader(reinterpret_cast(header.data), buffLen); return model(&reader, val); } case ClawFormat::Organic: { #ifdef OX_USE_STDLIB - OrganicClawReader reader(bit_cast(header.data), buffLen); + OrganicClawReader reader(header.data, buffLen); return model(&reader, val); #else break; diff --git a/deps/ox/src/ox/claw/test/tests.cpp b/deps/ox/src/ox/claw/test/tests.cpp index 95a1f7b9..02fba8ac 100644 --- a/deps/ox/src/ox/claw/test/tests.cpp +++ b/deps/ox/src/ox/claw/test/tests.cpp @@ -50,20 +50,18 @@ struct TestStruct { int32_t Int7 = 0; int32_t Int8 = 0; TestUnion Union; - char *CString = nullptr; ox::BString<32> String = ""; uint32_t List[4] = {0, 0, 0, 0}; TestStructNest EmptyStruct; TestStructNest Struct; ~TestStruct() { - delete[] CString; } }; template -ox::Error model(T *io, TestUnion *obj) { +constexpr ox::Error model(T *io, TestUnion *obj) { io->template setTypeInfo(); oxReturnError(io->field("Bool", &obj->Bool)); oxReturnError(io->field("Int", &obj->Int)); @@ -72,7 +70,7 @@ ox::Error model(T *io, TestUnion *obj) { } template -ox::Error model(T *io, TestStructNest *obj) { +constexpr ox::Error model(T *io, TestStructNest *obj) { io->template setTypeInfo(); oxReturnError(io->field("Bool", &obj->Bool)); oxReturnError(io->field("Int", &obj->Int)); @@ -81,7 +79,7 @@ ox::Error model(T *io, TestStructNest *obj) { } template -ox::Error model(T *io, TestStruct *obj) { +constexpr ox::Error model(T *io, TestStruct *obj) { io->template setTypeInfo(); oxReturnError(io->field("Bool", &obj->Bool)); oxReturnError(io->field("Int", &obj->Int)); @@ -94,7 +92,6 @@ ox::Error model(T *io, TestStruct *obj) { oxReturnError(io->field("Int7", &obj->Int7)); oxReturnError(io->field("Int8", &obj->Int8)); oxReturnError(io->field("Union", ox::UnionView{&obj->Union, 1})); - oxReturnError(io->field("CString", ox::SerStr(&obj->CString))); oxReturnError(io->field("String", &obj->String)); oxReturnError(io->field("List", obj->List, 4)); oxReturnError(io->field("EmptyStruct", &obj->EmptyStruct)); @@ -146,8 +143,6 @@ std::map tests = { testIn.Int = 42; testIn.Union.Int = 42; testIn.String = "Test String 1"; - testIn.CString = new char[ox_strlen("c-string") + 1]; - ox_strcpy(testIn.CString, "c-string"); testIn.List[0] = 1; testIn.List[1] = 2; testIn.List[2] = 3; @@ -171,7 +166,6 @@ std::map tests = { oxAssert(testIn.Int6 == testOut.Int6, "Int6 value mismatch"); oxAssert(testIn.Int7 == testOut.Int7, "Int7 value mismatch"); oxAssert(testIn.Int8 == testOut.Int8, "Int8 value mismatch"); - oxAssert(ox_strcmp(testIn.CString, testOut.CString) == 0, "CString value mismatch"); oxAssert(testIn.Union.Int == testOut.Union.Int, "Union.Int value mismatch"); oxAssert(testIn.String == testOut.String, "String value mismatch"); oxAssert(testIn.List[0] == testOut.List[0], "List[0] value mismatch"); diff --git a/deps/ox/src/ox/event/signal.hpp b/deps/ox/src/ox/event/signal.hpp index a9c40995..2bb3d802 100644 --- a/deps/ox/src/ox/event/signal.hpp +++ b/deps/ox/src/ox/event/signal.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace ox { @@ -44,7 +45,8 @@ class Signal { virtual ~BaseSlot() = default; virtual void call(Args...) = 0; virtual void cleanup(Signal*) noexcept {} - virtual const void *receiver() noexcept { return nullptr; } + [[nodiscard]] + virtual const void *receiver() const noexcept { return nullptr; } }; template @@ -86,7 +88,8 @@ class Signal { oxIgnoreError(m_receiver->destruction.disconnectSignal(signal)); } - const void *receiver() noexcept final { + [[nodiscard]] + const void *receiver() const noexcept final { return m_receiver; } }; @@ -112,7 +115,8 @@ class Signal { void cleanup(Signal*) noexcept final { } - const void *receiver() noexcept final { + [[nodiscard]] + const void *receiver() const noexcept final { return m_receiver; } }; @@ -228,7 +232,8 @@ class Signal { virtual ~BaseSlot() = default; virtual Error call(Args...) noexcept = 0; virtual void cleanup(Signal*) noexcept {} - virtual const void *receiver() noexcept { return nullptr; } + [[nodiscard]] + virtual const void *receiver() const noexcept { return nullptr; } }; struct FunctionSlot: public BaseSlot { @@ -261,7 +266,8 @@ class Signal { oxIgnoreError(m_receiver->destruction.disconnectSignal(signal)); } - const void *receiver() noexcept final { + [[nodiscard]] + const void *receiver() const noexcept final { return m_receiver; } }; @@ -283,7 +289,8 @@ class Signal { void cleanup(Signal*) noexcept final { } - const void *receiver() noexcept final { + [[nodiscard]] + const void *receiver() const noexcept final { return m_receiver; } }; diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index 62d7a925..d76d5511 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -12,11 +12,6 @@ namespace ox { -FileAddress::FileAddress() noexcept { - m_data.inode = 0; - m_type = FileAddressType::Inode; -} - FileAddress::FileAddress(const FileAddress &other) noexcept { operator=(other); } @@ -50,6 +45,9 @@ FileAddress::~FileAddress() noexcept { } FileAddress &FileAddress::operator=(const FileAddress &other) noexcept { + if (this == &other) { + return *this; + } cleanup(); m_type = other.m_type; switch (m_type) { @@ -71,9 +69,26 @@ FileAddress &FileAddress::operator=(const FileAddress &other) noexcept { } FileAddress &FileAddress::operator=(FileAddress &&other) noexcept { + if (this == &other) { + return *this; + } cleanup(); m_type = other.m_type; - memcpy(this, &other, sizeof(*this)); + switch (m_type) { + case FileAddressType::Path: + { + auto strSize = ox_strlen(other.m_data.path) + 1; + m_data.path = new char[strSize]; + ox_memcpy(m_data.path, other.m_data.path, strSize); + break; + } + case FileAddressType::ConstPath: + case FileAddressType::Inode: + m_data = other.m_data; + break; + case FileAddressType::None: + break; + } other.clear(); return *this; } @@ -86,10 +101,8 @@ void FileAddress::cleanup() noexcept { } void FileAddress::clear() noexcept { - if (m_type == FileAddressType::Path) { - m_data.path = nullptr; - m_type = FileAddressType::None; - } + m_data.path = nullptr; + m_type = FileAddressType::None; } } diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index b2b1a509..345653e4 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.hpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -9,6 +9,7 @@ #pragma once #include +#include #include namespace ox { @@ -32,7 +33,7 @@ class FileAddress { union Data { static constexpr auto TypeName = "net.drinkingtea.ox.FileAddress.Data"; static constexpr auto Fields = 3; - char *path; + char *path = nullptr; const char *constPath; uint64_t inode; }; @@ -42,7 +43,10 @@ class FileAddress { Data m_data; public: - FileAddress() noexcept; + constexpr FileAddress() noexcept { + m_data.inode = 0; + m_type = FileAddressType::Inode; + } FileAddress(const FileAddress &other) noexcept; @@ -93,7 +97,7 @@ class FileAddress { } } - constexpr operator bool() const noexcept { + explicit constexpr operator bool() const noexcept { return m_type != FileAddressType::None; } @@ -110,6 +114,16 @@ class FileAddress { }; +template<> +constexpr const char *getModelTypeName() noexcept { + return FileAddress::Data::TypeName; +} + +template<> +constexpr const char *getModelTypeName() noexcept { + return FileAddress::TypeName; +} + template constexpr Error model(T *io, FileAddress::Data *obj) noexcept { io->template setTypeInfo(); @@ -122,7 +136,9 @@ constexpr Error model(T *io, FileAddress::Data *obj) noexcept { template constexpr Error model(T *io, FileAddress *fa) noexcept { io->template setTypeInfo(); - oxReturnError(io->field("type", bit_cast(&fa->m_type))); + auto type = static_cast(fa->m_type); + oxReturnError(io->field("type", &type)); + fa->m_type = static_cast(type); oxReturnError(io->field("data", UnionView(&fa->m_data, static_cast(fa->m_type)))); return OxError(0); } diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystem.hpp index d96a347c..47329ea1 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -269,7 +269,7 @@ Result FileSystemTemplate::directAccess(uint6 if (!data.valid()) { return OxError(1); } - return bit_cast(data.get()); + return reinterpret_cast(data.get()); } template diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp index 2591a245..afc758a6 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -101,7 +101,7 @@ Result> PassThroughFS::ls(const char *dir) noexcept { oxReturnError(OxError(ec.value(), "PassThroughFS: ls failed")); for (const auto &p : di) { const auto u8p = p.path().filename().u8string(); - out.emplace_back(bit_cast(u8p.c_str())); + out.emplace_back(reinterpret_cast(u8p.c_str())); } return ox::move(out); } diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index 8f28f85b..a0dcaba9 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -95,8 +95,7 @@ Error PassThroughFS::ls(const char *dir, F cb) noexcept { const auto di = std::filesystem::directory_iterator(m_path / stripSlash(dir), ec); oxReturnError(OxError(ec.value(), "PassThroughFS: ls failed")); for (auto &p : di) { - const auto u8p = p.path().filename().u8string(); - oxReturnError(cb(bit_cast(u8p.c_str()), 0)); + oxReturnError(cb(p.path().filename().c_str(), 0)); } return OxError(0); } diff --git a/deps/ox/src/ox/mc/CMakeLists.txt b/deps/ox/src/ox/mc/CMakeLists.txt index 1ecf4113..6d07d123 100644 --- a/deps/ox/src/ox/mc/CMakeLists.txt +++ b/deps/ox/src/ox/mc/CMakeLists.txt @@ -5,6 +5,10 @@ add_library( write.cpp ) +if(NOT MSVC) + target_compile_options(OxMetalClaw PRIVATE -Wsign-conversion) +endif() + target_link_libraries( OxMetalClaw PUBLIC OxModel diff --git a/deps/ox/src/ox/mc/intops.hpp b/deps/ox/src/ox/mc/intops.hpp index ec297d8d..70c063db 100644 --- a/deps/ox/src/ox/mc/intops.hpp +++ b/deps/ox/src/ox/mc/intops.hpp @@ -25,14 +25,14 @@ static constexpr auto Bits = sizeof(T) << 3; template [[nodiscard]] constexpr std::size_t highestBit(I val) noexcept { - int shiftStart = sizeof(I) * 8 - 1; + unsigned shiftStart = sizeof(I) * 8 - 1; // find most significant non-sign indicator bit std::size_t highestBit = 0; // start at one bit lower if signed if constexpr(is_signed_v) { --shiftStart; } - for (auto i = shiftStart; i > -1; --i) { + for (auto i = shiftStart; i < MaxValue; --i) { const auto bitValue = (val >> i) & 1; if (bitValue) { highestBit = i; @@ -80,7 +80,7 @@ constexpr McInt encodeInteger(I input) noexcept { const auto bytesIndicator = onMask(bytes - 1); // ensure we are copying from little endian represenstation - LittleEndian leVal = val; + LittleEndian leVal = static_cast(val); if (bytes == 9) { out.data[0] = bytesIndicator; ox_memcpy(&out.data[1], &leVal, sizeof(I)); diff --git a/deps/ox/src/ox/mc/presenceindicator.cpp b/deps/ox/src/ox/mc/presenceindicator.cpp index b9f72953..114aca6b 100644 --- a/deps/ox/src/ox/mc/presenceindicator.cpp +++ b/deps/ox/src/ox/mc/presenceindicator.cpp @@ -14,20 +14,4 @@ namespace ox { template class FieldBitmapReader; template class FieldBitmapReader; -FieldBitmap::FieldBitmap(uint8_t *map, std::size_t maxLen) noexcept: FieldBitmapReader(map, maxLen) { -} - -Error FieldBitmap::set(std::size_t i, bool on) noexcept { - if (i / 8 < m_mapLen) { - if (on) { - m_map[i / 8] |= 1 << (i % 8); - } else { - m_map[i / 8] &= ~(1 << (i % 8)); - } - return OxError(0); - } else { - return OxError(MC_PRESENCEMASKOUTBOUNDS); - } -} - } diff --git a/deps/ox/src/ox/mc/presenceindicator.hpp b/deps/ox/src/ox/mc/presenceindicator.hpp index fcfa4d87..896ff6a9 100644 --- a/deps/ox/src/ox/mc/presenceindicator.hpp +++ b/deps/ox/src/ox/mc/presenceindicator.hpp @@ -18,29 +18,29 @@ class FieldBitmapReader { protected: T m_map = nullptr; std::size_t m_mapLen = 0; - std::size_t m_fields = 0; public: - FieldBitmapReader(T map, std::size_t maxLen) noexcept; + constexpr FieldBitmapReader(T map, std::size_t maxLen) noexcept; - Result get(std::size_t i) const noexcept; + constexpr Result get(std::size_t i) const noexcept; - void setFields(int) noexcept; + constexpr void setFields(int) noexcept; - void setMaxLen(int) noexcept; + constexpr void setMaxLen(int) noexcept; - int getMaxLen() const noexcept; + [[nodiscard]] + constexpr int64_t getMaxLen() const noexcept; }; template -FieldBitmapReader::FieldBitmapReader(T map, std::size_t maxLen) noexcept { +constexpr FieldBitmapReader::FieldBitmapReader(T map, std::size_t maxLen) noexcept { m_map = map; m_mapLen = maxLen; } template -Result FieldBitmapReader::get(std::size_t i) const noexcept { +constexpr Result FieldBitmapReader::get(std::size_t i) const noexcept { if (i / 8 < m_mapLen) { return (m_map[i / 8] >> (i % 8)) & 1; } else { @@ -49,19 +49,18 @@ Result FieldBitmapReader::get(std::size_t i) const noexcept { } template -void FieldBitmapReader::setFields(int fields) noexcept { - m_fields = fields; - m_mapLen = (fields / 8 + 1) - (fields % 8 == 0); +constexpr void FieldBitmapReader::setFields(int fields) noexcept { + m_mapLen = static_cast((fields / 8 + 1) - (fields % 8 == 0)); } template -void FieldBitmapReader::setMaxLen(int maxLen) noexcept { - m_mapLen = maxLen; +constexpr void FieldBitmapReader::setMaxLen(int maxLen) noexcept { + m_mapLen = static_cast(maxLen); } template -int FieldBitmapReader::getMaxLen() const noexcept { - return m_mapLen; +constexpr int64_t FieldBitmapReader::getMaxLen() const noexcept { + return static_cast(m_mapLen); } extern template class FieldBitmapReader; @@ -70,10 +69,26 @@ extern template class FieldBitmapReader; class FieldBitmap: public FieldBitmapReader { public: - FieldBitmap(uint8_t *map, std::size_t maxLen) noexcept; + constexpr FieldBitmap(uint8_t *map, std::size_t maxLen) noexcept; - Error set(std::size_t i, bool on) noexcept; + constexpr Error set(std::size_t i, bool on) noexcept; }; +constexpr FieldBitmap::FieldBitmap(uint8_t *map, std::size_t maxLen) noexcept: FieldBitmapReader(map, maxLen) { +} + +constexpr Error FieldBitmap::set(std::size_t i, bool on) noexcept { + if (i / 8 < m_mapLen) { + if (on) { + m_map[i / 8] |= 1 << (i % 8); + } else { + m_map[i / 8] &= ~(1 << (i % 8)); + } + return OxError(0); + } else { + return OxError(MC_PRESENCEMASKOUTBOUNDS); + } +} + } diff --git a/deps/ox/src/ox/mc/read.cpp b/deps/ox/src/ox/mc/read.cpp index 17ab4190..7aa33d09 100644 --- a/deps/ox/src/ox/mc/read.cpp +++ b/deps/ox/src/ox/mc/read.cpp @@ -66,7 +66,7 @@ Error MetalClawReader::field(const char*, uint64_t *val) noexcept { Error MetalClawReader::field(const char*, bool *val) noexcept { if (m_unionIdx == -1 || m_unionIdx == m_field) { - auto valErr = m_fieldPresence.get(m_field); + auto valErr = m_fieldPresence.get(static_cast(m_field)); *val = valErr.value; oxReturnError(valErr.error); } @@ -76,7 +76,7 @@ Error MetalClawReader::field(const char*, bool *val) noexcept { Error MetalClawReader::field(const char*, SerStr val) noexcept { if (m_unionIdx == -1 || m_unionIdx == m_field) { - if (m_fieldPresence.get(m_field)) { + if (m_fieldPresence.get(static_cast(m_field))) { // read the length if (m_buffIt >= m_buffLen) { return OxError(MC_BUFFENDED); @@ -110,7 +110,7 @@ Error MetalClawReader::field(const char*, SerStr val) noexcept { } Result MetalClawReader::arrayLength(const char*, bool pass) noexcept { - if ((m_unionIdx == -1 || m_unionIdx == m_field) && m_fieldPresence.get(m_field)) { + if ((m_unionIdx == -1 || m_unionIdx == m_field) && m_fieldPresence.get(static_cast(m_field))) { // read the length if (m_buffIt >= m_buffLen) { return OxError(MC_BUFFENDED); @@ -127,7 +127,7 @@ Result MetalClawReader::arrayLength(const char*, bool pass) noexcep [[nodiscard]] StringLength MetalClawReader::stringLength(const char*) noexcept { - if ((m_unionIdx == -1 || m_unionIdx == m_field) && m_fieldPresence.get(m_field)) { + if ((m_unionIdx == -1 || m_unionIdx == m_field) && m_fieldPresence.get(static_cast(m_field))) { // read the length std::size_t bytesRead = 0; auto len = mc::decodeInteger(&m_buff[m_buffIt], m_buffLen - m_buffIt, &bytesRead); @@ -141,11 +141,11 @@ MetalClawReader MetalClawReader::child(const char*, int unionIdx) noexcept { } bool MetalClawReader::fieldPresent(const char*) const noexcept { - return m_fieldPresence.get(m_field).value; + return m_fieldPresence.get(static_cast(m_field)).value; } bool MetalClawReader::fieldPresent(int fieldNo) const noexcept { - return m_fieldPresence.get(fieldNo).value; + return m_fieldPresence.get(static_cast(fieldNo)).value; } void MetalClawReader::nextField() noexcept { diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index a3038f43..076888f6 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -90,7 +90,9 @@ class MetalClawReader { StringLength stringLength(const char *name) noexcept; template - void setTypeInfo(const char *name = getModelTypeName(), int fields = countFields()) noexcept; + constexpr void setTypeInfo() noexcept; + + constexpr void setTypeInfo(const char *name, int fields) noexcept; /** * Returns a MetalClawReader to parse a child object. @@ -128,7 +130,7 @@ Error MetalClawReader::field(const char *name, T *val) noexcept { if constexpr(isVector_v) { if (m_unionIdx == -1 || m_unionIdx == m_field) { // set size of val if the field is present, don't worry about it if not - if (m_fieldPresence.get(m_field)) { + if (m_fieldPresence.get(static_cast(m_field))) { oxRequire(len, arrayLength(name, false)); val->resize(len); } @@ -148,7 +150,7 @@ Error MetalClawReader::field(const char *name, T *val) noexcept { template Error MetalClawReader::field(const char*, UnionView val) noexcept { - if ((m_unionIdx == -1 || m_unionIdx == m_field) && val.get() && m_fieldPresence.get(m_field)) { + if ((m_unionIdx == -1 || m_unionIdx == m_field) && val.get() && m_fieldPresence.get(static_cast(m_field))) { auto reader = child("", val.idx()); oxReturnError(model(&reader, val.get())); } @@ -164,7 +166,7 @@ Error MetalClawReader::field(const char *name, BString *val) noexcept { template Error MetalClawReader::readInteger(I *val) noexcept { if (m_unionIdx == -1 || m_unionIdx == m_field) { - if (m_fieldPresence.get(m_field)) { + if (m_fieldPresence.get(static_cast(m_field))) { std::size_t bytesRead = 0; if (m_buffIt >= m_buffLen) { oxTrace("ox::MetalClaw::readInteger") << "Buffer ended"; @@ -186,7 +188,7 @@ Error MetalClawReader::readInteger(I *val) noexcept { template Error MetalClawReader::field(const char *name, T *val, std::size_t valLen) noexcept { if (m_unionIdx == -1 || m_unionIdx == m_field) { - if (m_fieldPresence.get(m_field)) { + if (m_fieldPresence.get(static_cast(m_field))) { // read the length if (m_buffIt >= m_buffLen) { return OxError(MC_BUFFENDED); @@ -198,7 +200,7 @@ Error MetalClawReader::field(const char *name, T *val, std::size_t valLen) noexc // read the list if (valLen >= len) { auto reader = child(""); - reader.setTypeInfo("List", len); + reader.setTypeInfo("List", static_cast(len)); for (std::size_t i = 0; i < len; i++) { oxReturnError(reader.field("", &val[i])); } @@ -215,7 +217,7 @@ Error MetalClawReader::field(const char *name, T *val, std::size_t valLen) noexc template Error MetalClawReader::field(const char*, HashMap *val) noexcept { if (m_unionIdx == -1 || m_unionIdx == m_field) { - if (m_fieldPresence.get(m_field)) { + if (m_fieldPresence.get(static_cast(m_field))) { // read the length if (m_buffIt >= m_buffLen) { return OxError(MC_BUFFENDED); @@ -226,11 +228,12 @@ Error MetalClawReader::field(const char*, HashMap *val) noexcept { // read the list auto reader = child(""); - reader.setTypeInfo("List", len); + reader.setTypeInfo("List", static_cast(len)); for (std::size_t i = 0; i < len; i++) { const auto keyLen = reader.stringLength(nullptr); auto wkey = ox_malloca(keyLen + 1, char, 0); - oxReturnError(reader.field("", SerStr(wkey.get(), keyLen))); + auto wkeyPtr = wkey.get(); + oxReturnError(reader.field("", SerStr(&wkeyPtr, static_cast(keyLen)))); oxReturnError(reader.field("", &val->operator[](wkey.get()))); } } @@ -242,7 +245,7 @@ Error MetalClawReader::field(const char*, HashMap *val) noexcept { template Error MetalClawReader::field(const char*, Handler handler) noexcept { if (m_unionIdx == -1 || m_unionIdx == m_field) { - if (m_fieldPresence.get(m_field)) { + if (m_fieldPresence.get(static_cast(m_field))) { // read the length if (m_buffIt >= m_buffLen) { return OxError(MC_BUFFENDED); @@ -253,7 +256,7 @@ Error MetalClawReader::field(const char*, Handler handler) noexcept { // read the list auto reader = child(""); - reader.setTypeInfo("List", len); + reader.setTypeInfo("List", static_cast(len)); for (std::size_t i = 0; i < len; i++) { T val; oxReturnError(reader.field("", &val)); @@ -266,16 +269,20 @@ Error MetalClawReader::field(const char*, Handler handler) noexcept { } template -void MetalClawReader::setTypeInfo(const char*, int fields) noexcept { +constexpr void MetalClawReader::setTypeInfo() noexcept { + setTypeInfo(ModelTypeName_v, countFields()); +} + +constexpr void MetalClawReader::setTypeInfo(const char*, int fields) noexcept { m_fields = fields; - m_buffIt = (fields / 8 + 1) - (fields % 8 == 0); + m_buffIt = static_cast((fields / 8 + 1) - (fields % 8 == 0)); m_fieldPresence.setFields(fields); m_fieldPresence.setMaxLen(static_cast(m_buffIt)); } template Error readMC(const char *buff, std::size_t buffLen, T *val) noexcept { - MetalClawReader reader(bit_cast(buff), buffLen); + MetalClawReader reader(reinterpret_cast(buff), buffLen); return model(&reader, val); } diff --git a/deps/ox/src/ox/mc/test/tests.cpp b/deps/ox/src/ox/mc/test/tests.cpp index fa8c11ee..c425d089 100644 --- a/deps/ox/src/ox/mc/test/tests.cpp +++ b/deps/ox/src/ox/mc/test/tests.cpp @@ -36,7 +36,7 @@ struct TestStructNest { struct TestStruct { static constexpr auto TypeName = "TestStruct"; - static constexpr auto Fields = 17; + static constexpr auto Fields = 16; bool Bool = false; int32_t Int = 0; int32_t Int1 = 0; @@ -48,21 +48,15 @@ struct TestStruct { int32_t Int7 = 0; int32_t Int8 = 0; TestUnion Union; - char *CString = nullptr; ox::BString<32> String = ""; uint32_t List[4] = {0, 0, 0, 0}; ox::HashMap Map; TestStructNest EmptyStruct; TestStructNest Struct; - - ~TestStruct() { - delete[] CString; - } - }; template -ox::Error model(T *io, TestUnion *obj) { +constexpr ox::Error model(T *io, TestUnion *obj) noexcept { io->template setTypeInfo(); oxReturnError(io->field("Bool", &obj->Bool)); oxReturnError(io->field("Int", &obj->Int)); @@ -71,7 +65,7 @@ ox::Error model(T *io, TestUnion *obj) { } template -ox::Error model(T *io, TestStructNest *obj) { +constexpr ox::Error model(T *io, TestStructNest *obj) noexcept { io->template setTypeInfo(); oxReturnError(io->field("Bool", &obj->Bool)); oxReturnError(io->field("Int", &obj->Int)); @@ -80,7 +74,7 @@ ox::Error model(T *io, TestStructNest *obj) { } template -ox::Error model(T *io, TestStruct *obj) { +constexpr ox::Error model(T *io, TestStruct *obj) noexcept { io->template setTypeInfo(); oxReturnError(io->field("Bool", &obj->Bool)); oxReturnError(io->field("Int", &obj->Int)); @@ -93,7 +87,6 @@ ox::Error model(T *io, TestStruct *obj) { oxReturnError(io->field("Int7", &obj->Int7)); oxReturnError(io->field("Int8", &obj->Int8)); oxReturnError(io->field("Union", ox::UnionView{&obj->Union, 1})); - oxReturnError(io->field("CString", ox::SerStr(&obj->CString))); oxReturnError(io->field("String", &obj->String)); oxReturnError(io->field("List", obj->List, 4)); oxReturnError(io->field("Map", &obj->Map)); @@ -127,8 +120,6 @@ std::map tests = { testIn.Int = 42; testIn.Union.Int = 42; testIn.String = "Test String 1"; - testIn.CString = new char[ox_strlen("c-string") + 1]; - ox_strcpy(testIn.CString, "c-string"); testIn.List[0] = 1; testIn.List[1] = 2; testIn.List[2] = 3; @@ -153,7 +144,6 @@ std::map tests = { oxAssert(testIn.Int6 == testOut.Int6, "Int6 value mismatch"); oxAssert(testIn.Int7 == testOut.Int7, "Int7 value mismatch"); oxAssert(testIn.Int8 == testOut.Int8, "Int8 value mismatch"); - oxAssert(ox_strcmp(testIn.CString, testOut.CString) == 0, "CString value mismatch"); oxAssert(testIn.Union.Int == testOut.Union.Int, "Union.Int value mismatch"); oxAssert(testIn.String == testOut.String, "String value mismatch"); oxAssert(testIn.List[0] == testOut.List[0], "List[0] value mismatch"); diff --git a/deps/ox/src/ox/mc/write.cpp b/deps/ox/src/ox/mc/write.cpp index 4dc61619..ea4a7cdd 100644 --- a/deps/ox/src/ox/mc/write.cpp +++ b/deps/ox/src/ox/mc/write.cpp @@ -63,7 +63,7 @@ Error MetalClawWriter::field(const char*, uint64_t *val) noexcept { Error MetalClawWriter::field(const char*, bool *val) noexcept { if (m_unionIdx == -1 || m_unionIdx == m_field) { - oxReturnError(m_fieldPresence.set(m_field, *val)); + oxReturnError(m_fieldPresence.set(static_cast(m_field), *val)); } ++m_field; return OxError(0); @@ -74,18 +74,18 @@ Error MetalClawWriter::field(const char*, SerStr val) noexcept { if (val.len() && (m_unionIdx == -1 || m_unionIdx == m_field)) { // write the length const auto strLen = mc::encodeInteger(val.len()); - if (m_buffIt + strLen.length + val.len() < m_buffLen) { + if (m_buffIt + strLen.length + static_cast(val.len()) < m_buffLen) { ox_memcpy(&m_buff[m_buffIt], strLen.data, strLen.length); m_buffIt += strLen.length; // write the string - ox_memcpy(&m_buff[m_buffIt], val.c_str(), val.len()); - m_buffIt += val.len(); + ox_memcpy(&m_buff[m_buffIt], val.c_str(), static_cast(val.len())); + m_buffIt += static_cast(val.len()); fieldSet = true; } else { return OxError(MC_BUFFENDED); } } - oxReturnError(m_fieldPresence.set(m_field, fieldSet)); + oxReturnError(m_fieldPresence.set(static_cast(m_field), fieldSet)); ++m_field; return OxError(0); } diff --git a/deps/ox/src/ox/mc/write.hpp b/deps/ox/src/ox/mc/write.hpp index df75eb24..cd844421 100644 --- a/deps/ox/src/ox/mc/write.hpp +++ b/deps/ox/src/ox/mc/write.hpp @@ -107,7 +107,7 @@ Error MetalClawWriter::field(const char*, T *val) noexcept { fieldSet = true; } } - oxReturnError(m_fieldPresence.set(m_field, fieldSet)); + oxReturnError(m_fieldPresence.set(static_cast(m_field), fieldSet)); m_field++; return OxError(0); } @@ -124,7 +124,7 @@ Error MetalClawWriter::field(const char*, UnionView val) noexcept { fieldSet = true; } } - oxReturnError(m_fieldPresence.set(m_field, fieldSet)); + oxReturnError(m_fieldPresence.set(static_cast(m_field), fieldSet)); m_field++; return OxError(0); } @@ -155,7 +155,7 @@ Error MetalClawWriter::field(const char*, T *val, std::size_t len) noexcept { fieldSet = true; } - oxReturnError(m_fieldPresence.set(m_field, fieldSet)); + oxReturnError(m_fieldPresence.set(static_cast(m_field), fieldSet)); m_field++; return OxError(0); } @@ -194,7 +194,7 @@ Error MetalClawWriter::field(const char*, HashMap *val) noexcept { fieldSet = true; } - oxReturnError(m_fieldPresence.set(m_field, fieldSet)); + oxReturnError(m_fieldPresence.set(static_cast(m_field), fieldSet)); m_field++; return OxError(0); } @@ -212,7 +212,7 @@ Error MetalClawWriter::appendInteger(I val) noexcept { return OxError(MC_BUFFENDED); } } - oxReturnError(m_fieldPresence.set(m_field, fieldSet)); + oxReturnError(m_fieldPresence.set(static_cast(m_field), fieldSet)); m_field++; return OxError(0); } @@ -221,14 +221,14 @@ template void MetalClawWriter::setTypeInfo(const char*, int fields) noexcept { m_fields = fields; m_fieldPresence.setFields(fields); - m_buffIt = m_fieldPresence.getMaxLen(); + m_buffIt = static_cast(m_fieldPresence.getMaxLen()); ox_memset(m_buff, 0, m_buffIt); } template Result writeMC(T *val) noexcept { Buffer buff(10 * units::MB); - MetalClawWriter writer(bit_cast(buff.data()), buff.size()); + MetalClawWriter writer(reinterpret_cast(buff.data()), buff.size()); oxReturnError(model(&writer, val)); buff.resize(writer.size()); return move(buff); @@ -236,7 +236,7 @@ Result writeMC(T *val) noexcept { template Error writeMC(char *buff, std::size_t buffLen, T *val, std::size_t *sizeOut = nullptr) noexcept { - MetalClawWriter writer(bit_cast(buff), buffLen); + MetalClawWriter writer(reinterpret_cast(buff), buffLen); auto err = model(&writer, val); if (sizeOut) { *sizeOut = writer.size(); diff --git a/deps/ox/src/ox/model/desctypes.hpp b/deps/ox/src/ox/model/desctypes.hpp index 2ab8c25c..73372dc4 100644 --- a/deps/ox/src/ox/model/desctypes.hpp +++ b/deps/ox/src/ox/model/desctypes.hpp @@ -109,8 +109,8 @@ using FieldList = Vector; struct DescriptorType { static constexpr auto TypeVersion = 1; - TypeName typeName; - PrimitiveType primitiveType; + TypeName typeName = ""; + PrimitiveType primitiveType = PrimitiveType::UnsignedInteger; // fieldList only applies to structs FieldList fieldList; // - number of bytes for integer and float types @@ -123,7 +123,7 @@ struct DescriptorType { DescriptorType(const TypeName &tn, PrimitiveType t, int b): typeName(tn), primitiveType(t), length(b) { } - DescriptorType(const TypeName &tn, PrimitiveType t, FieldList fl): typeName(tn), primitiveType(t), fieldList(fl) { + DescriptorType(const TypeName &tn, PrimitiveType t, const FieldList &fl): typeName(tn), primitiveType(t), fieldList(fl) { } }; @@ -132,7 +132,9 @@ template constexpr Error model(T *io, DescriptorType *type) noexcept { io->template setTypeInfo("net.drinkingtea.ox.DescriptorType", 5); oxReturnError(io->field("typeName", &type->typeName)); - oxReturnError(io->field("primitiveType", bit_cast(&type->primitiveType))); + auto pt = std::bit_cast(type->primitiveType); + oxReturnError(io->field("primitiveType", &pt)); + type->primitiveType = std::bit_cast(pt); oxReturnError(io->field("fieldList", &type->fieldList)); oxReturnError(io->field("length", &type->length)); oxReturnError(io->field("preloadable", &type->preloadable)); @@ -143,8 +145,8 @@ template Error modelWrite(T *io, DescriptorField *field) noexcept { io->setTypeInfo("ox::DescriptorField", 4); if (field->ownsType) { - BString<2> empty = ""; - oxReturnError(io->field("typeName", SerStr(&empty))); + String empty = ""; + oxReturnError(io->field("typeName", &empty)); oxReturnError(io->field("type", field->type)); } else { oxReturnError(io->field("typeName", &field->type->typeName)); diff --git a/deps/ox/src/ox/model/modelops.hpp b/deps/ox/src/ox/model/modelops.hpp index 0b0f23a6..1d2b93ff 100644 --- a/deps/ox/src/ox/model/modelops.hpp +++ b/deps/ox/src/ox/model/modelops.hpp @@ -8,6 +8,7 @@ #pragma once +#include #include #include #include @@ -196,7 +197,7 @@ class Equals { template constexpr Error field(const char*, const FT *v) noexcept { const auto &src = *v; - const auto &dst = *cbit_cast(m_other->vars[m_i]); + const auto &dst = std::bit_cast(*m_other->vars[m_i]); ++m_i; if (dst == src) { return OxError(0); diff --git a/deps/ox/src/ox/model/optype.hpp b/deps/ox/src/ox/model/optype.hpp index 435e1580..85958a36 100644 --- a/deps/ox/src/ox/model/optype.hpp +++ b/deps/ox/src/ox/model/optype.hpp @@ -24,11 +24,13 @@ namespace OpType { template constexpr Error modelRead(T*, O*) noexcept { + oxAssert(OxError(1), "Missing modelRead function"); return OxError(1, "Model: modelRead not implemented"); } template constexpr Error modelWrite(T*, O*) noexcept { + oxAssert(OxError(1), "Missing modelWrite function"); return OxError(1, "Model: modelWrite not implemented"); } diff --git a/deps/ox/src/ox/model/typenamecatcher.hpp b/deps/ox/src/ox/model/typenamecatcher.hpp index 1edae1e3..a08084d1 100644 --- a/deps/ox/src/ox/model/typenamecatcher.hpp +++ b/deps/ox/src/ox/model/typenamecatcher.hpp @@ -10,6 +10,7 @@ #include #include +#include #include "fieldcounter.hpp" #include "optype.hpp" @@ -45,9 +46,11 @@ struct TypeNameCatcher { template constexpr const char *getModelTypeName() noexcept { - T t; + auto a = std::allocator(); + auto t = a.allocate(1); TypeNameCatcher nc; - oxIgnoreError(model(&nc, &t)); + oxIgnoreError(model(&nc, t)); + a.deallocate(t, 1); return nc.name; } @@ -58,4 +61,7 @@ constexpr const char *getModelTypeName(T *val) noexcept { return nc.name; } +template +constexpr auto ModelTypeName_v = getModelTypeName(); + } diff --git a/deps/ox/src/ox/model/walk.hpp b/deps/ox/src/ox/model/walk.hpp index d6ad4aab..8e119c4f 100644 --- a/deps/ox/src/ox/model/walk.hpp +++ b/deps/ox/src/ox/model/walk.hpp @@ -143,9 +143,9 @@ constexpr Error model(Reader *rdr, DataWalker *walker) noexcept { } template -Error walkModel(DescriptorType *type, char *data, std::size_t dataLen, Handler handler) noexcept { +Error walkModel(DescriptorType *type, const char *data, std::size_t dataLen, Handler handler) noexcept { DataWalker walker(type, handler); - Reader rdr(bit_cast(data), dataLen); + Reader rdr(reinterpret_cast(data), dataLen); return model(&rdr, &walker); } diff --git a/deps/ox/src/ox/oc/read.cpp b/deps/ox/src/ox/oc/read.cpp index 46b323c7..9e069840 100644 --- a/deps/ox/src/ox/oc/read.cpp +++ b/deps/ox/src/ox/oc/read.cpp @@ -13,7 +13,7 @@ namespace ox { OrganicClawReader::OrganicClawReader(const uint8_t *buff, std::size_t buffSize) { - auto json = bit_cast(buff); + auto json = reinterpret_cast(buff); auto jsonLen = ox_strnlen(json, buffSize); Json::CharReaderBuilder parserBuilder; auto parser = std::unique_ptr(parserBuilder.newCharReader()); diff --git a/deps/ox/src/ox/oc/read.hpp b/deps/ox/src/ox/oc/read.hpp index db28313d..31708df0 100644 --- a/deps/ox/src/ox/oc/read.hpp +++ b/deps/ox/src/ox/oc/read.hpp @@ -85,7 +85,15 @@ class OrganicClawReader { std::size_t stringLength(const char *name) noexcept; template - constexpr void setTypeInfo(const char* = getModelTypeName(), int = countFields()) noexcept { + constexpr void setTypeInfo() noexcept { + } + + template + constexpr void setTypeInfo(const char*) noexcept { + } + + template + constexpr void setTypeInfo(const char*, int) noexcept { } /** diff --git a/deps/ox/src/ox/std/bit.hpp b/deps/ox/src/ox/std/bit.hpp index ee5add2d..b23ee5ee 100644 --- a/deps/ox/src/ox/std/bit.hpp +++ b/deps/ox/src/ox/std/bit.hpp @@ -8,14 +8,26 @@ #pragma once -#if __cplusplus >= 202002L && defined(OX_USE_STDLIB) +#if defined(OX_USE_STDLIB) #include #endif +#include "defines.hpp" #include "memops.hpp" #include "types.hpp" #include "typetraits.hpp" +#if defined(OX_OS_Darwin) || !defined(OX_USE_STDLIB) +namespace std { + +template +constexpr typename ox::enable_if::type bit_cast(const From &src) noexcept { + return __builtin_bit_cast(To, src); +} + +} +#endif + namespace ox { template @@ -25,23 +37,16 @@ constexpr typename enable_if::type cbit_cast(Fro return dst; } -template -typename enable_if::type bit_cast(From src) noexcept { - To dst; - memcpy(&dst, &src, sizeof(src)); - return dst; -} - template [[nodiscard]] constexpr T rotl(T i, int shift) noexcept { constexpr auto bits = sizeof(i) * 8; return (i << static_cast(shift)) | (i >> (bits - static_cast(shift))); } -template -[[nodiscard]] constexpr T onMask(int bits = sizeof(T) << 3 /* *8 */) noexcept { +template +[[nodiscard]] constexpr T onMask(B bits = sizeof(T) << 3 /* *8 */) noexcept { T out = T(0); - for (auto i = 0; i < bits; i++) { + for (B i = 0; i < bits; i++) { out |= static_cast(1) << i; } return out; @@ -56,23 +61,3 @@ static_assert(onMask(3) == 7); static_assert(onMask(4) == 15); } - -namespace std { -#if __cplusplus >= 202002L && !defined(OX_USE_STDLIB) - -template -constexpr typename ox::enable_if::type bit_cast(const From &src) noexcept { - return __builtin_bit_cast(To, src); -} - -#elif __cplusplus < 202002L - -template -typename ox::enable_if::type bit_cast(From src) noexcept { - To dst = {}; - memcpy(&dst, &src, sizeof(src)); - return dst; -} - -#endif -} diff --git a/deps/ox/src/ox/std/fmt.hpp b/deps/ox/src/ox/std/fmt.hpp index 2d236964..81477eaa 100644 --- a/deps/ox/src/ox/std/fmt.hpp +++ b/deps/ox/src/ox/std/fmt.hpp @@ -42,7 +42,12 @@ constexpr const char *toCString(const BasicString &s) noexcept { } #if __has_include() -constexpr const char *toCString(const std::string &s) noexcept { +#ifdef OX_OS_Darwin +constexpr +#else +inline +#endif +const char *toCString(const std::string &s) noexcept { return s.c_str(); } #endif diff --git a/deps/ox/src/ox/std/hashmap.hpp b/deps/ox/src/ox/std/hashmap.hpp index 69b6e352..1aaeb56b 100644 --- a/deps/ox/src/ox/std/hashmap.hpp +++ b/deps/ox/src/ox/std/hashmap.hpp @@ -161,7 +161,7 @@ Result HashMap::at(K k) noexcept { auto p = access(m_pairs, k); if (!p) { AllocAlias v; - return {*bit_cast(&v), OxError(1)}; + return {*reinterpret_cast(&v), OxError(1)}; } return p->value; } @@ -190,7 +190,7 @@ Result HashMap::at(K k) const noexcept { auto p = access(m_pairs, k); if (!p) { AllocAlias v; - return {*bit_cast(&v), OxError(1)}; + return {*reinterpret_cast(&v), OxError(1)}; } return p->value; } diff --git a/deps/ox/src/ox/std/heapmgr.cpp b/deps/ox/src/ox/std/heapmgr.cpp index 7e61a91d..2b5315d5 100644 --- a/deps/ox/src/ox/std/heapmgr.cpp +++ b/deps/ox/src/ox/std/heapmgr.cpp @@ -25,29 +25,29 @@ static constexpr std::size_t alignedSize(T = {}) noexcept { return alignedSize(sizeof(T)); } -void HeapSegment::init(std::size_t maxSize = bit_cast(g_heapEnd)) noexcept { - this->size = maxSize - bit_cast(this); +void HeapSegment::init(std::size_t maxSize = std::bit_cast(g_heapEnd)) noexcept { + this->size = maxSize - std::bit_cast(this); this->inUse = false; } template T *HeapSegment::data() noexcept { - return bit_cast(bit_cast(this) + alignedSize(this)); + return reinterpret_cast(reinterpret_cast(this) + alignedSize(this)); } template T *HeapSegment::end() noexcept { const auto size = alignedSize(this) + alignedSize(this->size); - auto e = bit_cast(bit_cast(this) + size); - return bit_cast(e); + auto e = std::bit_cast(reinterpret_cast(this) + size); + return reinterpret_cast(e); } -void initHeap(char *heapBegin, char *heapEnd) noexcept { - g_heapBegin = bit_cast(heapBegin); - g_heapEnd = bit_cast(heapEnd); +void initHeap(void *heapBegin, void *heapEnd) noexcept { + g_heapBegin = reinterpret_cast(heapBegin); + g_heapEnd = reinterpret_cast(heapEnd); heapIdx = g_heapBegin; - heapIdx->size = bit_cast(heapEnd) - bit_cast(heapIdx); + heapIdx->size = std::bit_cast(heapEnd) - std::bit_cast(heapIdx); heapIdx->inUse = false; } diff --git a/deps/ox/src/ox/std/heapmgr.hpp b/deps/ox/src/ox/std/heapmgr.hpp index 81f44ffc..940b262c 100644 --- a/deps/ox/src/ox/std/heapmgr.hpp +++ b/deps/ox/src/ox/std/heapmgr.hpp @@ -26,7 +26,7 @@ struct HeapSegment { }; -void initHeap(char *heapBegin, char *heapEnd) noexcept; +void initHeap(void *heapBegin, void *heapEnd) noexcept; [[nodiscard]] void *malloc(std::size_t allocSize) noexcept; diff --git a/deps/ox/src/ox/std/random.cpp b/deps/ox/src/ox/std/random.cpp index 0e925d99..f123251a 100644 --- a/deps/ox/src/ox/std/random.cpp +++ b/deps/ox/src/ox/std/random.cpp @@ -11,27 +11,5 @@ namespace ox { -Random::Random() noexcept { - m_seed[0] = 540932923848; - m_seed[1] = 540932540932; -} - -Random::Random(RandomSeed seed) noexcept { - m_seed[0] = seed[0]; - m_seed[1] = seed[1]; -} - -uint64_t Random::gen() noexcept { - // An implementation of the Xoroshiro128+ algorithm - auto s0 = m_seed[0]; - auto s1 = m_seed[1]; - auto retval = s0 + s1; - - s1 ^= s0; - m_seed[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); - m_seed[1] = rotl(s1, 36); - - return retval; -} } diff --git a/deps/ox/src/ox/std/random.hpp b/deps/ox/src/ox/std/random.hpp index 3f1c0a09..1b4aa91e 100644 --- a/deps/ox/src/ox/std/random.hpp +++ b/deps/ox/src/ox/std/random.hpp @@ -15,16 +15,31 @@ namespace ox { using RandomSeed = uint64_t[2]; +// An implementation of the Xoroshiro128+ algorithm class OX_PACKED Random { private: - RandomSeed m_seed; + RandomSeed m_seed = {540932923848, 540932540932}; public: - Random() noexcept; + constexpr Random() noexcept = default; - explicit Random(RandomSeed seed) noexcept; + explicit constexpr Random(const RandomSeed &seed) noexcept; - uint64_t gen() noexcept; + constexpr uint64_t gen() noexcept; }; +constexpr Random::Random(const RandomSeed &seed) noexcept: m_seed{seed[0], seed[1]} { +} + +constexpr uint64_t Random::gen() noexcept { + auto s0 = m_seed[0]; + auto s1 = m_seed[1]; + const auto retval = s0 + s1; + // reseed for next number + s1 ^= s0; + m_seed[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); + m_seed[1] = rotl(s1, 36); + return retval; +} + } diff --git a/deps/ox/src/ox/std/stacktrace.cpp b/deps/ox/src/ox/std/stacktrace.cpp index 6de4af2f..29022109 100644 --- a/deps/ox/src/ox/std/stacktrace.cpp +++ b/deps/ox/src/ox/std/stacktrace.cpp @@ -26,35 +26,37 @@ namespace ox { #if defined(OX_USE_STDLIB) && __has_include() [[nodiscard]] -static Vector symbolicate([[maybe_unused]]const char **frames, [[maybe_unused]]std::size_t symbolsLen, [[maybe_unused]]const char *linePrefix) { +static auto symbolicate([[maybe_unused]]char **frames, + [[maybe_unused]]std::size_t symbolsLen, + [[maybe_unused]]const char *linePrefix) { + using StrT = BasicString<100>; + Vector out; #if __has_include() - Vector out; for (auto i = 0u; i < symbolsLen; ++i) { Dl_info info; if (dladdr(frames[i], &info) && info.dli_sname) { int status = -1; const auto name = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status); if (status == 0) { - out.emplace_back(sfmt("{}: {}", i, name)); + out.emplace_back(sfmt("{}: {}", i, name)); continue; } } - out.emplace_back(sfmt("{}", frames[i])); + out.emplace_back(sfmt("{}", frames[i])); } - return move(out); -#else // __has_include() - return {}; #endif // __has_include() + return move(out); } #endif // defined(OX_USE_STDLIB) && __has_include() void printStackTrace([[maybe_unused]]unsigned shave) noexcept { #if defined(OX_USE_STDLIB) && __has_include() - Vector frames(1000); + constexpr auto FrameCnt = 100; + Vector frames(FrameCnt); frames.resize(static_cast(backtrace(frames.data(), static_cast(frames.size())))); if (frames.size() - shave > 2) { const auto symbols = backtrace_symbols(frames.data() + shave, static_cast(frames.size() - shave)); - const auto symbolicatedStacktrace = symbolicate(bit_cast(frames.data() + shave), frames.size() - shave, "\t"); + const auto symbolicatedStacktrace = symbolicate(reinterpret_cast(frames.data() + shave), frames.size() - shave, "\t"); free(symbols); oxErr("Stacktrace:\n"); for (const auto &s : symbolicatedStacktrace) { diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index be6238aa..9c18d4c3 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -19,8 +19,8 @@ std::map> tests = { [] { ox::Buffer buff(ox::units::MB); ox::heapmgr::initHeap(&buff[0], &buff[buff.size()-1]); - auto a1 = ox::bit_cast(ox::heapmgr::malloc(5)); - auto a2 = ox::bit_cast(ox::heapmgr::malloc(5)); + auto a1 = static_cast(ox::heapmgr::malloc(5)); + auto a2 = static_cast(ox::heapmgr::malloc(5)); oxAssert(a1 >= &buff.front().value && a1 < &buff.back().value, "malloc is broken"); oxAssert(a2 >= &buff.front().value && a2 < &buff.back().value && a2 > a1 + 5, "malloc is broken"); return OxError(0); diff --git a/deps/ox/src/ox/std/trace.hpp b/deps/ox/src/ox/std/trace.hpp index 89b0ecea..ee414fd7 100644 --- a/deps/ox/src/ox/std/trace.hpp +++ b/deps/ox/src/ox/std/trace.hpp @@ -265,7 +265,7 @@ inline void logError(const char *file, int line, const char *fmt, const Error &e } else { trc << "Error:"; } - trc << sfmt>(fmt, static_cast(err)); + trc << ox::sfmt>(fmt, static_cast(err)); } }