From 317e714373e701d9b33e5a28e3f8bd0c0be655f3 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 25 Feb 2023 22:25:14 -0600 Subject: [PATCH] [ox/preloader] Cleanup --- deps/ox/src/ox/preloader/alignmentcatcher.hpp | 4 ++-- deps/ox/src/ox/preloader/platspecs.hpp | 21 ++++++++++++------- deps/ox/src/ox/preloader/preloader.hpp | 4 +++- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/deps/ox/src/ox/preloader/alignmentcatcher.hpp b/deps/ox/src/ox/preloader/alignmentcatcher.hpp index 88078256..cd9369bf 100644 --- a/deps/ox/src/ox/preloader/alignmentcatcher.hpp +++ b/deps/ox/src/ox/preloader/alignmentcatcher.hpp @@ -15,7 +15,7 @@ namespace ox { template [[nodiscard]] -constexpr std::size_t alignOf(const T &t) noexcept; +constexpr std::size_t alignOf(const T &v) noexcept; template [[nodiscard]] @@ -36,7 +36,7 @@ struct AlignmentCatcher: public ModelHandlerBase> { template constexpr ox::Error field(CRStringView, const T *val) noexcept { - if constexpr(ox::is_integer_v) { + if constexpr(ox::is_pointer_v || ox::is_integer_v) { biggestAlignment = ox::max(biggestAlignment, PlatSpec::alignOf(*val)); } else { biggestAlignment = ox::max(biggestAlignment, alignOf(*val)); diff --git a/deps/ox/src/ox/preloader/platspecs.hpp b/deps/ox/src/ox/preloader/platspecs.hpp index 11a4cff1..e5b672bd 100644 --- a/deps/ox/src/ox/preloader/platspecs.hpp +++ b/deps/ox/src/ox/preloader/platspecs.hpp @@ -21,11 +21,12 @@ struct NativePlatSpec { template [[nodiscard]] - static constexpr auto alignOf(const T &v) noexcept { + static constexpr std::size_t alignOf(const T &v) noexcept { if constexpr(ox::is_integral_v) { return alignof(T); } else if constexpr(ox::is_pointer_v) { - return PtrAlign; + PtrType p = 0; + return alignOf(p); } else { AlignmentCatcher c; oxAssert(model(c.interface(), &v), "Could not get alignment for type"); @@ -41,11 +42,17 @@ struct NativePlatSpec { template [[nodiscard]] -constexpr std::size_t alignOf(const T &t) noexcept { - return PlatSpec::alignOf(t); +constexpr std::size_t alignOf(const T &v) noexcept { + if constexpr(ox::is_integral_v) { + return alignof(T); + } else if constexpr(ox::is_pointer_v) { + typename PlatSpec::PtrType p = 0; + return PlatSpec::alignOf(p); + } else { + AlignmentCatcher c; + oxAssert(model(c.interface(), &v), "Could not get alignment for type"); + return c.biggestAlignment; + } } -template -constexpr auto alignOf_v = alignOf(static_cast(nullptr)); - } diff --git a/deps/ox/src/ox/preloader/preloader.hpp b/deps/ox/src/ox/preloader/preloader.hpp index 188d9b39..d38bf63e 100644 --- a/deps/ox/src/ox/preloader/preloader.hpp +++ b/deps/ox/src/ox/preloader/preloader.hpp @@ -33,6 +33,7 @@ class Preloader: public ModelHandlerBase> { private: using PtrType = typename PlatSpec::PtrType; + static constexpr auto PtrSize = sizeof(PtrType); class UnionIdxTracker { private: int m_unionIdx = -1; @@ -165,6 +166,7 @@ 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; @@ -336,7 +338,7 @@ constexpr ox::Error Preloader::fieldVector(CRStringView, const auto *v } // serialize the Vector oxReturnError(serialize(&m_writer, vecVal)); - m_ptrs.emplace_back(m_writer.tellp() - PlatSpec::PtrSize, vecVal.items); + m_ptrs.emplace_back(m_writer.tellp() - PtrSize, vecVal.items); return {}; }