Heap: Make const

This commit is contained in:
apio 2022-12-05 13:41:58 +01:00
parent 7cc139c3f7
commit 891320f7d3
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -38,7 +38,7 @@ static Result<HeapBlock*> allocate_pages(
// space is so huge, we can just start at a fairly large address and assume // 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. // 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); if (ptr) start_addr += (count * ARCH_PAGE_SIZE);
return (HeapBlock*)ptr; return (HeapBlock*)ptr;
} }
@ -95,13 +95,13 @@ static usize get_fair_offset_to_split_at(HeapBlock* block, usize min)
static Result<HeapBlock*> split(HeapBlock* block, usize size) static Result<HeapBlock*> split(HeapBlock* block, usize size)
{ {
usize available = space_available(block); // How much space can we steal from this block? const usize available = space_available(block); // How much space can we steal from this block?
usize old_size = const usize old_size =
block->full_size; // Save the old value of this variable since we are going to use it after modifying it 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. 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 block->full_size = offset; // shrink the old block to fit this offset
HeapBlock* new_block = offset_ptr(block, offset + sizeof(HeapBlock)); HeapBlock* new_block = offset_ptr(block, offset + sizeof(HeapBlock));
@ -120,7 +120,7 @@ static Result<HeapBlock*> split(HeapBlock* block, usize size)
static Result<void> combine_forward(HeapBlock* block) static Result<void> combine_forward(HeapBlock* block)
{ {
HeapBlock* next = block->next; HeapBlock* const next = block->next;
if (next == heap_end) heap_end = block; if (next == heap_end) heap_end = block;
next->magic = BLOCK_DEAD; next->magic = BLOCK_DEAD;
@ -145,7 +145,7 @@ static Result<void> combine_forward(HeapBlock* block)
static Result<HeapBlock*> combine_backward(HeapBlock* block) static Result<HeapBlock*> combine_backward(HeapBlock* block)
{ {
HeapBlock* last = block->last; HeapBlock* const last = block->last;
if (block == heap_end) heap_end = last; if (block == heap_end) heap_end = last;
block->magic = BLOCK_DEAD; block->magic = BLOCK_DEAD;
@ -176,8 +176,8 @@ Result<void*> kmalloc(usize size)
if (!heap_start) if (!heap_start)
{ {
usize pages = get_pages_for_allocation(size); const usize pages = get_pages_for_allocation(size);
auto* block = TRY(allocate_pages(pages)); HeapBlock* const block = TRY(allocate_pages(pages));
block->full_size = (pages * ARCH_PAGE_SIZE) - sizeof(HeapBlock); block->full_size = (pages * ARCH_PAGE_SIZE) - sizeof(HeapBlock);
block->magic = BLOCK_MAGIC; block->magic = BLOCK_MAGIC;
@ -293,7 +293,7 @@ Result<void*> krealloc(void* ptr, usize size)
return (void*)BLOCK_MAGIC; 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) if (block->magic != BLOCK_MAGIC)
{ {
@ -322,7 +322,7 @@ Result<void*> krealloc(void* ptr, usize size)
return ptr; 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); memcpy(new_ptr, ptr, block->req_size > size ? size : block->req_size);
TRY(kfree(ptr)); TRY(kfree(ptr));
@ -332,8 +332,8 @@ Result<void*> krealloc(void* ptr, usize size)
Result<void*> kcalloc(usize nmemb, usize size) Result<void*> kcalloc(usize nmemb, usize size)
{ {
// FIXME: Check for overflows. // FIXME: Check for overflows.
usize realsize = nmemb * size; const usize realsize = nmemb * size;
void* ptr = TRY(kmalloc(realsize)); void* const ptr = TRY(kmalloc(realsize));
return memset(ptr, 0, realsize); return memset(ptr, 0, realsize);
} }