From cce511c9c39043336b66735fc6801d165dba39a3 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Sun, 12 Feb 2023 17:45:42 +0100 Subject: [PATCH] feature (kernel): Implemented global kernel heap --- kernel/inc/utils/memory.h | 9 ++++++--- kernel/src/kmain.c | 29 +++-------------------------- kernel/src/utils/memory.c | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/kernel/inc/utils/memory.h b/kernel/inc/utils/memory.h index 76996e7..da2c178 100644 --- a/kernel/inc/utils/memory.h +++ b/kernel/inc/utils/memory.h @@ -18,8 +18,11 @@ #include "stdtypes.h" -void memory_copy (void* source, void* destination, uint32_t num); -void memory_set (void* destination, uint8_t data, uint32_t num); -bool memory_compare (void* a, void* b, uint32_t num); +void memory_copy (void* source, void* destination, uint32_t num); +void memory_set (void* destination, uint8_t data, uint32_t num); +bool memory_compare (void* a, void* b, uint32_t num); +void* memory_allocate (uint64_t size); +void memory_free (void* address); +void memory_allocator_init (void* base); #endif //NOX_MEMORY_H diff --git a/kernel/src/kmain.c b/kernel/src/kmain.c index 36bc10d..8b16783 100644 --- a/kernel/src/kmain.c +++ b/kernel/src/kmain.c @@ -15,14 +15,13 @@ #include "utils/logger.h" #include "utils/core.h" +#include "utils/memory.h" #include "boot/boot_info.h" #include "platform/gdt.h" #include "platform/interrupts.h" #include "mm/page_frame.h" #include "mm/page_map.h" -#include "mm/heap.h" - void limine_terminal_print(boot_info_T* boot_info, string_t string) { boot_info->terminal->write(boot_info->terminal->terminals[0], string, string_length(string)); } @@ -32,6 +31,7 @@ void kernel_init(boot_info_T* boot_info) { pframe_manager_init(boot_info); idt_init(); paging_init(); + memory_allocator_init((void*)0x100000000000); } void kmain(boot_info_T boot_info) { @@ -41,30 +41,7 @@ void kmain(boot_info_T boot_info) { kernel_init(&boot_info); - heap_T test_heap = heap_create((void*)0x100000000000); - void *debug1, *debug2, *debug3, *debug4 = NULL; - - heap_dump_segments(&test_heap); - - debug1 = heap_memory_allocate(&test_heap, 16); - heap_dump_segments(&test_heap); - - debug2 = heap_memory_allocate(&test_heap, 16); - heap_dump_segments(&test_heap); - - debug3 = heap_memory_allocate(&test_heap, 0x3000); - heap_dump_segments(&test_heap); - - heap_memory_free(&test_heap, debug2); - heap_dump_segments(&test_heap); - - heap_memory_free(&test_heap, debug3); - heap_dump_segments(&test_heap); - - debug4 = heap_memory_allocate(&test_heap, 16); - heap_dump_segments(&test_heap); - - heap_destruct(&test_heap); + void* debug = memory_allocate(1312); CORE_HALT_FOREVER } diff --git a/kernel/src/utils/memory.c b/kernel/src/utils/memory.c index 8e87ad0..b4f5173 100644 --- a/kernel/src/utils/memory.c +++ b/kernel/src/utils/memory.c @@ -14,6 +14,9 @@ */ #include "utils/memory.h" +#include "mm/heap.h" + +heap_T g_kernel_heap; void memory_copy (void* source, void* destination, uint32_t num) { uint8_t* src_8 = (uint8_t*)source; @@ -44,3 +47,17 @@ bool memory_compare (void* a, void* b, uint32_t num) { return true; } + +void* memory_allocate(uint64_t size) { + return heap_memory_allocate(&g_kernel_heap, size); +} + +void memory_free(void* address) { + return heap_memory_free(&g_kernel_heap, address); +} + +void memory_allocator_init(void* base) { + if (g_kernel_heap.start != NULL) { return; } + + heap_init(&g_kernel_heap, base); +} \ No newline at end of file