16#include "pw_allocator/capability.h"
17#include "pw_allocator/hardening.h"
18#include "pw_allocator/layout.h"
19#include "pw_result/result.h"
20#include "pw_status/status.h"
21#include "pw_status/status_with_size.h"
32 static constexpr bool is_bounded_array_v =
33 allocator::internal::is_bounded_array_v<T>;
36 static constexpr bool is_unbounded_array_v =
37 allocator::internal::is_unbounded_array_v<T>;
46 constexpr const Capabilities& capabilities()
const {
return capabilities_; }
50 return capabilities_.has(capability);
87 int&... kExplicitGuard,
88 std::enable_if_t<!std::is_array_v<T>,
int> = 0>
90 if constexpr (allocator::Hardening::kIncludesDebugChecks) {
91 if (
auto result = GetRequestedLayout(ptr); result.ok()) {
92 if constexpr (std::has_virtual_destructor_v<T>) {
93 PW_ASSERT(result->size() >=
sizeof(T) &&
94 result->alignment() >=
alignof(T));
96 PW_ASSERT(*result == Layout::Of<T>());
100 DeleteArray<T>(ptr, 1);
103 template <
typename T,
104 int&... kExplicitGuard,
105 typename ElementType = std::remove_extent_t<T>,
106 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
107 void Delete(ElementType* ptr) {
108 size_t count = std::extent_v<T>;
110 DeleteArray<ElementType>(&ptr[0], count);
114 template <
typename T,
115 int&... kExplicitGuard,
116 typename ElementType = std::remove_extent_t<T>,
117 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
118 void Delete(ElementType* ptr,
size_t count) {
119 DeleteArray<ElementType>(ptr, count);
135 template <
typename ElementType>
137 if (!capabilities_.has(Capability::kSkipsDestroy)) {
138 std::destroy_n(ptr, count);
151 auto result = DoGetInfo(InfoType::kCapacity,
nullptr);
172 : capabilities_(capabilities) {}
202 enum class InfoType {
261 Result<Layout> GetInfo(InfoType info_type,
const void* ptr)
const {
262 return DoGetInfo(info_type, ptr);
270 static Result<Layout> GetInfo(
const Deallocator& deallocator,
273 return deallocator.DoGetInfo(info_type, ptr);
278 Result<Layout> GetRequestedLayout(
const void* ptr)
const {
279 return DoGetInfo(InfoType::kRequestedLayoutOf, ptr);
284 static Result<Layout> GetRequestedLayout(
const Deallocator& deallocator,
286 return deallocator.GetRequestedLayout(ptr);
291 Result<Layout> GetUsableLayout(
const void* ptr)
const {
292 return DoGetInfo(InfoType::kUsableLayoutOf, ptr);
297 static Result<Layout> GetUsableLayout(
const Deallocator& deallocator,
299 return deallocator.GetUsableLayout(ptr);
304 Result<Layout> GetAllocatedLayout(
const void* ptr)
const {
305 return DoGetInfo(InfoType::kAllocatedLayoutOf, ptr);
310 static Result<Layout> GetAllocatedLayout(
const Deallocator& deallocator,
312 return deallocator.GetAllocatedLayout(ptr);
317 bool Recognizes(
const void* ptr)
const {
318 return DoGetInfo(InfoType::kRecognizes, ptr).ok();
323 static bool Recognizes(
const Deallocator& deallocator,
const void* ptr) {
324 return deallocator.Recognizes(ptr);
331 virtual void DoDeallocate([[maybe_unused]]
void* ptr) = 0;
334 virtual Result<Layout> DoGetInfo(InfoType,
const void*)
const {
338 const Capabilities capabilities_;
Abstract interface for releasing memory.
Definition: deallocator.h:28
static constexpr Status Unimplemented()
Definition: status.h:280
Definition: status_with_size.h:51
Definition: capability.h:65
static constexpr Layout Unwrap(const Result< Layout > &result)
Definition: layout.h:91
void Delete(T *ptr)
Definition: deallocator.h:89
StatusWithSize GetCapacity() const
Definition: deallocator.h:150
void DeleteArray(ElementType *ptr, size_t count)
Definition: deallocator.h:136
void Deallocate(void *ptr)
Definition: deallocator.h:59
constexpr bool HasCapability(Capability capability) const
Returns whether a given capability is enabled for this object.
Definition: deallocator.h:49
Capability
Definition: capability.h:28
bool IsEqual(const Deallocator &other) const
Definition: deallocator.h:165
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
The Pigweed namespace.
Definition: alignment.h:27