libluna: Document Alloc.h
This commit is contained in:
parent
e247310ded
commit
6058a69182
@ -1,16 +1,30 @@
|
|||||||
|
/**
|
||||||
|
* @file Alloc.h
|
||||||
|
* @author apio (cloudapio.eu)
|
||||||
|
* @brief Fallible version of new.
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2022-2023, the Luna authors.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <luna/Heap.h>
|
#include <luna/Heap.h>
|
||||||
#include <luna/PlacementNew.h>
|
#include <luna/PlacementNew.h>
|
||||||
#include <luna/Result.h>
|
#include <luna/Result.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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.
|
||||||
|
*/
|
||||||
template <typename T, class... Args> [[nodiscard]] Result<T*> make(Args... args)
|
template <typename T, class... Args> [[nodiscard]] Result<T*> make(Args... args)
|
||||||
{
|
{
|
||||||
T* const result = (T*)TRY(malloc_impl(sizeof(T)));
|
T* const result = (T*)TRY(malloc_impl(sizeof(T)));
|
||||||
new (result) T(args...);
|
new (result) T(args...);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void destroy(T* item)
|
|
||||||
{
|
|
||||||
delete item;
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user