2023-08-23 09:08:18 +00:00
|
|
|
/**
|
|
|
|
* @file Alloc.h
|
|
|
|
* @author apio (cloudapio.eu)
|
|
|
|
* @brief Fallible version of new.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022-2023, the Luna authors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-12-06 15:41:22 +00:00
|
|
|
#pragma once
|
2023-01-13 19:00:20 +00:00
|
|
|
#include <luna/Heap.h>
|
|
|
|
#include <luna/PlacementNew.h>
|
2022-12-06 15:41:22 +00:00
|
|
|
#include <luna/Result.h>
|
|
|
|
|
2023-08-23 09:08:18 +00:00
|
|
|
/**
|
|
|
|
* @brief Allocate a value on the heap and initialize it.
|
|
|
|
*
|
|
|
|
* When this value is no longer used, you must call delete to destroy it.
|
|
|
|
*
|
|
|
|
* @tparam T The type of the value.
|
|
|
|
* @tparam Args The types of arguments to pass to the value's constructor.
|
|
|
|
* @param args The arguments to pass to the value's constructor.
|
|
|
|
* @return Result<T*> An error, or a pointer to the new value.
|
|
|
|
*/
|
2022-12-07 10:01:45 +00:00
|
|
|
template <typename T, class... Args> [[nodiscard]] Result<T*> make(Args... args)
|
2022-12-06 15:41:22 +00:00
|
|
|
{
|
2023-08-27 18:48:33 +00:00
|
|
|
T* const result = (T*)TRY(malloc_impl(sizeof(T), false, false));
|
2023-01-13 19:00:20 +00:00
|
|
|
new (result) T(args...);
|
2022-12-06 15:41:22 +00:00
|
|
|
return result;
|
|
|
|
}
|