From 6815402ba15a5d55e7e97371a667c30324b168c3 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 14 Oct 2017 20:58:06 -0500 Subject: [PATCH] Fix heap delete to work with the moving of the HeapSegment header --- src/nostalgia/core/gba/mem.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/nostalgia/core/gba/mem.cpp b/src/nostalgia/core/gba/mem.cpp index db8c6776..45a283c2 100644 --- a/src/nostalgia/core/gba/mem.cpp +++ b/src/nostalgia/core/gba/mem.cpp @@ -16,6 +16,10 @@ struct HeapSegment { size_t size; uint8_t inUse; HeapSegment *next; + + uint8_t *end() { + return ((uint8_t*) this) + this->size; + } }; static HeapSegment *_heapIdx = nullptr; @@ -84,6 +88,12 @@ void operator delete(void *ptrIn) { // ptr was found as a valid memory allocation, deallocate it if (current) { + // move header back to end of segment + auto newCurrent = (HeapSegment*) current->end() - sizeof(HeapSegment); + *newCurrent = *current; + current = newCurrent; + prev->next = current; + // mark as not in use current->inUse = false;