Luna/libluna/include/luna/Alloc.h

29 lines
605 B
C
Raw Normal View History

2022-12-06 15:41:22 +00:00
#pragma once
#include <luna/Heap.h>
#include <luna/PlacementNew.h>
2022-12-06 15:41:22 +00:00
#include <luna/Result.h>
template <typename T, class... Args> [[nodiscard]] Result<T*> make(Args... args)
2022-12-06 15:41:22 +00:00
{
T* const result = (T*)TRY(malloc_impl(sizeof(T)));
new (result) T(args...);
2022-12-06 15:41:22 +00:00
return result;
}
template <typename T> [[nodiscard]] Result<T*> make_array(usize count)
2022-12-06 15:41:22 +00:00
{
T* const result = (T*)TRY(calloc_impl(count, sizeof(T)));
new (result) T[count];
2022-12-06 15:41:22 +00:00
return result;
}
template <typename T> void destroy(T* item)
{
delete item;
}
template <typename T> void destroy_array(T* item)
{
delete[] item;
2023-01-02 12:07:29 +00:00
}