Heap: Avoid combines with blocks outside a range
This commit is contained in:
parent
a1eca479d5
commit
1c70ab5a1a
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user