Fix heap allocator to not pass the meta data portion as output
This commit is contained in:
parent
26f1993f3d
commit
9f2441bc82
@ -36,21 +36,22 @@ using namespace nostalgia::core;
|
|||||||
void *operator new(size_t sz) {
|
void *operator new(size_t sz) {
|
||||||
// add space for heap segment header data
|
// add space for heap segment header data
|
||||||
sz += sizeof(HeapSegment);
|
sz += sizeof(HeapSegment);
|
||||||
auto out = _heapIdx;
|
auto seg = _heapIdx;
|
||||||
while (out && out->size < sz) {
|
while (seg && seg->size < sz) {
|
||||||
out = out->next;
|
seg = seg->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
// panic if the allocation failed
|
// panic if the allocation failed
|
||||||
if (out == nullptr) {
|
if (seg == nullptr) {
|
||||||
panic("Heap allocation failed");
|
panic("Heap allocation failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
// update size for the heap segment now that it is to be considered
|
// update size for the heap segment now that it is to be considered
|
||||||
// allocated
|
// allocated
|
||||||
out->size = sz;
|
seg->size = sz;
|
||||||
out->next = (HeapSegment*) (((uint8_t*) out) + sz);
|
seg->next = (HeapSegment*) (((uint8_t*) seg) + sz);
|
||||||
out->inUse = true;
|
seg->inUse = true;
|
||||||
|
auto out = seg + 1;
|
||||||
|
|
||||||
auto hs = *_heapIdx;
|
auto hs = *_heapIdx;
|
||||||
hs.size -= sz;
|
hs.size -= sz;
|
||||||
@ -64,7 +65,9 @@ void *operator new(size_t sz) {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator delete(void *ptr) {
|
void operator delete(void *ptrIn) {
|
||||||
|
// get ptr back down to the meta data
|
||||||
|
auto *ptr = ((HeapSegment*) ptrIn) - 1;
|
||||||
HeapSegment *prev = nullptr;
|
HeapSegment *prev = nullptr;
|
||||||
HeapSegment *current = _heapIdx;
|
HeapSegment *current = _heapIdx;
|
||||||
while (current && current != ptr) {
|
while (current && current != ptr) {
|
||||||
|
Loading…
Reference in New Issue
Block a user