Pigweed
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/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"
22
23namespace pw {
24
27 public:
29 using Capability = allocator::Capability;
31
32 virtual ~Deallocator() = default;
33
34 const Capabilities& capabilities() const { return capabilities_; }
35
37 bool HasCapability(Capability capability) const {
38 return capabilities_.has(capability);
39 }
40
47 void Deallocate(void* ptr) {
48 if (ptr != nullptr) {
49 DoDeallocate(ptr);
50 }
51 }
52
56 void Deallocate(void* ptr, Layout layout) {
57 if (ptr != nullptr) {
58 DoDeallocate(ptr, layout);
59 }
60 }
61
68 template <typename T>
69 void Delete(T* ptr) {
70 if (!capabilities_.has(Capability::kSkipsDestroy)) {
71 std::destroy_at(ptr);
72 }
73 Deallocate(ptr);
74 }
75
83 StatusWithSize GetCapacity() const {
84 auto result = DoGetInfo(InfoType::kCapacity, nullptr);
85 return StatusWithSize(result.status(), Layout::Unwrap(result).size());
86 }
87
98 bool IsEqual(const Deallocator& other) const { return this == &other; }
99
100 protected:
102 constexpr Deallocator() = default;
103
104 explicit constexpr Deallocator(const Capabilities& capabilities)
105 : capabilities_(capabilities) {}
106
110 template <typename T>
111 [[nodiscard]] UniquePtr<T> WrapUnique(T* ptr) {
113 }
114
143 enum class InfoType {
154 kRequestedLayoutOf,
155
159
166 kUsableLayoutOf,
167
178 kAllocatedLayoutOf,
179
185 kCapacity,
186
194 kRecognizes,
195 };
196
202 Result<Layout> GetInfo(InfoType info_type, const void* ptr) const {
203 return DoGetInfo(info_type, ptr);
204 }
205
211 static Result<Layout> GetInfo(const Deallocator& deallocator,
212 InfoType info_type,
213 const void* ptr) {
214 return deallocator.DoGetInfo(info_type, ptr);
215 }
216
219 Result<Layout> GetRequestedLayout(const void* ptr) const {
220 return DoGetInfo(InfoType::kRequestedLayoutOf, ptr);
221 }
222
225 static Result<Layout> GetRequestedLayout(const Deallocator& deallocator,
226 const void* ptr) {
227 return deallocator.GetRequestedLayout(ptr);
228 }
229
232 Result<Layout> GetUsableLayout(const void* ptr) const {
233 return DoGetInfo(InfoType::kUsableLayoutOf, ptr);
234 }
235
238 static Result<Layout> GetUsableLayout(const Deallocator& deallocator,
239 const void* ptr) {
240 return deallocator.GetUsableLayout(ptr);
241 }
242
245 Result<Layout> GetAllocatedLayout(const void* ptr) const {
246 return DoGetInfo(InfoType::kAllocatedLayoutOf, ptr);
247 }
248
251 static Result<Layout> GetAllocatedLayout(const Deallocator& deallocator,
252 const void* ptr) {
253 return deallocator.GetAllocatedLayout(ptr);
254 }
255
258 bool Recognizes(const void* ptr) const {
259 return DoGetInfo(InfoType::kRecognizes, ptr).ok();
260 }
261
264 static bool Recognizes(const Deallocator& deallocator, const void* ptr) {
265 return deallocator.Recognizes(ptr);
266 }
267
268 private:
272 virtual void DoDeallocate(void*) {
273 // This method will be pure virtual once consumer migrate from the deprected
274 // version that takes a `Layout` parameter. In the meantime, the check that
275 // this method is implemented is deferred to run-time.
276 PW_ASSERT(false);
277 }
278
281 virtual void DoDeallocate(void* ptr, Layout) { DoDeallocate(ptr); }
282
284 virtual Result<Layout> DoGetInfo(InfoType, const void*) const {
285 return Status::Unimplemented();
286 }
287
288 const Capabilities capabilities_;
289};
290
291namespace allocator {
292
293// Alias for module consumers using the older name for the above type.
294using Deallocator = ::pw::Deallocator;
295
296} // namespace allocator
297} // namespace pw
Abstract interface for releasing memory.
Definition: deallocator.h:26
void Deallocate(void *ptr, Layout layout)
Definition: deallocator.h:56
bool HasCapability(Capability capability) const
Returns whether a given capabilityis enabled for this object.
Definition: deallocator.h:37
StatusWithSize GetCapacity() const
Definition: deallocator.h:83
UniquePtr< T > WrapUnique(T *ptr)
Definition: deallocator.h:111
void Deallocate(void *ptr)
Definition: deallocator.h:47
void Delete(T *ptr)
Definition: deallocator.h:69
bool IsEqual(const Deallocator &other) const
Definition: deallocator.h:98
constexpr Deallocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
Definition: unique_ptr.h:47
Definition: capability.h:64
Definition: layout.h:39
static constexpr Layout Unwrap(const Result< Layout > &result)
Definition: layout.h:55
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27