Add new/delete-like functions but with Results
This commit is contained in:
parent
caf6d1563c
commit
d54c882c63
@ -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)
|
||||
{
|
||||
|
@ -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);
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user