16#ifndef PW_ALLOCATOR_PUBLIC_PW_ALLOCATOR_ALLOCATOR_H_
17#define PW_ALLOCATOR_PUBLIC_PW_ALLOCATOR_ALLOCATOR_H_
21#include "pw_allocator/capability.h"
22#include "pw_allocator/config.h"
23#include "pw_allocator/deallocator.h"
24#include "pw_allocator/layout.h"
25#include "pw_allocator/shared_ptr.h"
26#include "pw_allocator/unique_ptr.h"
27#include "pw_numeric/checked_arithmetic.h"
28#include "pw_result/result.h"
52 return layout.size() != 0 ?
DoAllocate(layout) :
nullptr;
63 template <
typename T,
int&... kExplicitGuard,
typename... Args>
64 [[nodiscard]] std::enable_if_t<!std::is_array_v<T>, T*>
New(Args&&... args) {
65 void* ptr =
Allocate(Layout::Of<T>());
66 return ptr !=
nullptr ?
new (ptr) T(std::forward<Args>(args)...) :
nullptr;
78 int&... kExplicitGuard,
79 typename ElementType = std::remove_extent_t<T>,
80 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
81 [[nodiscard]] ElementType*
New() {
82 return NewArrayImpl<ElementType>(std::extent_v<T>,
alignof(ElementType));
94 int&... kExplicitGuard,
95 typename ElementType = std::remove_extent_t<T>,
96 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
97 [[nodiscard]] ElementType*
New(
size_t count) {
98 return NewArrayImpl<ElementType>(count,
alignof(ElementType));
110 template <
typename T,
111 int&... kExplicitGuard,
112 typename ElementType = std::remove_extent_t<T>,
113 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
114 [[nodiscard]] ElementType*
New(
size_t count,
size_t alignment) {
115 return NewArrayImpl<ElementType>(count, alignment);
122 template <
typename T>
124 return New<T[]>(count,
alignof(T));
131 template <
typename T>
133 return New<T[]>(count, alignment);
144 template <
typename T,
145 int&... kExplicitGuard,
146 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
149 return UniquePtr<T>(New<T>(std::forward<Args>(args)...), *
this);
159 template <
typename T,
160 int&... kExplicitGuard,
161 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
163 return MakeUnique<T>(size,
alignof(std::remove_extent_t<T>));
175 template <
typename T,
176 int&... kExplicitGuard,
177 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
179 return UniquePtr<T>(New<T>(size, alignment), size, *
this);
188 template <
typename T,
189 int&... kExplicitGuard,
190 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
199 template <
typename T>
201 return MakeUnique<T[]>(size,
alignof(std::remove_extent_t<T>));
208 template <
typename T>
210 return MakeUnique<T[]>(size, alignment);
214#if PW_ALLOCATOR_HAS_ATOMICS
223 template <
typename T,
224 int&... kExplicitGuard,
225 std::enable_if_t<!std::is_array_v<T>,
int> = 0,
229 std::forward<Args>(args)...);
239 template <
typename T,
240 int&... kExplicitGuard,
241 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
243 return MakeShared<T>(size,
alignof(std::remove_extent_t<T>));
255 template <
typename T,
256 int&... kExplicitGuard,
257 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
271 template <
typename T,
272 int&... kExplicitGuard,
273 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
276 this, std::extent_v<T>,
alignof(std::remove_extent_t<T>));
294 bool Resize(
void* ptr,
size_t new_size) {
295 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, new_size);
302 return ptr !=
nullptr && new_size != 0 &&
DoResize(ptr, layout, new_size);
329 if (new_layout.size() == 0) {
332 if (ptr ==
nullptr) {
345 if (ptr ==
nullptr) {
377 [[maybe_unused]]
size_t new_size) {
409 template <
typename ElementType>
410 ElementType* NewArrayImpl(
size_t count,
size_t alignment) {
412 return ptr !=
nullptr ?
new (ptr) ElementType[count] :
nullptr;
Definition: allocator.h:43
SharedPtr< T > MakeShared(size_t size)
Definition: allocator.h:242
UniquePtr< T > MakeUnique(size_t size, size_t alignment)
Definition: allocator.h:178
ElementType * New(size_t count, size_t alignment)
Constructs an alignment-byte aligned array of count objects.
Definition: allocator.h:114
SharedPtr< T > MakeShared(Args &&... args)
Definition: allocator.h:227
UniquePtr< T > MakeUnique()
Definition: allocator.h:191
T * NewArray(size_t count)
Definition: allocator.h:123
virtual size_t DoGetAllocated() const
Definition: allocator.h:406
T * NewArray(size_t count, size_t alignment)
Definition: allocator.h:132
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:258
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:294
virtual void * DoReallocate(void *ptr, Layout new_layout)
UniquePtr< T[]> MakeUniqueArray(size_t size)
Definition: allocator.h:200
virtual void * DoReallocate(void *ptr, Layout old_layout, size_t new_size)
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:328
void * Reallocate(void *ptr, Layout old_layout, size_t new_size)
Definition: allocator.h:341
UniquePtr< T > MakeUnique(Args &&... args)
Definition: allocator.h:148
virtual bool DoResize(void *, Layout, size_t)
Definition: allocator.h:384
std::enable_if_t<!std::is_array_v< T >, T * > New(Args &&... args)
Definition: allocator.h:64
bool Resize(void *ptr, Layout layout, size_t new_size)
Definition: allocator.h:301
void * Allocate(Layout layout)
Definition: allocator.h:51
SharedPtr< T > MakeShared()
Definition: allocator.h:274
ElementType * New(size_t count)
Definition: allocator.h:97
UniquePtr< T[]> MakeUniqueArray(size_t size, size_t alignment)
Definition: allocator.h:209
ElementType * New()
Definition: allocator.h:81
UniquePtr< T > MakeUnique(size_t size)
Definition: allocator.h:162
size_t GetAllocated() const
Definition: allocator.h:353
virtual bool DoResize(void *ptr, size_t new_size)
Definition: allocator.h:376
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: shared_ptr.h:63
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