diff --git a/memory/heap_break.c b/memory/heap_break.c new file mode 100644 index 0000000..ca8dcf9 --- /dev/null +++ b/memory/heap_break.c @@ -0,0 +1,26 @@ + +#include "heap_break.h" +#include "memory.h" +#include "paging_internal.h" +#include "../runtime/stdio.h" + + +static uintptr_t heapBreak = KERNEL_HEAP_VIRTUAL, pageBreak = KERNEL_HEAP_VIRTUAL; + +void *memory_AddBreak(intptr_t inc) { + if (heapBreak + inc < KERNEL_HEAP_VIRTUAL) + return (void *)-1; + + heapBreak += inc; + + if (heapBreak > pageBreak) { + // we need more pages + int pageCount = roundUpToPageCount(heapBreak - pageBreak); + io_Printf("memory_AddBreak(): 0x%llx -> 0x%llx, add %d, allocating %d pages\n", heapBreak - inc, heapBreak, inc, pageCount); + paging_map_PageAllocated(pageBreak, pageCount, MAP_PROT_READ | MAP_PROT_WRITE); + pageBreak += SYSTEM_PAGE_SIZE * pageCount; + } else + io_Printf("memory_AddBreak(): 0x%llx -> 0x%llx, add %d\n", heapBreak - inc, heapBreak, inc); + + return (void *)(heapBreak - inc); +} diff --git a/memory/heap_break.h b/memory/heap_break.h new file mode 100644 index 0000000..4a61db4 --- /dev/null +++ b/memory/heap_break.h @@ -0,0 +1,10 @@ +#pragma once + +#include "../main.h" +#include + + +// memory_AddBreak is a sbrk() clone, increasing the kernel heap size mapped at KERNEL_HEAP_VIRTUAL. +// TODO It does not handle negative increments well (yet), as it does not unmap freed heap pages. +// I don't know if I should actually implement this, given it's a rather simple fix. +void *memory_AddBreak(intptr_t increment);