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"
45 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>());
59 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 NewArrayImpl<ElementType>(std::extent_v<T>,
alignof(ElementType));
87 int&... kExplicitGuard,
88 typename ElementType = std::remove_extent_t<T>,
89 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
90 [[nodiscard]] ElementType*
New(
size_t count) {
91 return NewArrayImpl<ElementType>(count,
alignof(ElementType));
103 template <
typename T,
104 int&... kExplicitGuard,
105 typename ElementType = std::remove_extent_t<T>,
106 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
107 [[nodiscard]] ElementType*
New(
size_t count,
size_t alignment) {
108 return NewArrayImpl<ElementType>(count, alignment);
115 template <
typename T>
117 return New<T[]>(count,
alignof(T));
124 template <
typename T>
126 return New<T[]>(count, alignment);
137 template <
typename T,
138 int&... kExplicitGuard,
139 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
142 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *
this);
152 template <
typename T,
153 int&... kExplicitGuard,
154 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
156 return MakeUnique<T>(size,
alignof(std::remove_extent_t<T>));
168 template <
typename T,
169 int&... kExplicitGuard,
170 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
172 return UniquePtr<T>(New<T>(size, alignment), size, *
this);
181 template <
typename T,
182 int&... kExplicitGuard,
183 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
192 template <
typename T>
194 return MakeUnique<T[]>(size,
alignof(std::remove_extent_t<T>));
201 template <
typename T>
203 return MakeUnique<T[]>(size, alignment);
207#if PW_ALLOCATOR_HAS_ATOMICS
216 template <
typename T,
217 int&... kExplicitGuard,
218 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
222 std::forward<Args>(args)...);
232 template <
typename T,
233 int&... kExplicitGuard,
234 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
236 return MakeShared<T>(size,
alignof(std::remove_extent_t<T>));
248 template <
typename T,
249 int&... kExplicitGuard,
250 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
264 template <
typename T,
265 int&... kExplicitGuard,
266 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
269 this, std::extent_v<T>,
alignof(std::remove_extent_t<T>));
287 bool Resize(
void* ptr,
size_t new_size) {
288 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, new_size);
295 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, layout, new_size);
322 if (new_layout.size() == 0) {
325 if (ptr ==
nullptr) {
338 if (ptr ==
nullptr) {
370 [[maybe_unused]]
size_t new_size) {
402 template <
typename ElementType>
403 ElementType* NewArrayImpl(
size_t count,
size_t alignment) {
405 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
Definition: allocator.h:36
SharedPtr< T > MakeShared(size_t size)
Definition: allocator.h:235
UniquePtr< T > MakeUnique(size_t size, size_t alignment)
Definition: allocator.h:171
ElementType * New(size_t count, size_t alignment)
Constructs an alignment-byte aligned array of count objects.
Definition: allocator.h:107
SharedPtr< T > MakeShared(Args &&... args)
Definition: allocator.h:220
UniquePtr< T > MakeUnique()
Definition: allocator.h:184
T * NewArray(size_t count)
Definition: allocator.h:116
virtual size_t DoGetAllocated() const
Definition: allocator.h:399
T * NewArray(size_t count, size_t alignment)
Definition: allocator.h:125
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:251
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:287
virtual void * DoReallocate(void *ptr, Layout new_layout)
UniquePtr< T[]> MakeUniqueArray(size_t size)
Definition: allocator.h:193
virtual void * DoReallocate(void *ptr, Layout old_layout, size_t new_size)
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:321
void * Reallocate(void *ptr, Layout old_layout, size_t new_size)
Definition: allocator.h:334
UniquePtr< T > MakeUnique(Args &&... args)
Definition: allocator.h:141
virtual bool DoResize(void *, Layout, size_t)
Definition: allocator.h:377
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:294
void * Allocate(Layout layout)
Definition: allocator.h:44
SharedPtr< T > MakeShared()
Definition: allocator.h:267
ElementType * New(size_t count)
Definition: allocator.h:90
UniquePtr< T[]> MakeUniqueArray(size_t size, size_t alignment)
Definition: allocator.h:202
ElementType * New()
Definition: allocator.h:74
UniquePtr< T > MakeUnique(size_t size)
Definition: allocator.h:155
size_t GetAllocated() const
Definition: allocator.h:346
virtual bool DoResize(void *ptr, size_t new_size)
Definition: allocator.h:369
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: shared_ptr.h:59
Definition: unique_ptr.h:43
Definition: capability.h:64
static constexpr std::enable_if_t<!std::is_array_v< T >, Layout > Of()
Creates a Layout for the given type.
Definition: layout.h:68
The Pigweed namespace.
Definition: alignment.h:27