C/C++ API Reference
Loading...
Searching...
No Matches
guarded_allocator.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 <cstddef>
17
18#include "pw_allocator/allocator.h"
19#include "pw_allocator/layout.h"
20#include "pw_allocator/synchronized_allocator.h"
21#include "pw_result/result.h"
22#include "pw_sync/borrow.h"
23
24namespace pw::allocator {
25namespace internal {
26
54 protected:
55 constexpr explicit GenericGuardedAllocator(const Capabilities& capabilities)
56 : Allocator(capabilities) {}
57
62 static Layout AdjustLayout(Layout layout);
63
68 static size_t AdjustSize(void* ptr, size_t inner_size);
69
72 static void* GetOriginal(void* ptr);
73
76 static void* AddPrefix(void* ptr, size_t alignment);
77
79 static void AddSuffix(void* ptr, size_t size);
80
83 static bool CheckPrefixAndSuffix(void* ptr, size_t size);
84};
85
86} // namespace internal
87
89
116template <typename BlockAllocatorType, typename LockType = NoSync>
118 private:
120
121 public:
122 using BlockType = typename BlockAllocatorType::BlockType;
123
124 constexpr explicit GuardedAllocator(BlockAllocatorType& allocator)
125 : Base(allocator.capabilities()), borrowable_(allocator, lock_) {
126 block_ = *(allocator.blocks().end());
127 }
128
137 void* ValidateOne();
138
147 void* ValidateAll();
148
149 protected:
151 void* DoAllocate(Layout layout) override;
152
154 void DoDeallocate(void* ptr) override;
155
157 bool DoResize(void* ptr, size_t new_size) override;
158
160 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override;
161
162 private:
163 LockType lock_;
165
166 // Ideally, this would be annotated as being guarded `lock_`. However, the
167 // lock is always acquired via `borrowable_`, which does not support thread
168 // safety analysis.
169 BlockType* block_;
170};
171
173
174// Template method implementations.
175
176template <typename BlockAllocatorType, typename LockType>
178 auto allocator = borrowable_.acquire();
179
180 // Find the bounds of the block range.
181 auto range = allocator->blocks();
182 BlockType* begin = *(range.begin());
183 BlockType* end = *(range.end());
184
185 // Ensure there is at least one block.
186 if (begin == end) {
187 return nullptr;
188 }
189
190 // Ensure we are starting from a block.
191 if (block_ == nullptr || block_ == end) {
192 block_ = begin;
193 }
194
195 // Find the next used block.
196 BlockType* prev = block_;
197 while (block_->IsFree()) {
198 BlockType* next = block_->Next();
199 if (next == end) {
200 // Loop around.
201 next = begin;
202 }
203 if (next == prev) {
204 // All blocks are free.
205 return nullptr;
206 }
207 block_ = next;
208 }
209
210 // Validate the block.
211 void* ptr = block_->UsableSpace();
212 size_t size = block_->InnerSize();
213 return Base::CheckPrefixAndSuffix(ptr, size) ? nullptr : ptr;
214}
215
216template <typename BlockAllocatorType, typename LockType>
218 auto allocator = borrowable_.acquire();
219 for (BlockType* block : allocator->blocks()) {
220 if (block->IsFree()) {
221 continue;
222 }
223 void* ptr = block->UsableSpace();
224 size_t size = block->InnerSize();
225 if (!Base::CheckPrefixAndSuffix(ptr, size)) {
226 return ptr;
227 }
228 }
229 return nullptr;
230}
231
232template <typename BlockAllocatorType, typename LockType>
234 Layout layout) {
235 layout = Base::AdjustLayout(layout);
236 auto allocator = borrowable_.acquire();
237 void* ptr = allocator->Allocate(layout);
238 if (ptr == nullptr) {
239 return nullptr;
240 }
241 auto* block = BlockType::FromUsableSpace(ptr);
242
243 // Bytes may be shifted to the previous block.
244 BlockType* prev = block->Prev();
245 if (prev != nullptr && !prev->IsFree()) {
246 Base::AddSuffix(prev->UsableSpace(), prev->InnerSize());
247 }
248
249 Base::AddSuffix(block->UsableSpace(), block->InnerSize());
250 return Base::AddPrefix(ptr, layout.alignment());
251}
252
253template <typename BlockAllocatorType, typename LockType>
255 ptr = Base::GetOriginal(ptr);
256
257 // This block and or the next one may be merged on deallocation.
258 auto allocator = borrowable_.acquire();
259 auto* block = BlockType::FromUsableSpace(ptr);
260 BlockType* prev = block->Prev();
261 if (block_ == block || block_ == block->Next()) {
262 block_ = prev;
263 }
264 allocator->Deallocate(ptr);
265
266 // Bytes may have been shifted from the previous block.
267 if (prev != nullptr && !prev->IsFree()) {
268 Base::AddSuffix(prev->UsableSpace(), prev->InnerSize());
269 }
270}
271
272template <typename BlockAllocatorType, typename LockType>
274 size_t new_size) {
275 ptr = Base::GetOriginal(ptr);
276 new_size = Base::AdjustSize(ptr, new_size);
277
278 // The next block may be recreated on resizing.
279 auto allocator = borrowable_.acquire();
280 auto* block = BlockType::FromUsableSpace(ptr);
281 if (block_ == block->Next()) {
282 block_ = block;
283 }
284 if (!allocator->Resize(ptr, new_size)) {
285 return false;
286 }
287 Base::AddSuffix(block->UsableSpace(), block->InnerSize());
288 return true;
289}
290
291template <typename BlockAllocatorType, typename LockType>
293 InfoType info_type, const void* ptr) const {
294 ptr = const_cast<const void*>(Base::GetOriginal(const_cast<void*>(ptr)));
295 auto allocator = borrowable_.acquire();
296 return BlockAllocatorType::GetInfo(*allocator, info_type, ptr);
297}
298
299} // namespace pw::allocator
Definition: allocator.h:42
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
Definition: result.h:145
Definition: capability.h:65
Definition: guarded_allocator.h:117
void DoDeallocate(void *ptr) override
Definition: guarded_allocator.h:254
void * ValidateAll()
Definition: guarded_allocator.h:217
bool DoResize(void *ptr, size_t new_size) override
Definition: guarded_allocator.h:273
void * DoAllocate(Layout layout) override
Definition: guarded_allocator.h:233
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: guarded_allocator.h:292
void * ValidateOne()
Definition: guarded_allocator.h:177
Definition: layout.h:64
Definition: guarded_allocator.h:53
static size_t AdjustSize(void *ptr, size_t inner_size)
static void AddSuffix(void *ptr, size_t size)
Adds a suffix a the end of the allocation given by ptr and size.
static void * AddPrefix(void *ptr, size_t alignment)
static bool CheckPrefixAndSuffix(void *ptr, size_t size)
Definition: borrow.h:164