16#include "pw_allocator/capability.h"
17#include "pw_allocator/hardening.h"
18#include "pw_allocator/layout.h"
19#include "pw_allocator/unique_ptr.h"
20#include "pw_result/result.h"
21#include "pw_status/status.h"
22#include "pw_status/status_with_size.h"
31 static constexpr bool is_bounded_array_v =
32 allocator::internal::is_bounded_array_v<T>;
35 static constexpr bool is_unbounded_array_v =
36 allocator::internal::is_unbounded_array_v<T>;
40 using Capability = allocator::Capability;
45 constexpr const Capabilities& capabilities()
const {
return capabilities_; }
49 return capabilities_.has(capability);
69 DoDeallocate(ptr, layout);
95 int&... kExplicitGuard,
96 std::enable_if_t<!std::is_array_v<T>,
int> = 0>
98 if constexpr (allocator::Hardening::kIncludesDebugChecks) {
99 if (
auto result = GetRequestedLayout(ptr); result.ok()) {
100 PW_ASSERT(*result == Layout::Of<T>());
103 DeleteArray<T>(ptr, 1);
106 template <
typename T,
107 int&... kExplicitGuard,
108 typename ElementType = std::remove_extent_t<T>,
109 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
110 void Delete(ElementType* ptr) {
111 size_t count = std::extent_v<T>;
113 DeleteArray<ElementType>(&ptr[0], count);
117 template <
typename T,
118 int&... kExplicitGuard,
119 typename ElementType = std::remove_extent_t<T>,
120 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
121 void Delete(ElementType* ptr,
size_t count) {
122 DeleteArray<ElementType>(ptr, count);
138 template <
typename ElementType>
140 if (!capabilities_.has(Capability::kSkipsDestroy)) {
141 std::destroy_n(ptr, count);
154 auto result = DoGetInfo(InfoType::kCapacity,
nullptr);
175 : capabilities_(capabilities) {}
185 template <
typename T,
186 int&... kExplicitGuard,
187 typename ElementType = std::remove_extent_t<T>,
188 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
190 ElementType* ptr,
size_t size) {
222 enum class InfoType {
281 Result<Layout> GetInfo(InfoType info_type,
const void* ptr)
const {
282 return DoGetInfo(info_type, ptr);
290 static Result<Layout> GetInfo(
const Deallocator& deallocator,
293 return deallocator.DoGetInfo(info_type, ptr);
298 Result<Layout> GetRequestedLayout(
const void* ptr)
const {
299 return DoGetInfo(InfoType::kRequestedLayoutOf, ptr);
304 static Result<Layout> GetRequestedLayout(
const Deallocator& deallocator,
306 return deallocator.GetRequestedLayout(ptr);
311 Result<Layout> GetUsableLayout(
const void* ptr)
const {
312 return DoGetInfo(InfoType::kUsableLayoutOf, ptr);
317 static Result<Layout> GetUsableLayout(
const Deallocator& deallocator,
319 return deallocator.GetUsableLayout(ptr);
324 Result<Layout> GetAllocatedLayout(
const void* ptr)
const {
325 return DoGetInfo(InfoType::kAllocatedLayoutOf, ptr);
330 static Result<Layout> GetAllocatedLayout(
const Deallocator& deallocator,
332 return deallocator.GetAllocatedLayout(ptr);
337 bool Recognizes(
const void* ptr)
const {
338 return DoGetInfo(InfoType::kRecognizes, ptr).ok();
343 static bool Recognizes(
const Deallocator& deallocator,
const void* ptr) {
344 return deallocator.Recognizes(ptr);
351 virtual void DoDeallocate([[maybe_unused]]
void* ptr) {
360 virtual void DoDeallocate(
void* ptr, Layout) { DoDeallocate(ptr); }
363 virtual Result<Layout> DoGetInfo(InfoType,
const void*)
const {
364 return Status::Unimplemented();
367 const Capabilities capabilities_;
Abstract interface for releasing memory.
Definition: deallocator.h:27
void Deallocate(void *ptr, Layout layout)
Definition: deallocator.h:67
bool HasCapability(Capability capability) const
Returns whether a given capability is enabled for this object.
Definition: deallocator.h:48
void Delete(T *ptr)
Definition: deallocator.h:97
StatusWithSize GetCapacity() const
Definition: deallocator.h:153
void DeleteArray(ElementType *ptr, size_t count)
Definition: deallocator.h:139
void Deallocate(void *ptr)
Definition: deallocator.h:58
UniquePtr< T > WrapUnique(ElementType *ptr, size_t size)
Definition: deallocator.h:189
bool IsEqual(const Deallocator &other) const
Definition: deallocator.h:168
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
Definition: status_with_size.h:49
Definition: unique_ptr.h:41
Definition: capability.h:62
static constexpr Layout Unwrap(const Result< Layout > &result)
Definition: layout.h:89
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27