Move make and destroy to luna
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2022-12-06 16:41:22 +01:00
parent 1fa99f4f64
commit a021e7a309
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 28 additions and 12 deletions

View File

@ -7,16 +7,4 @@ 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);
}
void dump_heap_usage();

28
luna/include/luna/Alloc.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include <luna/PlacementNew.h>
#include <luna/Result.h>
#include <luna/SafeArithmetic.h>
template <typename T> Result<T*> make()
{
T* const result = new T;
if (!result) return err(ENOMEM);
return result;
}
template <typename T> Result<T*> make_array(usize count)
{
T* const result = new T[count];
if (!result) return err(ENOMEM);
return result;
}
template <typename T> void destroy(T* item)
{
delete item;
}
template <typename T> void destroy_array(T* item)
{
delete[] item;
}