From 08c7647298bf2c843f02371006c985b6b22c15c2 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 8 Nov 2017 23:33:14 -0600 Subject: [PATCH] Fix issue with part of memory allocator getting optimized out --- src/nostalgia/core/gba/mem.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/nostalgia/core/gba/mem.cpp b/src/nostalgia/core/gba/mem.cpp index 3987a48b..4569dfe6 100644 --- a/src/nostalgia/core/gba/mem.cpp +++ b/src/nostalgia/core/gba/mem.cpp @@ -22,11 +22,10 @@ struct HeapSegment { } }; -static HeapSegment *_heapIdx = nullptr; +static HeapSegment *volatile _heapIdx = nullptr; void initHeap() { - _heapIdx = (HeapSegment*) MEM_WRAM_END; - _heapIdx--; + _heapIdx = ((HeapSegment*) MEM_WRAM_END) - 1; // set size to half of WRAM _heapIdx->size = (MEM_WRAM_END - MEM_WRAM_BEGIN) / 2; _heapIdx->next = nullptr; @@ -43,7 +42,7 @@ void *malloc(size_t allocSize) { const auto fullSize = allocSize + sizeof(HeapSegment); auto seg = _heapIdx; HeapSegment *prev = nullptr; - while (seg && seg->size < fullSize) { + while (seg && (seg->inUse || seg->size < fullSize)) { prev = seg; seg = seg->next; } @@ -80,7 +79,7 @@ void free(void *ptrIn) { // get ptr back down to the meta data auto *ptr = ((HeapSegment*) ptrIn) - 1; HeapSegment *prev = nullptr; - HeapSegment *current = _heapIdx; + auto current = _heapIdx; while (current && current != ptr) { prev = current; current = current->next;