Add new/delete-like functions but with Results

This commit is contained in:
apio 2022-11-20 15:48:08 +01:00
parent caf6d1563c
commit d54c882c63
2 changed files with 16 additions and 3 deletions

View File

@ -102,7 +102,7 @@ extern "C" [[noreturn]] void _start()
usize start = 0;
int* mem = (int*)kmalloc(sizeof(int)).release_value();
int* mem = make<int>().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)
{

View File

@ -1,7 +1,20 @@
#pragma once
#include <PlacementNew.h>
#include <Result.h>
Result<void*> kmalloc(usize size);
Result<void*> kcalloc(usize nmemb, usize size);
Result<void*> krealloc(void* ptr, usize size);
Result<void> kfree(void* ptr);
template <typename T> Result<T*> make()
{
void* ptr = TRY(kcalloc(1, sizeof(T)));
return new (ptr) T;
}
template <typename T> Result<void> destroy(T* item)
{
item->~T();
return kfree(item);
}