19#include "pw_allocator/capability.h"
20#include "pw_allocator/deallocator.h"
21#include "pw_allocator/layout.h"
22#include "pw_allocator/unique_ptr.h"
23#include "pw_assert/assert.h"
24#include "pw_bytes/span.h"
25#include "pw_result/result.h"
27namespace pw::allocator {
41 constexpr const Layout& layout()
const {
return layout_; }
62 int&... kExplicitGuard,
63 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
65 [[nodiscard]] T*
New(Args&&... args) {
66 PW_ASSERT(Layout::Of<T>() == layout_);
68 return ptr !=
nullptr ?
new (ptr) T(std::forward<Args>(args)...) :
nullptr;
72 int&... kExplicitGuard,
73 typename ElementType = std::remove_extent_t<T>,
74 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
75 [[nodiscard]] ElementType* New() {
76 return NewArray<ElementType>(std::extent_v<T>);
80 int&... kExplicitGuard,
81 typename ElementType = std::remove_extent_t<T>,
82 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
83 [[nodiscard]] ElementType* New() {
84 return NewArray<ElementType>(layout_.size() /
sizeof(ElementType));
100 int&... kExplicitGuard,
101 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
104 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *
this);
107 template <
typename T,
108 int&... kExplicitGuard,
109 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
111 using ElementType = std::remove_extent_t<T>;
112 return UniquePtr<T>(NewArray<ElementType>(std::extent_v<T>), *
this);
115 template <
typename T,
116 int&... kExplicitGuard,
117 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
119 using ElementType = std::remove_extent_t<T>;
120 size_t size = layout_.size() /
sizeof(ElementType);
121 return UniquePtr<T>(NewArray<ElementType>(size), size, *
this);
130 template <
typename ElementType>
131 [[nodiscard]] ElementType* NewArray(
size_t count) {
132 Layout layout = Layout::Of<ElementType[]>(count);
133 PW_ASSERT(layout.size() == layout_.size());
134 PW_ASSERT(layout.alignment() <= layout_.alignment());
136 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
Abstract interface for releasing memory.
Definition: deallocator.h:28
Definition: unique_ptr.h:44
Definition: capability.h:65
void * Allocate()
Definition: pool.h:48
UniquePtr< T > MakeUnique(Args &&... args)
Definition: pool.h:103
T * New(Args &&... args)
Definition: pool.h:65
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.