memory: implememt sbrk() clone for dlmalloc

This commit is contained in:
Edgar Su 2021-10-11 23:36:05 +08:00
parent cd287fe8cb
commit 8d53812b36
2 changed files with 36 additions and 0 deletions

26
memory/heap_break.c Normal file
View File

@ -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);
}

10
memory/heap_break.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include "../main.h"
#include <stdint.h>
// 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);