18#include "pw_allocator/capability.h"
19#include "pw_allocator/deallocator.h"
20#include "pw_allocator/internal/control_block.h"
21#include "pw_allocator/layout.h"
22#include "pw_allocator/shared_ptr.h"
23#include "pw_allocator/unique_ptr.h"
24#include "pw_numeric/checked_arithmetic.h"
25#include "pw_result/result.h"
36 using ControlBlock = allocator::internal::ControlBlock;
46 return layout.size() != 0 ?
DoAllocate(layout) :
nullptr;
56 template <
typename T,
int&... kExplicitGuard,
typename... Args>
57 [[nodiscard]] std::enable_if_t<!std::is_array_v<T>, T*>
New(Args&&... args) {
58 void* ptr =
Allocate(Layout::Of<T>());
62 return new (ptr) T(std::forward<Args>(args)...);
73 int&... kExplicitGuard,
74 typename ElementType = std::remove_extent_t<T>,
75 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
76 [[nodiscard]] ElementType*
New(
size_t count) {
77 return New<T>(count,
alignof(ElementType));
90 int&... kExplicitGuard,
91 typename ElementType = std::remove_extent_t<T>,
92 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
93 [[nodiscard]] ElementType*
New(
size_t count,
size_t alignment) {
94 void* ptr =
Allocate(Layout::Of<T>(count).Align(alignment));
95 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
102 template <
typename T>
104 return New<T[]>(count,
alignof(T));
111 template <
typename T>
113 return New<T[]>(count, alignment);
123 template <
typename T,
124 int&... kExplicitGuard,
125 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
128 return Deallocator::WrapUnique<T>(New<T>(std::forward<Args>(args)...));
138 template <
typename T,
139 int&... kExplicitGuard,
140 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
142 return MakeUnique<T>(size,
alignof(std::remove_extent_t<T>));
154 template <
typename T,
155 int&... kExplicitGuard,
156 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
158 return Deallocator::WrapUnique<T>(New<T>(size, alignment), size);
165 template <
typename T>
167 return MakeUnique<T[]>(size,
alignof(std::remove_extent_t<T>));
174 template <
typename T>
176 return MakeUnique<T[]>(size, alignment);
180 template <
typename T,
181 int&... kExplicitGuard,
182 std::enable_if_t<is_bounded_array_v<T>,
int> = 0,
193 template <
typename T,
194 int&... kExplicitGuard,
195 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
198 auto* control_block = ControlBlock::Create(
this, Layout::Of<T>(), 1);
199 if (control_block ==
nullptr) {
202 auto* t =
new (control_block->data()) T(std::forward<Args>(args)...);
213 template <
typename T,
214 int&... kExplicitGuard,
215 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
217 return MakeShared<T>(size,
alignof(std::remove_extent_t<T>));
229 template <
typename T,
230 int&... kExplicitGuard,
231 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
233 Layout layout = Layout::Of<T>(size).Align(alignment);
234 auto* control_block = ControlBlock::Create(
this, layout, size);
235 if (control_block ==
nullptr) {
238 auto* t =
new (control_block->data()) std::remove_extent_t<T>[size];
243 template <
typename T,
244 int&... kExplicitGuard,
245 std::enable_if_t<is_bounded_array_v<T>,
int> = 0,
247 std::enable_if_t<is_bounded_array_v<T>>
MakeShared(Args&&...) =
delete;
261 bool Resize(
void* ptr,
size_t new_size) {
262 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, new_size);
269 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, layout, new_size);
296 if (new_layout.size() == 0) {
299 if (ptr ==
nullptr) {
312 if (ptr ==
nullptr) {
343 virtual bool DoResize(
void* ,
size_t ) {
return false; }
Definition: allocator.h:34
SharedPtr< T > MakeShared(size_t size)
Definition: allocator.h:216
UniquePtr< T > MakeUnique(size_t size, size_t alignment)
Definition: allocator.h:157
ElementType * New(size_t count, size_t alignment)
Definition: allocator.h:93
SharedPtr< T > MakeShared(Args &&... args)
Definition: allocator.h:197
T * NewArray(size_t count)
Definition: allocator.h:103
virtual bool DoResize(void *, size_t)
Definition: allocator.h:343
virtual size_t DoGetAllocated() const
Definition: allocator.h:370
T * NewArray(size_t count, size_t alignment)
Definition: allocator.h:112
virtual void * DoAllocate(Layout layout)=0
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
SharedPtr< T > MakeShared(size_t size, size_t alignment)
Definition: allocator.h:232
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:261
virtual void * DoReallocate(void *ptr, Layout new_layout)
UniquePtr< T[]> MakeUniqueArray(size_t size)
Definition: allocator.h:166
virtual void * DoReallocate(void *ptr, Layout old_layout, size_t new_size)
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:295
void * Reallocate(void *ptr, Layout old_layout, size_t new_size)
Definition: allocator.h:308
UniquePtr< T > MakeUnique(Args &&... args)
Definition: allocator.h:127
virtual bool DoResize(void *, Layout, size_t)
Definition: allocator.h:348
std::enable_if_t<!std::is_array_v< T >, T * > New(Args &&... args)
Definition: allocator.h:57
bool Resize(void *ptr, Layout layout, size_t new_size)
Definition: allocator.h:268
void * Allocate(Layout layout)
Definition: allocator.h:45
ElementType * New(size_t count)
Definition: allocator.h:76
UniquePtr< T[]> MakeUniqueArray(size_t size, size_t alignment)
Definition: allocator.h:175
UniquePtr< T > MakeUnique(size_t size)
Definition: allocator.h:141
size_t GetAllocated() const
Definition: allocator.h:320
Abstract interface for releasing memory.
Definition: deallocator.h:26
Definition: shared_ptr.h:44
Definition: unique_ptr.h:40
Definition: capability.h:62
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27