[ox/ptrarith] Fix NodeBuffer::malloc to correctly update the old last's prev

This commit is contained in:
Gary Talent 2019-07-17 20:12:28 -05:00
parent ffce1dc4df
commit dc03e01cf8

View File

@ -242,7 +242,7 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
if (last.valid()) { if (last.valid()) {
addr = last.offset() + last.size(); addr = last.offset() + last.size();
} else { } 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) { if (!m_header.firstItem) {
oxTrace("ox::ptrarith::NodeBuffer::malloc") << "No first item, initializing."; oxTrace("ox::ptrarith::NodeBuffer::malloc") << "No first item, initializing.";
m_header.firstItem = sizeof(m_header); m_header.firstItem = sizeof(m_header);
@ -255,6 +255,7 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
out->setSize(size); out->setSize(size);
auto first = firstItem(); auto first = firstItem();
auto oldLast = last;
out->next = first.offset(); out->next = first.offset();
if (first.valid()) { if (first.valid()) {
first->prev = out.offset(); first->prev = out.offset();
@ -263,18 +264,22 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
return nullptr; return nullptr;
} }
auto last = lastItem(); if (oldLast.valid()) {
out->prev = last.offset(); out->prev = oldLast.offset();
if (last.valid()) { oldLast->next = out.offset();
last->next = out.offset(); } else { // check to see if this is the first allocation
} else { if (out.offset() != first.offset()) {
oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "NodeBuffer malloc failed due to invalid last element pointer."; // if this is not the first allocation, there should be an oldLast
return nullptr; 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(); m_header.bytesUsed += out.size();
} else { } else {
oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "Unknown"; oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "Unknown";
} }
oxTrace("ox::ptrarith::NodeBuffer::malloc") << "Offset:" << out.offset();
return out; return out;
} }
oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "Insufficient space:" << fullSize << "needed," << available() << "available"; oxTrace("ox::ptrarith::NodeBuffer::malloc::fail") << "Insufficient space:" << fullSize << "needed," << available() << "available";
@ -283,6 +288,7 @@ typename NodeBuffer<size_t, Item>::ItemPtr NodeBuffer<size_t, Item>::malloc(size
template<typename size_t, typename Item> template<typename size_t, typename Item>
Error NodeBuffer<size_t, Item>::free(ItemPtr item) { Error NodeBuffer<size_t, Item>::free(ItemPtr item) {
oxTrace("ox::ptrarith::NodeBuffer::free") << "offset:" << item.offset();
auto prev = this->prev(item); auto prev = this->prev(item);
auto next = this->next(item); auto next = this->next(item);
if (prev.valid() && next.valid()) { if (prev.valid() && next.valid()) {