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
// 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<HeapBlock*> 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<HeapBlock*> split(HeapBlock* block, usize size)
static Result<void> 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<void> combine_forward(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;
block->magic = BLOCK_DEAD;
@ -176,8 +176,8 @@ Result<void*> 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<void*> 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<void*> 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<void*> krealloc(void* ptr, usize size)
Result<void*> 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);
}