16#ifndef PW_ALLOCATOR_PUBLIC_PW_ALLOCATOR_ALLOCATOR_H_
17#define PW_ALLOCATOR_PUBLIC_PW_ALLOCATOR_ALLOCATOR_H_
22#include "pw_allocator/capability.h"
23#include "pw_allocator/config.h"
24#include "pw_allocator/deallocator.h"
25#include "pw_allocator/fragmentation.h"
26#include "pw_allocator/layout.h"
27#include "pw_allocator/shared_ptr.h"
28#include "pw_allocator/unique_ptr.h"
29#include "pw_numeric/checked_arithmetic.h"
30#include "pw_result/result.h"
54 return layout.size() != 0 ?
DoAllocate(layout) :
nullptr;
65 template <
typename T,
int&... kExplicitGuard,
typename... Args>
66 [[nodiscard]] std::enable_if_t<!std::is_array_v<T>, T*>
New(Args&&... args) {
67 void* ptr =
Allocate(Layout::Of<T>());
68 return ptr !=
nullptr ?
new (ptr) T(std::forward<Args>(args)...) :
nullptr;
80 int&... kExplicitGuard,
81 typename ElementType = std::remove_extent_t<T>,
82 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
83 [[nodiscard]] ElementType*
New() {
84 return NewArrayImpl<ElementType>(std::extent_v<T>,
alignof(ElementType));
96 int&... kExplicitGuard,
97 typename ElementType = std::remove_extent_t<T>,
98 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
99 [[nodiscard]] ElementType*
New(
size_t count) {
100 return NewArrayImpl<ElementType>(count,
alignof(ElementType));
112 template <
typename T,
113 int&... kExplicitGuard,
114 typename ElementType = std::remove_extent_t<T>,
115 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
116 [[nodiscard]] ElementType*
New(
size_t count,
size_t alignment) {
117 return NewArrayImpl<ElementType>(count, alignment);
124 template <
typename T>
126 return New<T[]>(count,
alignof(T));
133 template <
typename T>
135 return New<T[]>(count, alignment);
146 template <
typename T,
147 int&... kExplicitGuard,
148 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
151 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *
this);
161 template <
typename T,
162 int&... kExplicitGuard,
163 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
165 return MakeUnique<T>(size,
alignof(std::remove_extent_t<T>));
177 template <
typename T,
178 int&... kExplicitGuard,
179 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
181 return UniquePtr<T>(New<T>(size, alignment), size, *
this);
190 template <
typename T,
191 int&... kExplicitGuard,
192 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
201 template <
typename T>
203 return MakeUnique<T[]>(size,
alignof(std::remove_extent_t<T>));
210 template <
typename T>
212 return MakeUnique<T[]>(size, alignment);
216#if PW_ALLOCATOR_HAS_ATOMICS
225 template <
typename T,
226 int&... kExplicitGuard,
227 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
231 std::forward<Args>(args)...);
241 template <
typename T,
242 int&... kExplicitGuard,
243 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
245 return MakeShared<T>(size,
alignof(std::remove_extent_t<T>));
257 template <
typename T,
258 int&... kExplicitGuard,
259 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
273 template <
typename T,
274 int&... kExplicitGuard,
275 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
278 this, std::extent_v<T>,
alignof(std::remove_extent_t<T>));
296 bool Resize(
void* ptr,
size_t new_size) {
297 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, new_size);
304 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, layout, new_size);
331 if (new_layout.size() == 0) {
334 if (ptr ==
nullptr) {
347 if (ptr ==
nullptr) {
396 [[maybe_unused]]
size_t new_size) {
428 template <
typename ElementType>
429 ElementType* NewArrayImpl(
size_t count,
size_t alignment) {
431 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
Definition: allocator.h:45
SharedPtr< T > MakeShared(size_t size)
Definition: allocator.h:244
UniquePtr< T > MakeUnique(size_t size, size_t alignment)
Definition: allocator.h:180
ElementType * New(size_t count, size_t alignment)
Constructs an alignment-byte aligned array of count objects.
Definition: allocator.h:116
SharedPtr< T > MakeShared(Args &&... args)
Definition: allocator.h:229
UniquePtr< T > MakeUnique()
Definition: allocator.h:193
T * NewArray(size_t count)
Definition: allocator.h:125
std::optional< allocator::Fragmentation > MeasureFragmentation() const
Returns fragmentation information for the allocator's memory region.
Definition: allocator.h:360
virtual size_t DoGetAllocated() const
Definition: allocator.h:425
T * NewArray(size_t count, size_t alignment)
Definition: allocator.h:134
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:260
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:296
virtual void * DoReallocate(void *ptr, Layout new_layout)
UniquePtr< T[]> MakeUniqueArray(size_t size)
Definition: allocator.h:202
virtual void * DoReallocate(void *ptr, Layout old_layout, size_t new_size)
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:330
void * Reallocate(void *ptr, Layout old_layout, size_t new_size)
Definition: allocator.h:343
UniquePtr< T > MakeUnique(Args &&... args)
Definition: allocator.h:150
virtual bool DoResize(void *, Layout, size_t)
Definition: allocator.h:403
std::enable_if_t<!std::is_array_v< T >, T * > New(Args &&... args)
Definition: allocator.h:66
bool Resize(void *ptr, Layout layout, size_t new_size)
Definition: allocator.h:303
void * Allocate(Layout layout)
Definition: allocator.h:53
SharedPtr< T > MakeShared()
Definition: allocator.h:276
ElementType * New(size_t count)
Definition: allocator.h:99
UniquePtr< T[]> MakeUniqueArray(size_t size, size_t alignment)
Definition: allocator.h:211
ElementType * New()
Definition: allocator.h:83
virtual std::optional< allocator::Fragmentation > DoMeasureFragmentation() const
Definition: allocator.h:376
UniquePtr< T > MakeUnique(size_t size)
Definition: allocator.h:164
size_t GetAllocated() const
Definition: allocator.h:355
virtual bool DoResize(void *ptr, size_t new_size)
Definition: allocator.h:395
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: shared_ptr.h:67
Definition: unique_ptr.h:43
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