19#include "pw_allocator/capability.h"
20#include "pw_allocator/config.h"
21#include "pw_allocator/deallocator.h"
22#include "pw_allocator/fragmentation.h"
23#include "pw_allocator/layout.h"
24#include "pw_allocator/shared_ptr.h"
25#include "pw_allocator/unique_ptr.h"
26#include "pw_numeric/checked_arithmetic.h"
27#include "pw_result/result.h"
51 return layout.size() != 0 ?
DoAllocate(layout) :
nullptr;
62 template <
typename T,
int&... kExplicitGuard,
typename... Args>
63 [[nodiscard]] std::enable_if_t<!std::is_array_v<T>, T*>
New(Args&&... args) {
64 void* ptr =
Allocate(Layout::Of<T>());
65 return ptr !=
nullptr ?
new (ptr) T(std::forward<Args>(args)...) :
nullptr;
77 int&... kExplicitGuard,
78 typename ElementType = std::remove_extent_t<T>,
79 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
80 [[nodiscard]] ElementType*
New() {
81 return NewArrayImpl<ElementType>(std::extent_v<T>,
alignof(ElementType));
93 int&... kExplicitGuard,
94 typename ElementType = std::remove_extent_t<T>,
95 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
96 [[nodiscard]] ElementType*
New(
size_t count) {
97 return NewArrayImpl<ElementType>(count,
alignof(ElementType));
109 template <
typename T,
110 int&... kExplicitGuard,
111 typename ElementType = std::remove_extent_t<T>,
112 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
113 [[nodiscard]] ElementType*
New(
size_t count,
size_t alignment) {
114 return NewArrayImpl<ElementType>(count, alignment);
125 template <
typename T,
126 int&... kExplicitGuard,
127 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
130 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *
this);
140 template <
typename T,
141 int&... kExplicitGuard,
142 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
144 return MakeUnique<T>(size,
alignof(std::remove_extent_t<T>));
156 template <
typename T,
157 int&... kExplicitGuard,
158 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
160 return UniquePtr<T>(New<T>(size, alignment), size, *
this);
169 template <
typename T,
170 int&... kExplicitGuard,
171 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
177#if PW_ALLOCATOR_HAS_ATOMICS
186 template <
typename T,
187 int&... kExplicitGuard,
188 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
192 std::forward<Args>(args)...);
202 template <
typename T,
203 int&... kExplicitGuard,
204 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
206 return MakeShared<T>(size,
alignof(std::remove_extent_t<T>));
218 template <
typename T,
219 int&... kExplicitGuard,
220 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
234 template <
typename T,
235 int&... kExplicitGuard,
236 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
239 this, std::extent_v<T>,
alignof(std::remove_extent_t<T>));
257 bool Resize(
void* ptr,
size_t new_size) {
258 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, new_size);
285 if (new_layout.size() == 0) {
288 if (ptr ==
nullptr) {
337 [[maybe_unused]]
size_t new_size) {
359 template <
typename ElementType>
360 ElementType* NewArrayImpl(
size_t count,
size_t alignment) {
362 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
370using Allocator [[deprecated(
"Use pw::Allocator instead")]] =
::pw::Allocator;
Definition: allocator.h:42
SharedPtr< T > MakeShared(size_t size)
Definition: allocator.h:205
UniquePtr< T > MakeUnique(size_t size, size_t alignment)
Definition: allocator.h:159
ElementType * New(size_t count, size_t alignment)
Constructs an alignment-byte aligned array of count objects.
Definition: allocator.h:113
SharedPtr< T > MakeShared(Args &&... args)
Definition: allocator.h:190
UniquePtr< T > MakeUnique()
Definition: allocator.h:172
std::optional< allocator::Fragmentation > MeasureFragmentation() const
Returns fragmentation information for the allocator's memory region.
Definition: allocator.h:301
virtual size_t DoGetAllocated() const
Definition: allocator.h:356
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:221
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:257
virtual void * DoReallocate(void *ptr, Layout new_layout)
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:284
UniquePtr< T > MakeUnique(Args &&... args)
Definition: allocator.h:129
std::enable_if_t<!std::is_array_v< T >, T * > New(Args &&... args)
Definition: allocator.h:63
void * Allocate(Layout layout)
Definition: allocator.h:50
SharedPtr< T > MakeShared()
Definition: allocator.h:237
ElementType * New(size_t count)
Definition: allocator.h:96
ElementType * New()
Definition: allocator.h:80
virtual std::optional< allocator::Fragmentation > DoMeasureFragmentation() const
Definition: allocator.h:317
UniquePtr< T > MakeUnique(size_t size)
Definition: allocator.h:143
size_t GetAllocated() const
Definition: allocator.h:296
virtual bool DoResize(void *ptr, size_t new_size)
Definition: allocator.h:336
Abstract interface for releasing memory.
Definition: deallocator.h:28
Definition: shared_ptr.h:64
static SharedPtr Create(Allocator *allocator, Args &&... args)
Definition: unique_ptr.h:44
Definition: capability.h:65
static constexpr std::enable_if_t<!std::is_array_v< T >, Layout > Of()
Creates a Layout for the given type.
Definition: layout.h:68
Capability
Definition: capability.h:28
The Pigweed namespace.
Definition: alignment.h:27