16#include "pw_allocator/capability.h"
17#include "pw_allocator/layout.h"
18#include "pw_allocator/unique_ptr.h"
19#include "pw_result/result.h"
20#include "pw_status/status.h"
21#include "pw_status/status_with_size.h"
30 static constexpr bool is_bounded_array_v =
31 allocator::internal::is_bounded_array_v<T>;
34 static constexpr bool is_unbounded_array_v =
35 allocator::internal::is_unbounded_array_v<T>;
39 using Capability = allocator::Capability;
44 constexpr const Capabilities& capabilities()
const {
return capabilities_; }
48 return capabilities_.has(capability);
68 DoDeallocate(ptr, layout);
80 if (!capabilities_.has(Capability::kSkipsDestroy)) {
94 auto result = DoGetInfo(InfoType::kCapacity,
nullptr);
112 template <
typename T,
113 int&... kExplicitGuard,
114 std::enable_if_t<is_bounded_array_v<T>,
int> = 0,
116 void WrapUnique(Args&&...) =
delete;
123 : capabilities_(capabilities) {}
128 template <
typename T, std::enable_if_t<!std::is_array_v<T>,
int> = 0>
137 template <
typename T,
138 int&... kExplicitGuard,
139 typename ElementType = std::remove_extent_t<T>,
140 std::enable_if_t<is_unbounded_array_v<T>,
int> = 0>
149 template <
typename T>
151 return WrapUnique<T[]>(ptr, size);
182 enum class InfoType {
241 Result<Layout> GetInfo(InfoType info_type,
const void* ptr)
const {
242 return DoGetInfo(info_type, ptr);
250 static Result<Layout> GetInfo(
const Deallocator& deallocator,
253 return deallocator.DoGetInfo(info_type, ptr);
258 Result<Layout> GetRequestedLayout(
const void* ptr)
const {
259 return DoGetInfo(InfoType::kRequestedLayoutOf, ptr);
264 static Result<Layout> GetRequestedLayout(
const Deallocator& deallocator,
266 return deallocator.GetRequestedLayout(ptr);
271 Result<Layout> GetUsableLayout(
const void* ptr)
const {
272 return DoGetInfo(InfoType::kUsableLayoutOf, ptr);
277 static Result<Layout> GetUsableLayout(
const Deallocator& deallocator,
279 return deallocator.GetUsableLayout(ptr);
284 Result<Layout> GetAllocatedLayout(
const void* ptr)
const {
285 return DoGetInfo(InfoType::kAllocatedLayoutOf, ptr);
290 static Result<Layout> GetAllocatedLayout(
const Deallocator& deallocator,
292 return deallocator.GetAllocatedLayout(ptr);
297 bool Recognizes(
const void* ptr)
const {
298 return DoGetInfo(InfoType::kRecognizes, ptr).ok();
303 static bool Recognizes(
const Deallocator& deallocator,
const void* ptr) {
304 return deallocator.Recognizes(ptr);
311 virtual void DoDeallocate(
void*) {
320 virtual void DoDeallocate(
void* ptr, Layout) { DoDeallocate(ptr); }
323 virtual Result<Layout> DoGetInfo(InfoType,
const void*)
const {
324 return Status::Unimplemented();
327 const Capabilities capabilities_;
Abstract interface for releasing memory.
Definition: deallocator.h:26
void Deallocate(void *ptr, Layout layout)
Definition: deallocator.h:66
bool HasCapability(Capability capability) const
Returns whether a given capabilityis enabled for this object.
Definition: deallocator.h:47
StatusWithSize GetCapacity() const
Definition: deallocator.h:93
void Deallocate(void *ptr)
Definition: deallocator.h:57
UniquePtr< T[]> WrapUniqueArray(T *ptr, size_t size)
Definition: deallocator.h:150
void Delete(T *ptr)
Definition: deallocator.h:79
UniquePtr< T > WrapUnique(ElementType *ptr, size_t size)
Definition: deallocator.h:141
UniquePtr< T > WrapUnique(T *ptr)
Definition: deallocator.h:129
bool IsEqual(const Deallocator &other) const
Definition: deallocator.h:108
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
Definition: status_with_size.h:49
Definition: unique_ptr.h:40
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