C/C++ API Reference
Loading...
Searching...
No Matches
deallocator.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
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"
23
24namespace pw {
25
27
30 protected:
31 // Alias types and variables needed for SFINAE below.
32 template <typename T>
33 static constexpr bool is_bounded_array_v =
34 allocator::internal::is_bounded_array_v<T>;
35
36 template <typename T>
37 static constexpr bool is_unbounded_array_v =
38 allocator::internal::is_unbounded_array_v<T>;
39
40 public:
44
45 virtual ~Deallocator() = default;
46
47 constexpr const Capabilities& capabilities() const { return capabilities_; }
48
50 bool HasCapability(Capability capability) const {
51 return capabilities_.has(capability);
52 }
53
60 void Deallocate(void* ptr) {
61 if (ptr != nullptr) {
62 DoDeallocate(ptr);
63 }
64 }
65
69 void Deallocate(void* ptr, Layout layout) {
70 if (ptr != nullptr) {
71 DoDeallocate(ptr, layout);
72 }
73 }
74
96 template <typename T,
97 int&... kExplicitGuard,
98 std::enable_if_t<!std::is_array_v<T>, int> = 0>
99 void Delete(T* ptr) {
100 if constexpr (allocator::Hardening::kIncludesDebugChecks) {
101 if (auto result = GetRequestedLayout(ptr); result.ok()) {
102 PW_ASSERT(*result == Layout::Of<T>());
103 }
104 }
105 DeleteArray<T>(ptr, 1);
106 }
107
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>;
114 if (count != 0) {
115 DeleteArray<ElementType>(&ptr[0], count);
116 }
117 }
118
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);
125 }
127
140 template <typename ElementType>
141 void DeleteArray(ElementType* ptr, size_t count) {
142 if (!capabilities_.has(Capability::kSkipsDestroy)) {
143 std::destroy_n(ptr, count);
144 }
145 Deallocate(ptr);
146 }
147
156 auto result = DoGetInfo(InfoType::kCapacity, nullptr);
157 return StatusWithSize(result.status(), Layout::Unwrap(result).size());
158 }
159
170 bool IsEqual(const Deallocator& other) const { return this == &other; }
171
172 protected:
174 constexpr Deallocator() = default;
175
176 explicit constexpr Deallocator(const Capabilities& capabilities)
177 : capabilities_(capabilities) {}
178
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>
191 [[deprecated("Use `UniquePtr<T>(...)` instead.")]] UniquePtr<T> WrapUnique(
192 ElementType* ptr, size_t size) {
193 return UniquePtr<T>(ptr, size, this);
194 }
195
224 enum class InfoType {
235 kRequestedLayoutOf,
236
247 kUsableLayoutOf,
248
259 kAllocatedLayoutOf,
260
266 kCapacity,
267
275 kRecognizes,
276 };
277
283 Result<Layout> GetInfo(InfoType info_type, const void* ptr) const {
284 return DoGetInfo(info_type, ptr);
285 }
286
292 static Result<Layout> GetInfo(const Deallocator& deallocator,
293 InfoType info_type,
294 const void* ptr) {
295 return deallocator.DoGetInfo(info_type, ptr);
296 }
297
300 Result<Layout> GetRequestedLayout(const void* ptr) const {
301 return DoGetInfo(InfoType::kRequestedLayoutOf, ptr);
302 }
303
306 static Result<Layout> GetRequestedLayout(const Deallocator& deallocator,
307 const void* ptr) {
308 return deallocator.GetRequestedLayout(ptr);
309 }
310
313 Result<Layout> GetUsableLayout(const void* ptr) const {
314 return DoGetInfo(InfoType::kUsableLayoutOf, ptr);
315 }
316
319 static Result<Layout> GetUsableLayout(const Deallocator& deallocator,
320 const void* ptr) {
321 return deallocator.GetUsableLayout(ptr);
322 }
323
326 Result<Layout> GetAllocatedLayout(const void* ptr) const {
327 return DoGetInfo(InfoType::kAllocatedLayoutOf, ptr);
328 }
329
332 static Result<Layout> GetAllocatedLayout(const Deallocator& deallocator,
333 const void* ptr) {
334 return deallocator.GetAllocatedLayout(ptr);
336
339 bool Recognizes(const void* ptr) const {
340 return DoGetInfo(InfoType::kRecognizes, ptr).ok();
341 }
342
345 static bool Recognizes(const Deallocator& deallocator, const void* ptr) {
346 return deallocator.Recognizes(ptr);
347 }
348
349 private:
353 virtual void DoDeallocate([[maybe_unused]] void* ptr) {
354 // This method will be pure virtual once consumer migrate from the
355 // deprecated version that takes a `Layout` parameter. In the meantime, the
356 // check that this method is implemented is deferred to run-time.
357 PW_ASSERT(false);
358 }
359
362 virtual void DoDeallocate(void* ptr, Layout) { DoDeallocate(ptr); }
363
365 virtual Result<Layout> DoGetInfo(InfoType, const void*) const {
366 return Status::Unimplemented();
367 }
368
369 const Capabilities capabilities_;
370};
372namespace allocator {
373
374// Alias for module consumers using the older name for the above type.
375using Deallocator = ::pw::Deallocator;
376
377} // namespace allocator
378
380
381} // namespace pw
Abstract interface for releasing memory.
Definition: deallocator.h:29
Definition: status_with_size.h:49
Definition: unique_ptr.h:43
Definition: capability.h:64
Definition: layout.h:58
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