Heap: Avoid combines with blocks outside a range

This commit is contained in:
apio 2022-12-26 15:20:56 +01:00
parent a1eca479d5
commit 1c70ab5a1a
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -125,8 +125,14 @@ static Option<HeapBlock*> split(HeapBlock* block, usize size)
static Result<void> combine_forward(HeapBlock* block)
{
// This block ends a memory range, cannot be combined with blocks outside its range.
if (block->status & BLOCK_END_MEM) return {};
// The caller needs to ensure there is a next block.
HeapBlock* const next = heap.next(block).value();
// This block starts a memory range, cannot be combined with blocks outside its range.
if (next->status & BLOCK_START_MEM) return {};
heap.remove(next);
next->magic = BLOCK_DEAD;
@ -148,8 +154,13 @@ static Result<void> combine_forward(HeapBlock* block)
static Result<HeapBlock*> combine_backward(HeapBlock* block)
{
// This block starts a memory range, cannot be combined with blocks outside its range.
if (block->status & BLOCK_START_MEM) return block;
// The caller needs to ensure there is a last block.
HeapBlock* const last = heap.previous(block).value();
// This block ends a memory range, cannot be combined with blocks outside its range.
if (last->status & BLOCK_END_MEM) return block;
heap.remove(block);
block->magic = BLOCK_DEAD;