19#include "pw_allocator/capability.h"
20#include "pw_allocator/deallocator.h"
21#include "pw_allocator/layout.h"
22#include "pw_assert/assert.h"
23#include "pw_bytes/span.h"
24#include "pw_result/result.h"
26namespace pw::allocator {
40 constexpr const Layout& layout()
const {
return layout_; }
61 int&... kExplicitGuard,
62 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
64 [[nodiscard]] T*
New(Args&&... args) {
65 PW_ASSERT(Layout::Of<T>() == layout_);
67 return ptr !=
nullptr ?
new (ptr) T(std::forward<Args>(args)...) :
nullptr;
71 int&... kExplicitGuard,
72 typename ElementType = std::remove_extent_t<T>,
73 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
74 [[nodiscard]] ElementType* New() {
75 return NewArray<ElementType>(std::extent_v<T>);
79 int&... kExplicitGuard,
80 typename ElementType = std::remove_extent_t<T>,
81 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
82 [[nodiscard]] ElementType* New() {
83 return NewArray<ElementType>(layout_.size() /
sizeof(ElementType));
99 int&... kExplicitGuard,
100 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
103 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *
this);
106 template <
typename T,
107 int&... kExplicitGuard,
108 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
109 UniquePtr<T> MakeUnique() {
110 using ElementType = std::remove_extent_t<T>;
111 return UniquePtr<T>(NewArray<ElementType>(std::extent_v<T>), *
this);
114 template <
typename T,
115 int&... kExplicitGuard,
116 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
118 using ElementType = std::remove_extent_t<T>;
119 size_t size = layout_.size() /
sizeof(ElementType);
120 return UniquePtr<T>(NewArray<ElementType>(size), size, *
this);
129 template <
typename ElementType>
130 [[nodiscard]] ElementType* NewArray(
size_t count) {
131 Layout layout = Layout::Of<ElementType[]>(count);
132 PW_ASSERT(layout.size() == layout_.size());
133 PW_ASSERT(layout.alignment() <= layout_.alignment());
135 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: unique_ptr.h:43
Definition: capability.h:64
void * Allocate()
Definition: pool.h:47
UniquePtr< T > MakeUnique(Args &&... args)
Definition: pool.h:102
T * New(Args &&... args)
Definition: pool.h:64
virtual void * DoAllocate()=0
Virtual Allocate function that can be overridden by derived classes.
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.