From c30ef720c4062e8318974c9b203274132f689e00 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 3 Nov 2019 16:44:22 -0600 Subject: [PATCH] [ox/ptrarith] Fix Ptr type's derefernce operator --- deps/ox/src/ox/ptrarith/nodebuffer.hpp | 70 ++++++++++++++------------ deps/ox/src/ox/ptrarith/ptr.hpp | 4 +- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/deps/ox/src/ox/ptrarith/nodebuffer.hpp b/deps/ox/src/ox/ptrarith/nodebuffer.hpp index a5752a1a..5392991f 100644 --- a/deps/ox/src/ox/ptrarith/nodebuffer.hpp +++ b/deps/ox/src/ox/ptrarith/nodebuffer.hpp @@ -93,9 +93,10 @@ class __attribute__((packed)) NodeBuffer { Header m_header; public: - NodeBuffer() = default; + NodeBuffer() { + } - NodeBuffer(const NodeBuffer &other); + NodeBuffer(const NodeBuffer &other, size_t size); explicit NodeBuffer(size_t size); @@ -168,11 +169,13 @@ class __attribute__((packed)) NodeBuffer { template NodeBuffer::NodeBuffer(size_t size) { m_header.size = size; + oxTrace("ox::NodeBuffer::constructor") << m_header.firstItem; } template -NodeBuffer::NodeBuffer(const NodeBuffer &other) { - ox_memcpy(this, &other, other.size()); +NodeBuffer::NodeBuffer(const NodeBuffer &other, size_t size) { + oxTrace("ox::ptrarith::NodeBuffer::copy") << "other.m_header.firstItem:" << other.m_header.firstItem; + ox_memcpy(this, &other, size); } template @@ -270,39 +273,44 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size oxTrace("ox::ptrarith::NodeBuffer::malloc") << "No first item, initializing."; m_header.firstItem = sizeof(m_header); addr = m_header.firstItem; - } - } - auto out = ItemPtr(this, m_header.size, addr, fullSize); - if (out.valid()) { - new (out) Item; - out->setSize(size); - - auto first = firstItem(); - auto oldLast = last; - out->next = first.offset(); - if (first.valid()) { - first->prev = out.offset(); } else { - oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "NodeBuffer malloc failed due to invalid first element pointer."; + oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "NodeBuffer is in invalid state."; return nullptr; } - - if (oldLast.valid()) { - out->prev = oldLast.offset(); - oldLast->next = out.offset(); - } else { // check to see if this is the first allocation - if (out.offset() != first.offset()) { - // if this is not the first allocation, there should be an oldLast - oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "NodeBuffer malloc failed due to invalid last element pointer."; - return nullptr; - } - out->prev = out.offset(); - } - m_header.bytesUsed += out.size(); - } else { + } + oxTrace("ox::ptrarith::NodeBuffer::malloc") << "buffer size:" << m_header.size + << ";addr:" << addr + << ";fullSize:" << fullSize; + auto out = ItemPtr(this, m_header.size, addr, fullSize); + if (!out.valid()) { oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "Unknown"; return nullptr; } + new (out) Item; + out->setSize(size); + + auto first = firstItem(); + auto oldLast = last; + out->next = first.offset(); + if (first.valid()) { + first->prev = out.offset(); + } else { + oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "NodeBuffer malloc failed due to invalid first element pointer."; + return nullptr; + } + + if (oldLast.valid()) { + out->prev = oldLast.offset(); + oldLast->next = out.offset(); + } else { // check to see if this is the first allocation + if (out.offset() != first.offset()) { + // if this is not the first allocation, there should be an oldLast + oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "NodeBuffer malloc failed due to invalid last element pointer."; + return nullptr; + } + out->prev = out.offset(); + } + m_header.bytesUsed += out.size(); oxTrace("ox::ptrarith::NodeBuffer::malloc") << "Offset:" << out.offset(); return out; } diff --git a/deps/ox/src/ox/ptrarith/ptr.hpp b/deps/ox/src/ox/ptrarith/ptr.hpp index 81ad934b..ec5cd386 100644 --- a/deps/ox/src/ox/ptrarith/ptr.hpp +++ b/deps/ox/src/ox/ptrarith/ptr.hpp @@ -163,14 +163,14 @@ template inline const T &Ptr::operator*() const { oxAssert(m_validated, "Unvalidated pointer dereference. (ox::fs::Ptr::operator*())"); oxAssert(valid(), "Invalid pointer dereference. (ox::fs::Ptr::operator*())"); - return *reinterpret_cast(this); + return *reinterpret_cast(m_dataStart + m_itemOffset); } template inline T &Ptr::operator*() { oxAssert(m_validated, "Unvalidated pointer dereference. (ox::fs::Ptr::operator*())"); oxAssert(valid(), "Invalid pointer dereference. (ox::fs::Ptr::operator*())"); - return *reinterpret_cast(this); + return *reinterpret_cast(m_dataStart + m_itemOffset); } template