18#include "pw_allocator/capability.h"
19#include "pw_allocator/config.h"
20#include "pw_allocator/deallocator.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"
43 return layout.size() != 0 ?
DoAllocate(layout) :
nullptr;
54 template <
typename T,
int&... kExplicitGuard,
typename... Args>
55 [[nodiscard]] std::enable_if_t<!std::is_array_v<T>, T*>
New(Args&&... args) {
56 void* ptr =
Allocate(Layout::Of<T>());
57 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 NewArrayImpl<ElementType>(std::extent_v<T>,
alignof(ElementType));
85 int&... kExplicitGuard,
86 typename ElementType = std::remove_extent_t<T>,
87 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
88 [[nodiscard]] ElementType*
New(
size_t count) {
89 return NewArrayImpl<ElementType>(count,
alignof(ElementType));
101 template <
typename T,
102 int&... kExplicitGuard,
103 typename ElementType = std::remove_extent_t<T>,
104 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
105 [[nodiscard]] ElementType*
New(
size_t count,
size_t alignment) {
106 return NewArrayImpl<ElementType>(count, alignment);
113 template <
typename T>
115 return New<T[]>(count,
alignof(T));
122 template <
typename T>
124 return New<T[]>(count, alignment);
135 template <
typename T,
136 int&... kExplicitGuard,
137 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
140 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *
this);
150 template <
typename T,
151 int&... kExplicitGuard,
152 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
154 return MakeUnique<T>(size,
alignof(std::remove_extent_t<T>));
166 template <
typename T,
167 int&... kExplicitGuard,
168 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
170 return UniquePtr<T>(New<T>(size, alignment), size, *
this);
179 template <
typename T,
180 int&... kExplicitGuard,
181 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
190 template <
typename T>
192 return MakeUnique<T[]>(size,
alignof(std::remove_extent_t<T>));
199 template <
typename T>
201 return MakeUnique<T[]>(size, alignment);
205#if PW_ALLOCATOR_HAS_ATOMICS
214 template <
typename T,
215 int&... kExplicitGuard,
216 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
220 std::forward<Args>(args)...);
230 template <
typename T,
231 int&... kExplicitGuard,
232 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
234 return MakeShared<T>(size,
alignof(std::remove_extent_t<T>));
246 template <
typename T,
247 int&... kExplicitGuard,
248 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
262 template <
typename T,
263 int&... kExplicitGuard,
264 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
267 this, std::extent_v<T>,
alignof(std::remove_extent_t<T>));
285 bool Resize(
void* ptr,
size_t new_size) {
286 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, new_size);
293 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, layout, new_size);
320 if (new_layout.size() == 0) {
323 if (ptr ==
nullptr) {
336 if (ptr ==
nullptr) {
368 [[maybe_unused]]
size_t new_size) {
400 template <
typename ElementType>
401 ElementType* NewArrayImpl(
size_t count,
size_t alignment) {
403 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
Definition: allocator.h:34
SharedPtr< T > MakeShared(size_t size)
Definition: allocator.h:233
UniquePtr< T > MakeUnique(size_t size, size_t alignment)
Definition: allocator.h:169
ElementType * New(size_t count, size_t alignment)
Constructs an alignment-byte aligned array of count objects.
Definition: allocator.h:105
SharedPtr< T > MakeShared(Args &&... args)
Definition: allocator.h:218
UniquePtr< T > MakeUnique()
Definition: allocator.h:182
T * NewArray(size_t count)
Definition: allocator.h:114
virtual size_t DoGetAllocated() const
Definition: allocator.h:397
T * NewArray(size_t count, size_t alignment)
Definition: allocator.h:123
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:249
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:285
virtual void * DoReallocate(void *ptr, Layout new_layout)
UniquePtr< T[]> MakeUniqueArray(size_t size)
Definition: allocator.h:191
virtual void * DoReallocate(void *ptr, Layout old_layout, size_t new_size)
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:319
void * Reallocate(void *ptr, Layout old_layout, size_t new_size)
Definition: allocator.h:332
UniquePtr< T > MakeUnique(Args &&... args)
Definition: allocator.h:139
virtual bool DoResize(void *, Layout, size_t)
Definition: allocator.h:375
std::enable_if_t<!std::is_array_v< T >, T * > New(Args &&... args)
Definition: allocator.h:55
bool Resize(void *ptr, Layout layout, size_t new_size)
Definition: allocator.h:292
void * Allocate(Layout layout)
Definition: allocator.h:42
SharedPtr< T > MakeShared()
Definition: allocator.h:265
ElementType * New(size_t count)
Definition: allocator.h:88
UniquePtr< T[]> MakeUniqueArray(size_t size, size_t alignment)
Definition: allocator.h:200
ElementType * New()
Definition: allocator.h:72
UniquePtr< T > MakeUnique(size_t size)
Definition: allocator.h:153
size_t GetAllocated() const
Definition: allocator.h:344
virtual bool DoResize(void *ptr, size_t new_size)
Definition: allocator.h:367
Abstract interface for releasing memory.
Definition: deallocator.h:27
Definition: shared_ptr.h:57
Definition: unique_ptr.h:41
Definition: capability.h:62
static constexpr std::enable_if_t<!std::is_array_v< T >, Layout > Of()
Creates a Layout for the given type.
Definition: layout.h:66
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27