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 PW_ASSERT(*result == Layout::Of<T>());
105 DeleteArray<T>(ptr, 1);
108 template <
typename T,
109 int&... kExplicitGuard,
110 typename ElementType = std::remove_extent_t<T>,
111 std::enable_if_t<is_bounded_array_v<T>,
int> = 0>
112 void Delete(ElementType* ptr) {
113 size_t count = std::extent_v<T>;
115 DeleteArray<ElementType>(&ptr[0], count);
119 template <
typename T,
120 int&... kExplicitGuard,
121 typename ElementType = std::remove_extent_t<T>,
122 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
123 void Delete(ElementType* ptr,
size_t count) {
124 DeleteArray<ElementType>(ptr, count);
140 template <
typename ElementType>
142 if (!capabilities_.has(Capability::kSkipsDestroy)) {
143 std::destroy_n(ptr, count);
156 auto result = DoGetInfo(InfoType::kCapacity,
nullptr);
177 : capabilities_(capabilities) {}
187 template <
typename T,
188 int&... kExplicitGuard,
189 typename ElementType = std::remove_extent_t<T>,
190 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
192 ElementType* ptr,
size_t size) {
224 enum class InfoType {
283 Result<Layout> GetInfo(InfoType info_type,
const void* ptr)
const {
284 return DoGetInfo(info_type, ptr);
292 static Result<Layout> GetInfo(
const Deallocator& deallocator,
295 return deallocator.DoGetInfo(info_type, ptr);
300 Result<Layout> GetRequestedLayout(
const void* ptr)
const {
301 return DoGetInfo(InfoType::kRequestedLayoutOf, ptr);
306 static Result<Layout> GetRequestedLayout(
const Deallocator& deallocator,
308 return deallocator.GetRequestedLayout(ptr);
313 Result<Layout> GetUsableLayout(
const void* ptr)
const {
314 return DoGetInfo(InfoType::kUsableLayoutOf, ptr);
319 static Result<Layout> GetUsableLayout(
const Deallocator& deallocator,
321 return deallocator.GetUsableLayout(ptr);
326 Result<Layout> GetAllocatedLayout(
const void* ptr)
const {
327 return DoGetInfo(InfoType::kAllocatedLayoutOf, ptr);
332 static Result<Layout> GetAllocatedLayout(
const Deallocator& deallocator,
334 return deallocator.GetAllocatedLayout(ptr);
339 bool Recognizes(
const void* ptr)
const {
340 return DoGetInfo(InfoType::kRecognizes, ptr).ok();
345 static bool Recognizes(
const Deallocator& deallocator,
const void* ptr) {
346 return deallocator.Recognizes(ptr);
353 virtual void DoDeallocate([[maybe_unused]]
void* ptr) {
362 virtual void DoDeallocate(
void* ptr, Layout) { DoDeallocate(ptr); }
365 virtual Result<Layout> DoGetInfo(InfoType,
const void*)
const {
366 return Status::Unimplemented();
369 const Capabilities capabilities_;
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: status_with_size.h:49
Definition: unique_ptr.h:43
Definition: capability.h:64
static constexpr Layout Unwrap(const Result< Layout > &result)
Definition: layout.h:91
void Deallocate(void *ptr, Layout layout)
Definition: deallocator.h:69
bool HasCapability(Capability capability) const
Returns whether a given capability is enabled for this object.
Definition: deallocator.h:50
void Delete(T *ptr)
Definition: deallocator.h:99
StatusWithSize GetCapacity() const
Definition: deallocator.h:155
void DeleteArray(ElementType *ptr, size_t count)
Definition: deallocator.h:141
void Deallocate(void *ptr)
Definition: deallocator.h:60
Capability
Definition: capability.h:28
UniquePtr< T > WrapUnique(ElementType *ptr, size_t size)
Definition: deallocator.h:191
bool IsEqual(const Deallocator &other) const
Definition: deallocator.h:170
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
The Pigweed namespace.
Definition: alignment.h:27