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"
33 static constexpr bool is_bounded_array_v =
34 allocator::internal::is_bounded_array_v<T>;
37 static constexpr bool is_unbounded_array_v =
38 allocator::internal::is_unbounded_array_v<T>;
47 constexpr const Capabilities& capabilities()
const {
return capabilities_; }
51 return capabilities_.has(capability);
71 DoDeallocate(ptr, layout);
97 int&... kExplicitGuard,
98 std::enable_if_t<!std::is_array_v<T>,
int> = 0>
100 if constexpr (allocator::Hardening::kIncludesDebugChecks) {
101 if (
auto result = GetRequestedLayout(ptr); result.ok()) {
102 if constexpr (std::has_virtual_destructor_v<T>) {
103 PW_ASSERT(result->size() >=
sizeof(T) &&
104 result->alignment() >=
alignof(T));
106 PW_ASSERT(*result == Layout::Of<T>());
110 DeleteArray<T>(ptr, 1);
113 template <
typename T,
114 int&... kExplicitGuard,
115 typename ElementType = std::remove_extent_t<T>,
116 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
117 void Delete(ElementType* ptr) {
118 size_t count = std::extent_v<T>;
120 DeleteArray<ElementType>(&ptr[0], count);
124 template <
typename T,
125 int&... kExplicitGuard,
126 typename ElementType = std::remove_extent_t<T>,
127 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
128 void Delete(ElementType* ptr,
size_t count) {
129 DeleteArray<ElementType>(ptr, count);
145 template <
typename ElementType>
147 if (!capabilities_.has(Capability::kSkipsDestroy)) {
148 std::destroy_n(ptr, count);
161 auto result = DoGetInfo(InfoType::kCapacity,
nullptr);
182 : capabilities_(capabilities) {}
192 template <
typename T,
193 int&... kExplicitGuard,
194 typename ElementType = std::remove_extent_t<T>,
195 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
197 ElementType* ptr,
size_t size) {
229 enum class InfoType {
288 Result<Layout> GetInfo(InfoType info_type,
const void* ptr)
const {
289 return DoGetInfo(info_type, ptr);
297 static Result<Layout> GetInfo(
const Deallocator& deallocator,
300 return deallocator.DoGetInfo(info_type, ptr);
305 Result<Layout> GetRequestedLayout(
const void* ptr)
const {
306 return DoGetInfo(InfoType::kRequestedLayoutOf, ptr);
311 static Result<Layout> GetRequestedLayout(
const Deallocator& deallocator,
313 return deallocator.GetRequestedLayout(ptr);
318 Result<Layout> GetUsableLayout(
const void* ptr)
const {
319 return DoGetInfo(InfoType::kUsableLayoutOf, ptr);
324 static Result<Layout> GetUsableLayout(
const Deallocator& deallocator,
326 return deallocator.GetUsableLayout(ptr);
331 Result<Layout> GetAllocatedLayout(
const void* ptr)
const {
332 return DoGetInfo(InfoType::kAllocatedLayoutOf, ptr);
337 static Result<Layout> GetAllocatedLayout(
const Deallocator& deallocator,
339 return deallocator.GetAllocatedLayout(ptr);
344 bool Recognizes(
const void* ptr)
const {
345 return DoGetInfo(InfoType::kRecognizes, ptr).ok();
350 static bool Recognizes(
const Deallocator& deallocator,
const void* ptr) {
351 return deallocator.Recognizes(ptr);
358 virtual void DoDeallocate([[maybe_unused]]
void* ptr) {
367 virtual void DoDeallocate(
void* ptr, Layout) { DoDeallocate(ptr); }
370 virtual Result<Layout> DoGetInfo(InfoType,
const void*)
const {
374 const Capabilities capabilities_;
Abstract interface for releasing memory.
Definition: deallocator.h:29
static constexpr Status Unimplemented()
Definition: status.h:280
Definition: status_with_size.h:51
Definition: unique_ptr.h:43
Definition: capability.h:65
static constexpr Layout Unwrap(const Result< Layout > &result)
Definition: layout.h:91
void Deallocate(void *ptr, Layout layout)
Definition: deallocator.h:69
void Delete(T *ptr)
Definition: deallocator.h:99
StatusWithSize GetCapacity() const
Definition: deallocator.h:160
void DeleteArray(ElementType *ptr, size_t count)
Definition: deallocator.h:146
void Deallocate(void *ptr)
Definition: deallocator.h:60
constexpr bool HasCapability(Capability capability) const
Returns whether a given capability is enabled for this object.
Definition: deallocator.h:50
Capability
Definition: capability.h:28
UniquePtr< T > WrapUnique(ElementType *ptr, size_t size)
Definition: deallocator.h:196
bool IsEqual(const Deallocator &other) const
Definition: deallocator.h:175
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
The Pigweed namespace.
Definition: alignment.h:27