[ox/ptrarith] Fix NodeBuffer::compact to correclty update dest.prev

This commit is contained in:
Gary Talent 2019-07-18 20:31:51 -05:00
parent 69f918664d
commit d55089b582

View File

@ -278,6 +278,7 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
m_header.bytesUsed += out.size(); m_header.bytesUsed += out.size();
} else { } else {
oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "Unknown"; oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "Unknown";
return nullptr;
} }
oxTrace("ox::ptrarith::NodeBuffer::malloc") << "Offset:" << out.offset(); oxTrace("ox::ptrarith::NodeBuffer::malloc") << "Offset:" << out.offset();
return out; return out;
@ -293,8 +294,8 @@ Error NodeBuffer<size_t, Item>::free(ItemPtr item) {
auto next = this->next(item); auto next = this->next(item);
if (prev.valid() && next.valid()) { if (prev.valid() && next.valid()) {
if (next != item) { if (next != item) {
prev->next = next.offset(); prev->next = next;
next->prev = prev.offset(); next->prev = prev;
if (item.offset() == m_header.firstItem) { if (item.offset() == m_header.firstItem) {
m_header.firstItem = next; m_header.firstItem = next;
} }
@ -319,8 +320,11 @@ Error NodeBuffer<size_t, Item>::free(ItemPtr item) {
template<typename size_t, typename Item> template<typename size_t, typename Item>
Error NodeBuffer<size_t, Item>::setSize(size_t size) { Error NodeBuffer<size_t, Item>::setSize(size_t size) {
oxTrace("ox::ptrarith::NodeBuffer::setSize") << size;
auto last = lastItem(); auto last = lastItem();
if ((last.valid() && last.end() >= size) || size < sizeof(m_header)) { auto end = last.valid() ? last.end() : sizeof(m_header);
oxTrace("ox::ptrarith::NodeBuffer::setSize") << "end:" << end;
if (end > size) {
return OxError(1); return OxError(1);
} else { } else {
m_header.size = size; m_header.size = size;
@ -358,7 +362,7 @@ void NodeBuffer<size_t, Item>::compact(F cb) {
ox_memcpy(dest, src, src.size()); ox_memcpy(dest, src, src.size());
cb(src, dest); cb(src, dest);
// update surrounding nodes // update surrounding nodes
auto prev = ptr(dest->next); auto prev = ptr(dest->prev);
if (prev.valid()) { if (prev.valid()) {
prev->next = dest; prev->next = dest;
} }