diff options
| author | Aleksa Vuckovic <aleksav013@gmail.com> | 2022-09-08 14:37:47 +0200 |
|---|---|---|
| committer | Aleksa Vuckovic <aleksav013@gmail.com> | 2022-09-08 14:38:29 +0200 |
| commit | 82e9f02aef1ea1a6588234ee58e3625e3561005f (patch) | |
| tree | 80214489336fa2cc1c615bd63b133934832a5ba4 /kernel/src/mem/heap.c | |
| parent | 1effe3b9ac0bb1a819f6ae5ebb8438e60b77f9a2 (diff) | |
higher half in x86_64
kernel is now located at -2GB
heap blocks are aligned on block size
paging will allocate new page tables on heap
Diffstat (limited to 'kernel/src/mem/heap.c')
| -rw-r--r-- | kernel/src/mem/heap.c | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/kernel/src/mem/heap.c b/kernel/src/mem/heap.c index 229933b..e37e003 100644 --- a/kernel/src/mem/heap.c +++ b/kernel/src/mem/heap.c @@ -34,12 +34,12 @@ void kheap_add_block(kheap_t* kheap, uint64_t addr, uint32_t size, uint32_t bsiz bm[i] = 0; } - bcnt = upper_div(bcnt, bsize); - for (size_t i = 0; i < bcnt; i++) { + uint32_t bcnt_used = upper_div((bcnt + (uint32_t)sizeof(kheapblock_t)), bsize); + for (size_t i = 0; i < bcnt_used; i++) { bm[i] = 5; } - kheapblock->used = bcnt; + kheapblock->used = bcnt_used; } void* kheap_alloc(kheap_t* kheap, uint32_t size) @@ -52,6 +52,13 @@ void* kheap_alloc(kheap_t* kheap, uint32_t size) continue; } + // use heap with bsize 4096 just for that block size + bool ind1 = ((size % 4096) == 0); + bool ind2 = (kheapblock->bsize == 4096); + if (ind1 + ind2 == 1) { + continue; + } + uint32_t bcnt = kheapblock->size / kheapblock->bsize; uint32_t bneed = upper_div(size, kheapblock->bsize); uint8_t* bm = (uint8_t*)&kheapblock[1]; @@ -81,7 +88,7 @@ void* kheap_alloc(kheap_t* kheap, uint32_t size) kheapblock->used += bneed; - return (void*)(i * kheapblock->bsize + (uintptr_t)&kheapblock[1]); + return (void*)(i * kheapblock->bsize + (uintptr_t)&kheapblock[0]); } } printf("Error: there is no remaining memory in kheap\n"); @@ -118,10 +125,6 @@ void kheap_free(kheap_t* kheap, void* pointer) void init_heap() { kheap_init(&main_kheap); - - // allocate pages for kheap - for (size_t i = 0; i < upper_div(HEAP_SIZE, PAGE_SIZE); i++) - map_addr(HEAP_VMEM_ADDR + i * PAGE_SIZE, HEAP_PMEM_ADDR + i * PAGE_SIZE, FLAG_PRESENT + FLAG_WRITABLE + FLAG_HUGE); - - kheap_add_block(&main_kheap, HEAP_VMEM_ADDR, HEAP_SIZE, HEAP_BLOCK_SIZE); + kheap_add_block(&main_kheap, HEAP1_VMEM_ADDR, HEAP1_SIZE, HEAP1_BLOCK_SIZE); + kheap_add_block(&main_kheap, HEAP2_VMEM_ADDR, HEAP2_SIZE, HEAP2_BLOCK_SIZE); } |
