From dc03e01cf897daadcab6fbbb0d96d5a51411efed Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 17 Jul 2019 20:12:28 -0500 Subject: [PATCH] [ox/ptrarith] Fix NodeBuffer::malloc to correctly update the old last's prev --- deps/ox/src/ox/ptrarith/nodebuffer.hpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/deps/ox/src/ox/ptrarith/nodebuffer.hpp b/deps/ox/src/ox/ptrarith/nodebuffer.hpp index 87a1db66..5a795e6a 100644 --- a/deps/ox/src/ox/ptrarith/nodebuffer.hpp +++ b/deps/ox/src/ox/ptrarith/nodebuffer.hpp @@ -242,7 +242,7 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size if (last.valid()) { addr = last.offset() + last.size(); } else { - // there is no first item, so this may be the first item + // there is no first item, so this must be the first item if (!m_header.firstItem) { oxTrace("ox::ptrarith::NodeBuffer::malloc") << "No first item, initializing."; m_header.firstItem = sizeof(m_header); @@ -255,6 +255,7 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size out->setSize(size); auto first = firstItem(); + auto oldLast = last; out->next = first.offset(); if (first.valid()) { first->prev = out.offset(); @@ -263,18 +264,22 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size return nullptr; } - auto last = lastItem(); - out->prev = last.offset(); - if (last.valid()) { - last->next = out.offset(); - } else { - oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "NodeBuffer malloc failed due to invalid last 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(); } else { oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "Unknown"; } + oxTrace("ox::ptrarith::NodeBuffer::malloc") << "Offset:" << out.offset(); return out; } oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "Insufficient space:" << fullSize << "needed," << available() << "available"; @@ -283,6 +288,7 @@ typename NodeBuffer::ItemPtr NodeBuffer::malloc(size template Error NodeBuffer::free(ItemPtr item) { + oxTrace("ox::ptrarith::NodeBuffer::free") << "offset:" << item.offset(); auto prev = this->prev(item); auto next = this->next(item); if (prev.valid() && next.valid()) {