From 891320f7d3a297578f943a018e91beae308ae993 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 5 Dec 2022 13:41:58 +0100 Subject: [PATCH] Heap: Make const --- kernel/src/memory/Heap.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/kernel/src/memory/Heap.cpp b/kernel/src/memory/Heap.cpp index dfd0ae5f..edc689f8 100644 --- a/kernel/src/memory/Heap.cpp +++ b/kernel/src/memory/Heap.cpp @@ -38,7 +38,7 @@ static Result allocate_pages( // space is so huge, we can just start at a fairly large address and assume // we'll never run into anything, but this will probably bite us in the future. { - void* ptr = (void*)TRY(MemoryManager::alloc_at(start_addr, count, MMU::ReadWrite | MMU::NoExecute)); + void* const ptr = (void*)TRY(MemoryManager::alloc_at(start_addr, count, MMU::ReadWrite | MMU::NoExecute)); if (ptr) start_addr += (count * ARCH_PAGE_SIZE); return (HeapBlock*)ptr; } @@ -95,13 +95,13 @@ static usize get_fair_offset_to_split_at(HeapBlock* block, usize min) static Result split(HeapBlock* block, usize size) { - usize available = space_available(block); // How much space can we steal from this block? - usize old_size = + const usize available = space_available(block); // How much space can we steal from this block? + const usize old_size = block->full_size; // Save the old value of this variable since we are going to use it after modifying it if (available < (size + sizeof(HeapBlock))) return err(0); // This error is not propagated. - usize offset = get_fair_offset_to_split_at(block, size + sizeof(HeapBlock)); + const usize offset = get_fair_offset_to_split_at(block, size + sizeof(HeapBlock)); block->full_size = offset; // shrink the old block to fit this offset HeapBlock* new_block = offset_ptr(block, offset + sizeof(HeapBlock)); @@ -120,7 +120,7 @@ static Result split(HeapBlock* block, usize size) static Result combine_forward(HeapBlock* block) { - HeapBlock* next = block->next; + HeapBlock* const next = block->next; if (next == heap_end) heap_end = block; next->magic = BLOCK_DEAD; @@ -145,7 +145,7 @@ static Result combine_forward(HeapBlock* block) static Result combine_backward(HeapBlock* block) { - HeapBlock* last = block->last; + HeapBlock* const last = block->last; if (block == heap_end) heap_end = last; block->magic = BLOCK_DEAD; @@ -176,8 +176,8 @@ Result kmalloc(usize size) if (!heap_start) { - usize pages = get_pages_for_allocation(size); - auto* block = TRY(allocate_pages(pages)); + const usize pages = get_pages_for_allocation(size); + HeapBlock* const block = TRY(allocate_pages(pages)); block->full_size = (pages * ARCH_PAGE_SIZE) - sizeof(HeapBlock); block->magic = BLOCK_MAGIC; @@ -293,7 +293,7 @@ Result krealloc(void* ptr, usize size) return (void*)BLOCK_MAGIC; } - HeapBlock* block = get_heap_block_for_pointer(ptr); + HeapBlock* const block = get_heap_block_for_pointer(ptr); if (block->magic != BLOCK_MAGIC) { @@ -322,7 +322,7 @@ Result krealloc(void* ptr, usize size) return ptr; } - void* new_ptr = TRY(kmalloc(size)); + void* const new_ptr = TRY(kmalloc(size)); memcpy(new_ptr, ptr, block->req_size > size ? size : block->req_size); TRY(kfree(ptr)); @@ -332,8 +332,8 @@ Result krealloc(void* ptr, usize size) Result kcalloc(usize nmemb, usize size) { // FIXME: Check for overflows. - usize realsize = nmemb * size; - void* ptr = TRY(kmalloc(realsize)); + const usize realsize = nmemb * size; + void* const ptr = TRY(kmalloc(realsize)); return memset(ptr, 0, realsize); }