From 461e3d61efc9676801ae4b8f13b7ec6866bdb53f Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 5 Dec 2022 17:40:22 -0600 Subject: [PATCH] [ox/preloader] Add ptr handling --- deps/ox/src/ox/preloader/preloader.hpp | 44 ++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/deps/ox/src/ox/preloader/preloader.hpp b/deps/ox/src/ox/preloader/preloader.hpp index 293cf37b..969e2b4e 100644 --- a/deps/ox/src/ox/preloader/preloader.hpp +++ b/deps/ox/src/ox/preloader/preloader.hpp @@ -32,6 +32,7 @@ class Preloader: public ModelHandlerBase> { friend constexpr ox::Error preload(Preloader *pl, ox::CommonPtrWith auto *obj) noexcept; private: + using PtrType = typename PlatSpec::PtrType; class UnionIdxTracker { private: int m_unionIdx = -1; @@ -55,6 +56,14 @@ class Preloader: public ModelHandlerBase> { }; ox::Vector m_ptrs; ox::Vector m_unionIdx = {{}}; + class AllocStackItem { + public: + PtrType restore = 0; + ox::ios_base::seekdir seekdir = ox::ios_base::end; + constexpr AllocStackItem(PtrType pRestore, ox::ios_base::seekdir pSeekdir = ox::ios_base::end) noexcept: + restore(pRestore), seekdir(pSeekdir) {} + }; + ox::Vector m_allocStack; constexpr Preloader() noexcept: m_writer(&m_buff) {} @@ -86,7 +95,9 @@ class Preloader: public ModelHandlerBase> { template constexpr ox::Error field(CRStringView, const T **val, std::size_t cnt) noexcept; - constexpr ox::Error startAlloc(std::size_t sz) noexcept; + constexpr ox::Result startAlloc(std::size_t sz) noexcept; + + constexpr ox::Result startAlloc(std::size_t sz, std::size_t restore) noexcept; constexpr ox::Error endAlloc() noexcept; @@ -156,7 +167,10 @@ constexpr ox::Error Preloader::field(CRStringView name, const T *val) if constexpr(ox::is_integral_v) { return ox::serialize(&m_writer, PlatSpec::correctEndianness(*val)); } else if constexpr(ox::is_pointer_v) { - return {}; + const PtrType a = startAlloc(sizeOf(*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) { return fieldVector(name, val); } else { @@ -188,7 +202,7 @@ constexpr ox::Error Preloader::field(CRStringView, const ox::BasicStri } else { a = restore; } - vecVal.items = PlatSpec::correctEndianness(static_cast(a) + PlatSpec::RomStart); + vecVal.items = PlatSpec::correctEndianness(static_cast(a) + PlatSpec::RomStart); oxReturnError(m_writer.seekp(a)); oxReturnError(m_writer.write(val->data(), sz)); oxReturnError(m_writer.seekp(restore)); @@ -230,14 +244,30 @@ constexpr ox::Error Preloader::field(CRStringView, const T **val, std: } template -constexpr ox::Error Preloader::startAlloc(std::size_t sz) noexcept { - oxRequire(p, ox::allocate(&m_writer, sz)); - return m_writer.seekp(p); +constexpr ox::Result Preloader::startAlloc(std::size_t sz) noexcept { + oxRequire(a, ox::allocate(&m_writer, sz)); + m_allocStack.emplace_back(m_writer.tellp()); + 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)); + m_allocStack.emplace_back(restore, ox::ios_base::beg); + oxReturnError(m_writer.seekp(a)); + return a; } template constexpr ox::Error Preloader::endAlloc() noexcept { - return m_writer.seekp(0, ox::ios_base::end); + if (m_allocStack.empty()) { + return m_writer.seekp(0, ox::ios_base::end); + } + const auto &si = m_allocStack.back().unwrap(); + oxReturnError(m_writer.seekp(si.restore, si.seekdir)); + m_allocStack.pop_back(); + return {}; } template