Luna/luna/include/luna/Alloc.h
apio d759058b80
Introduce std::nothrow
Let's make sure we explicitly tell new that we don't want exceptions
2022-12-16 18:14:48 +01:00

46 lines
928 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;
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;
}