From d54c882c63a96d6af6702bd79f053286a590a04d Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 20 Nov 2022 15:48:08 +0100 Subject: [PATCH] Add new/delete-like functions but with Results --- kernel/src/main.cpp | 4 ++-- kernel/src/memory/Heap.h | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) 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