From 0626c2a815d0c79190d87af5e82355ecb3419d83 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 1 Feb 2024 21:02:46 -0600 Subject: [PATCH 1/4] [ox/fs] Add comparison operator for FileAddress --- deps/ox/src/ox/fs/filesystem/filelocation.cpp | 26 +++++++++++++++++++ deps/ox/src/ox/fs/filesystem/filelocation.hpp | 11 +++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.cpp b/deps/ox/src/ox/fs/filesystem/filelocation.cpp index 417b44d4..88f9ee21 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.cpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.cpp @@ -88,6 +88,32 @@ FileAddress &FileAddress::operator=(FileAddress &&other) noexcept { return *this; } +bool FileAddress::operator==(FileAddress const&other) const noexcept { + if (m_type != other.m_type) { + auto const aIsPath = + m_type == FileAddressType::Path || m_type == FileAddressType::ConstPath; + auto const bIsPath = + other.m_type == FileAddressType::Path || other.m_type == FileAddressType::ConstPath; + if (!(aIsPath && bIsPath)) { + return false; + } + } + switch (m_type) { + case FileAddressType::ConstPath: + case FileAddressType::Path: { + auto const a = getPath(); + auto const b = other.getPath(); + return (other.m_type == FileAddressType::ConstPath || other.m_type == FileAddressType::Path) + && (a.value == b.value); + } + case FileAddressType::Inode: + return m_data.inode == other.m_data.inode; + case FileAddressType::None: + return true; + } + return true; +} + bool FileAddress::operator==(CRStringView path) const noexcept { auto [p, err] = getPath(); if (err) { diff --git a/deps/ox/src/ox/fs/filesystem/filelocation.hpp b/deps/ox/src/ox/fs/filesystem/filelocation.hpp index 587fb9bd..ded301e9 100644 --- a/deps/ox/src/ox/fs/filesystem/filelocation.hpp +++ b/deps/ox/src/ox/fs/filesystem/filelocation.hpp @@ -22,6 +22,9 @@ enum class FileAddressType: int8_t { Inode, }; +template +constexpr Error model(T *h, CommonPtrWith auto *fa) noexcept; + class FileAddress { template @@ -67,6 +70,8 @@ class FileAddress { FileAddress &operator=(FileAddress &&other) noexcept; + bool operator==(const FileAddress &other) const noexcept; + bool operator==(CRStringView path) const noexcept; [[nodiscard]] @@ -89,12 +94,12 @@ class FileAddress { } } - constexpr Result getPath() const noexcept { + constexpr Result getPath() const noexcept { switch (m_type) { case FileAddressType::Path: - return ox::StringView(m_data.path); + return ox::CStringView(m_data.path); case FileAddressType::ConstPath: - return ox::StringView(m_data.constPath); + return ox::CStringView(m_data.constPath); default: return OxError(1); } From e81d28a681c8e7d90866bf44cfa1dc9e1a91561e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 1 Feb 2024 21:06:35 -0600 Subject: [PATCH 2/4] [ox] Fix various preloader problems --- deps/ox/src/ox/model/modelvalue.hpp | 2 +- deps/ox/src/ox/preloader/platspecs.hpp | 1 + deps/ox/src/ox/preloader/preloader.hpp | 48 ++++++++++++++++++-------- deps/ox/src/ox/std/strops.hpp | 2 +- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/deps/ox/src/ox/model/modelvalue.hpp b/deps/ox/src/ox/model/modelvalue.hpp index 82892714..67bec3a5 100644 --- a/deps/ox/src/ox/model/modelvalue.hpp +++ b/deps/ox/src/ox/model/modelvalue.hpp @@ -760,7 +760,7 @@ constexpr std::size_t alignOf(const ModelValue &t) noexcept { size = PlatSpec::alignOf(t.get()); break; case ModelValue::Type::String: - size = PlatSpec::alignOf(t.get()); + size = alignOf(t.get()); break; case ModelValue::Type::Object: size = alignOf(t.get()); diff --git a/deps/ox/src/ox/preloader/platspecs.hpp b/deps/ox/src/ox/preloader/platspecs.hpp index 8f9c8397..dc5d6567 100644 --- a/deps/ox/src/ox/preloader/platspecs.hpp +++ b/deps/ox/src/ox/preloader/platspecs.hpp @@ -9,6 +9,7 @@ #pragma once #include +#include #include #include "alignmentcatcher.hpp" diff --git a/deps/ox/src/ox/preloader/preloader.hpp b/deps/ox/src/ox/preloader/preloader.hpp index 909b8c8d..f9fe6e82 100644 --- a/deps/ox/src/ox/preloader/preloader.hpp +++ b/deps/ox/src/ox/preloader/preloader.hpp @@ -14,10 +14,12 @@ #include #include #include +#include #include #include #include #include +#include #include "platspecs.hpp" @@ -105,9 +107,9 @@ class Preloader: public ModelHandlerBase, OpType::Reflect> { template constexpr ox::Error field(CRStringView, const T **val, std::size_t cnt) noexcept; - constexpr ox::Result startAlloc(std::size_t sz) noexcept; + constexpr ox::Result startAlloc(size_t sz, size_t align) noexcept; - constexpr ox::Result startAlloc(std::size_t sz, std::size_t restore) noexcept; + constexpr ox::Result startAlloc(size_t sz, size_t align, std::size_t restore) noexcept; constexpr ox::Error endAlloc() noexcept; @@ -169,14 +171,16 @@ constexpr ox::Error Preloader::field(CRStringView name, const T *val) } oxReturnError(pad(val)); if constexpr(ox::is_integral_v) { - //oxDebugf("Preloader::field(name, val): {}", name); return ox::serialize(&m_writer, PlatSpec::correctEndianness(*val)); } else if constexpr(ox::is_pointer_v) { - const PtrType a = startAlloc(sizeOf(*val), m_writer.tellp()) + PlatSpec::RomStart; + const PtrType a = startAlloc(sizeOf(val), alignOf(*val), m_writer.tellp()) + PlatSpec::RomStart; oxReturnError(field(name, *val)); oxReturnError(endAlloc()); return ox::serialize(&m_writer, PlatSpec::correctEndianness(a)); - } else if constexpr(ox::isVector_v || ox::is_same_v) { + } else if constexpr(ox::isVector_v) { + return fieldVector(name, val); + } else if constexpr(ox::is_same_v) { + val->types(); return fieldVector(name, val); } else { m_unionIdx.emplace_back(-1); @@ -222,6 +226,7 @@ constexpr ox::Error Preloader::field(CRStringView name, const ox::Arra if (!unionCheckAndIt()) { return {}; } + oxReturnError(pad(&(*val)[0])); // serialize the Array elements if constexpr(sz) { m_unionIdx.emplace_back(-1); @@ -239,27 +244,36 @@ constexpr ox::Error Preloader::field(CRStringView, const T **val, std: if (!unionCheckAndIt()) { return {}; } - // serialize the array - m_unionIdx.emplace_back(-1); - for (std::size_t i = 0; i < cnt; ++i) { - oxReturnError(this->interface()->field(nullptr, &val[i])); + if (cnt) { + oxReturnError(pad(*val)); + // serialize the array + m_unionIdx.emplace_back(-1); + for (std::size_t i = 0; i < cnt; ++i) { + oxReturnError(this->interface()->field(nullptr, &val[i])); + } + m_unionIdx.pop_back(); } - m_unionIdx.pop_back(); return {}; } template -constexpr ox::Result Preloader::startAlloc(std::size_t sz) noexcept { - oxRequire(a, ox::allocate(&m_writer, sz)); +constexpr ox::Result Preloader::startAlloc(size_t sz, size_t align) noexcept { m_allocStack.emplace_back(static_cast(m_writer.tellp())); + oxReturnError(m_writer.seekp(0, ox::ios_base::end)); + const auto padding = m_writer.tellp() % align; + oxRequireM(a, ox::allocate(&m_writer, sz + padding)); + a += padding; oxReturnError(m_writer.seekp(a)); return a; } template -constexpr ox::Result Preloader::startAlloc(std::size_t sz, std::size_t restore) noexcept { - oxRequire(a, ox::allocate(&m_writer, sz)); +constexpr ox::Result Preloader::startAlloc(std::size_t sz, size_t align, std::size_t restore) noexcept { m_allocStack.emplace_back(restore, ox::ios_base::beg); + oxReturnError(m_writer.seekp(0, ox::ios_base::end)); + const auto padding = m_writer.tellp() % align; + oxRequireM(a, ox::allocate(&m_writer, sz + padding)); + a += padding; oxReturnError(m_writer.seekp(a)); return a; } @@ -331,7 +345,11 @@ constexpr ox::Error Preloader::fieldVector( // serialize the Vector elements if (val->size()) { const auto sz = sizeOf(&(*val)[0]) * val->size(); - oxRequire(p, ox::allocate(&m_writer, sz)); + const auto align = alignOf((*val)[0]); + oxReturnError(m_writer.seekp(0, ox::ios_base::end)); + const auto padding = m_writer.tellp() % align; + oxRequireM(p, ox::allocate(&m_writer, sz + padding)); + p += padding; oxReturnError(m_writer.seekp(p)); m_unionIdx.emplace_back(-1); for (std::size_t i = 0; i < val->size(); ++i) { diff --git a/deps/ox/src/ox/std/strops.hpp b/deps/ox/src/ox/std/strops.hpp index 3528ddac..d3236d87 100644 --- a/deps/ox/src/ox/std/strops.hpp +++ b/deps/ox/src/ox/std/strops.hpp @@ -20,7 +20,7 @@ namespace ox { template [[nodiscard]] -constexpr ox::StringView substr(Str const&str, std::size_t pos) noexcept { +constexpr Str substr(Str const&str, std::size_t pos) noexcept { if (str.len() >= pos) { return Str(str.data() + pos, str.len() - pos); } From c55994f67d82824070fec934e0d01aa14d2dcb18 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 1 Feb 2024 21:06:53 -0600 Subject: [PATCH 3/4] [ox/std] Add Vector::capacity --- deps/ox/src/ox/std/vector.hpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index 87033fda..9ccd9aba 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -239,6 +239,9 @@ class Vector: detail::VectorAllocator { [[nodiscard]] constexpr Result back() const noexcept; + [[nodiscard]] + constexpr std::size_t capacity() const noexcept; + [[nodiscard]] constexpr std::size_t size() const noexcept; @@ -424,7 +427,7 @@ constexpr const T &Vector::operator[](std::size_t template constexpr Result Vector::at(size_t i) noexcept { - if (i < size()) { + if (i < size()) [[likely]] { return &operator[](i); } return OxError(1, "Vector: Invalid index"); @@ -432,7 +435,7 @@ constexpr Result Vector::at(size_t i) noexcep template constexpr Result Vector::at(size_t i) const noexcept { - if (i < size()) { + if (i < size()) [[likely]] { return &operator[](i); } return OxError(1, "Vector: Invalid index"); @@ -470,6 +473,11 @@ constexpr Result Vector::back() const n return &m_items[m_size - 1]; } +template +constexpr std::size_t Vector::capacity() const noexcept { + return m_cap; +} + template constexpr std::size_t Vector::size() const noexcept { return m_size; From 59fa39070f0c93be71060afb2584608e90cef84f Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 1 Feb 2024 21:07:42 -0600 Subject: [PATCH 4/4] [keel] Update pack for Preloader changes --- src/olympic/keel/include/keel/pack.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/olympic/keel/include/keel/pack.hpp b/src/olympic/keel/include/keel/pack.hpp index eb8baa52..3f7afe48 100644 --- a/src/olympic/keel/include/keel/pack.hpp +++ b/src/olympic/keel/include/keel/pack.hpp @@ -91,9 +91,9 @@ ox::Error preloadObj( oxRequireM(buff, romFs.read(path)); oxRequireM(obj, keel::readAsset(ts, buff)); if (obj.type()->preloadable) { - oxOutf("preloading {}\n", path); + oxOutf("preloading {} as a {}\n", path, obj.type()->typeName); // preload - oxRequire(a, pl.startAlloc(ox::sizeOf(&obj))); + oxRequire(a, pl.startAlloc(ox::sizeOf(&obj), ox::alignOf(obj))); auto const err = ox::preload(&pl, &obj); oxReturnError(pl.endAlloc()); oxReturnError(err);