From 8d53812b361919abe5b990ff2501a93b9c7b35b0 Mon Sep 17 00:00:00 2001 From: Edgar Su Date: Mon, 11 Oct 2021 23:36:05 +0800 Subject: [PATCH] memory: implememt sbrk() clone for dlmalloc --- memory/heap_break.c | 26 ++++++++++++++++++++++++++ memory/heap_break.h | 10 ++++++++++ 2 files changed, 36 insertions(+) create mode 100644 memory/heap_break.c create mode 100644 memory/heap_break.h 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);