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"
63 int&... kExplicitGuard,
64 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
66 [[nodiscard]] T*
New(Args&&... args) {
67 void* ptr =
Allocate(Layout::Of<T>());
68 return ptr !=
nullptr ?
new (ptr) T(std::forward<Args>(args)...) :
nullptr;
79 int&... kExplicitGuard,
80 typename ElementType = std::remove_extent_t<T>,
81 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
82 [[nodiscard]] ElementType*
New() {
83 return NewArrayImpl<ElementType>(std::extent_v<T>,
alignof(ElementType));
95 int&... kExplicitGuard,
96 typename ElementType = std::remove_extent_t<T>,
97 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
98 [[nodiscard]] ElementType*
New(
size_t count) {
99 return NewArrayImpl<ElementType>(count,
alignof(ElementType));
111 template <
typename T,
112 int&... kExplicitGuard,
113 typename ElementType = std::remove_extent_t<T>,
114 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
115 [[nodiscard]] ElementType*
New(
size_t count,
size_t alignment) {
116 return NewArrayImpl<ElementType>(count, alignment);
127 template <
typename T,
128 int&... kExplicitGuard,
129 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
132 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *
this);
142 template <
typename T, 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, std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
158 return UniquePtr<T>(New<T>(size, alignment), size, *
this);
167 template <
typename T, std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
173#if PW_ALLOCATOR_HAS_ATOMICS
182 template <
typename T,
183 int&... kExplicitGuard,
184 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
188 std::forward<Args>(args)...);
198 template <
typename T, std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
200 return MakeShared<T>(size,
alignof(std::remove_extent_t<T>));
212 template <
typename T, std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
223 template <
typename T, std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
226 this, std::extent_v<T>,
alignof(std::remove_extent_t<T>));
244 [[nodiscard]]
bool Resize(
void* ptr,
size_t new_size);
292 [[maybe_unused]]
size_t new_size) {
319 template <
typename ElementType>
320 ElementType* NewArrayImpl(
size_t count,
size_t alignment) {
322 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
330using Allocator [[deprecated(
"Use pw::Allocator instead")]] =
::pw::Allocator;
Definition: allocator.h:42
ElementType * New(size_t count, size_t alignment)
Constructs an alignment-byte aligned array of count objects.
Definition: allocator.h:115
SharedPtr< T > MakeShared(Args &&... args)
Definition: allocator.h:186
SharedPtr< T > MakeShared()
Definition: allocator.h:224
UniquePtr< T > MakeUnique()
Definition: allocator.h:168
virtual size_t DoGetAllocated() const
Definition: allocator.h:307
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:213
bool Resize(void *ptr, size_t new_size)
virtual void * DoReallocate(void *ptr, Layout new_layout)
void * Reallocate(void *ptr, Layout new_layout)
T * New(Args &&... args)
Definition: allocator.h:66
UniquePtr< T > MakeUnique(Args &&... args)
Definition: allocator.h:131
UniquePtr< T > MakeUnique(size_t size, size_t alignment)
Definition: allocator.h:157
virtual std::optional< Fragmentation > DoMeasureFragmentation() const
Returns fragmentation information for the allocator's memory region.
Definition: allocator.h:313
void * Allocate(Layout layout)
UniquePtr< T > MakeUnique(size_t size)
Definition: allocator.h:143
ElementType * New(size_t count)
Definition: allocator.h:98
SharedPtr< T > MakeShared(size_t size)
Definition: allocator.h:199
ElementType * New()
Definition: allocator.h:82
size_t GetAllocated() const
Definition: allocator.h:273
virtual bool DoResize(void *ptr, size_t new_size)
Definition: allocator.h:291
std::optional< Fragmentation > MeasureFragmentation() const
Returns fragmentation information for the allocator's memory region.
Definition: allocator.h:276
Abstract interface for releasing memory.
Definition: deallocator.h:30
Definition: shared_ptr.h:64
static SharedPtr Create(Allocator *allocator, Args &&... args)
Definition: unique_ptr.h:44
Definition: capability.h:65
static constexpr Layout Of()
Creates a Layout for the given type.
Definition: layout.h:77
Capability
Definition: capability.h:28
The Pigweed namespace.
Definition: alignment.h:27
Definition: fragmentation.h:47