Luna/luna/include/luna/Alloc.h
2023-01-02 13:07:29 +01:00

48 lines
1011 B
C++

#pragma once
#include <luna/Result.h>
namespace std
{
struct nothrow_t
{
explicit nothrow_t() = default;
};
extern const nothrow_t nothrow;
enum class align_val_t : usize
{
};
};
[[nodiscard]] void* raw_malloc(usize);
void raw_free(void*);
void* operator new(usize size, const std::nothrow_t&) noexcept;
void* operator new[](usize size, const std::nothrow_t&) noexcept;
void operator delete(void* ptr, usize size, std::align_val_t alignment) noexcept;
template <typename T, class... Args> [[nodiscard]] Result<T*> make(Args... args)
{
T* const result = new (std::nothrow) T(args...);
if (!result) return err(ENOMEM);
return result;
}
template <typename T> [[nodiscard]] Result<T*> make_array(usize count)
{
T* const result = new (std::nothrow) 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;
}