From d48eb85d1d8205c8b0dc68f459e72675cc1a656e Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 6 Dec 2022 19:05:00 +0100 Subject: [PATCH] Heap: Avoid magic numbers --- kernel/src/memory/Heap.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/src/memory/Heap.cpp b/kernel/src/memory/Heap.cpp index d0f65e5d..703b7922 100644 --- a/kernel/src/memory/Heap.cpp +++ b/kernel/src/memory/Heap.cpp @@ -29,6 +29,8 @@ struct HeapBlock : DoublyLinkedListNode static_assert(sizeof(HeapBlock) == 48UL); +static const isize HEAP_BLOCK_SIZE = 48; + static DoublyLinkedList heap; static Result allocate_pages(usize count) @@ -64,14 +66,15 @@ static usize space_available(HeapBlock* block) return block->full_size - block->req_size; } +// The heap block is stored right behind a memory block. static HeapBlock* get_heap_block_for_pointer(void* ptr) { - return (HeapBlock*)offset_ptr(ptr, -48); + return (HeapBlock*)offset_ptr(ptr, -HEAP_BLOCK_SIZE); } static void* get_pointer_from_heap_block(HeapBlock* block) { - return (void*)offset_ptr(block, 48); + return (void*)offset_ptr(block, HEAP_BLOCK_SIZE); } static usize get_fair_offset_to_split_at(HeapBlock* block, usize min)