diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index e328b55f..7b3faf7c 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -102,7 +102,7 @@ extern "C" [[noreturn]] void _start() usize start = 0; - int* mem = (int*)kmalloc(sizeof(int)).release_value(); + int* mem = make().release_value(); *(volatile int*)mem = 6; Serial::printf("Read %d from memory\n", *mem); @@ -110,7 +110,7 @@ extern "C" [[noreturn]] void _start() Serial::printf("Resized to %p\n", (void*)mem); - kfree(mem); + destroy(mem).release_value(); while (1) { diff --git a/kernel/src/memory/Heap.h b/kernel/src/memory/Heap.h index 3bb4a3bf..b7bf53e9 100644 --- a/kernel/src/memory/Heap.h +++ b/kernel/src/memory/Heap.h @@ -1,7 +1,20 @@ #pragma once +#include #include Result kmalloc(usize size); Result kcalloc(usize nmemb, usize size); Result krealloc(void* ptr, usize size); -Result kfree(void* ptr); \ No newline at end of file +Result kfree(void* ptr); + +template Result make() +{ + void* ptr = TRY(kcalloc(1, sizeof(T))); + return new (ptr) T; +} + +template Result destroy(T* item) +{ + item->~T(); + return kfree(item); +} \ No newline at end of file