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 {
38 constexpr const Layout& layout()
const {
return layout_; }
59 int&... kExplicitGuard,
60 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
62 [[nodiscard]] T*
New(Args&&... args) {
63 PW_ASSERT(Layout::Of<T>() == layout_);
65 return ptr !=
nullptr ?
new (ptr) T(std::forward<Args>(args)...) :
nullptr;
69 int&... kExplicitGuard,
70 typename ElementType = std::remove_extent_t<T>,
71 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
72 [[nodiscard]] ElementType* New() {
73 return NewArray<ElementType>(std::extent_v<T>);
77 int&... kExplicitGuard,
78 typename ElementType = std::remove_extent_t<T>,
79 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
80 [[nodiscard]] ElementType* New() {
81 return NewArray<ElementType>(layout_.size() /
sizeof(ElementType));
97 int&... kExplicitGuard,
98 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
101 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *
this);
104 template <
typename T,
105 int&... kExplicitGuard,
106 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
107 UniquePtr<T> MakeUnique() {
108 using ElementType = std::remove_extent_t<T>;
109 return UniquePtr<T>(NewArray<ElementType>(std::extent_v<T>), *
this);
112 template <
typename T,
113 int&... kExplicitGuard,
114 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
116 using ElementType = std::remove_extent_t<T>;
117 size_t size = layout_.size() /
sizeof(ElementType);
118 return UniquePtr<T>(NewArray<ElementType>(size), size, *
this);
127 template <
typename ElementType>
128 [[nodiscard]] ElementType* NewArray(
size_t count) {
129 Layout layout = Layout::Of<ElementType[]>(count);
130 PW_ASSERT(layout.size() == layout_.size());
131 PW_ASSERT(layout.alignment() <= layout_.alignment());
133 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
Abstract interface for releasing memory.
Definition: deallocator.h:27
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
Definition: unique_ptr.h:41
Definition: capability.h:62
void * Allocate()
Definition: pool.h:45
UniquePtr< T > MakeUnique(Args &&... args)
Definition: pool.h:100
T * New(Args &&... args)
Definition: pool.h:62
virtual void * DoAllocate()=0
Virtual Allocate function that can be overridden by derived classes.