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 private:
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 LockType lock_;
164
165 // Ideally, this would be annotated as being guarded `lock_`. However, the
166 // lock is always acquired via `borrowable_`, which does not support thread
167 // safety analysis.
168 BlockType* block_;
169};
170
172
173// Template method implementations.
174
175template <typename BlockAllocatorType, typename LockType>
177 auto allocator = borrowable_.acquire();
178
179 // Find the bounds of the block range.
180 auto range = allocator->blocks();
181 BlockType* begin = *(range.begin());
182 BlockType* end = *(range.end());
183
184 // Ensure there is at least one block.
185 if (begin == end) {
186 return nullptr;
187 }
188
189 // Ensure we are starting from a block.
190 if (block_ == nullptr || block_ == end) {
191 block_ = begin;
192 }
193
194 // Find the next used block.
195 BlockType* prev = block_;
196 while (block_->IsFree()) {
197 BlockType* next = block_->Next();
198 if (next == end) {
199 // Loop around.
200 next = begin;
201 }
202 if (next == prev) {
203 // All blocks are free.
204 return nullptr;
205 }
206 block_ = next;
207 }
208
209 // Validate the block.
210 void* ptr = block_->UsableSpace();
211 size_t size = block_->InnerSize();
212 return Base::CheckPrefixAndSuffix(ptr, size) ? nullptr : ptr;
213}
214
215template <typename BlockAllocatorType, typename LockType>
217 auto allocator = borrowable_.acquire();
218 for (BlockType* block : allocator->blocks()) {
219 if (block->IsFree()) {
220 continue;
221 }
222 void* ptr = block->UsableSpace();
223 size_t size = block->InnerSize();
224 if (!Base::CheckPrefixAndSuffix(ptr, size)) {
225 return ptr;
226 }
227 }
228 return nullptr;
229}
230
231template <typename BlockAllocatorType, typename LockType>
233 Layout layout) {
234 layout = Base::AdjustLayout(layout);
235 auto allocator = borrowable_.acquire();
236 void* ptr = allocator->Allocate(layout);
237 if (ptr == nullptr) {
238 return nullptr;
239 }
240 auto* block = BlockType::FromUsableSpace(ptr);
241
242 // Bytes may be shifted to the previous block.
243 BlockType* prev = block->Prev();
244 if (prev != nullptr && !prev->IsFree()) {
245 Base::AddSuffix(prev->UsableSpace(), prev->InnerSize());
246 }
247
248 Base::AddSuffix(block->UsableSpace(), block->InnerSize());
249 return Base::AddPrefix(ptr, layout.alignment());
250}
251
252template <typename BlockAllocatorType, typename LockType>
254 ptr = Base::GetOriginal(ptr);
255
256 // This block and or the next one may be merged on deallocation.
257 auto allocator = borrowable_.acquire();
258 auto* block = BlockType::FromUsableSpace(ptr);
259 BlockType* prev = block->Prev();
260 if (block_ == block || block_ == block->Next()) {
261 block_ = prev;
262 }
263 allocator->Deallocate(ptr);
264
265 // Bytes may have been shifted from the previous block.
266 if (prev != nullptr && !prev->IsFree()) {
267 Base::AddSuffix(prev->UsableSpace(), prev->InnerSize());
268 }
269}
270
271template <typename BlockAllocatorType, typename LockType>
273 size_t new_size) {
274 ptr = Base::GetOriginal(ptr);
275 new_size = Base::AdjustSize(ptr, new_size);
276
277 // The next block may be recreated on resizing.
278 auto allocator = borrowable_.acquire();
279 auto* block = BlockType::FromUsableSpace(ptr);
280 if (block_ == block->Next()) {
281 block_ = block;
282 }
283 if (!allocator->Resize(ptr, new_size)) {
284 return false;
285 }
286 Base::AddSuffix(block->UsableSpace(), block->InnerSize());
287 return true;
288}
289
290template <typename BlockAllocatorType, typename LockType>
292 InfoType info_type, const void* ptr) const {
293 ptr = const_cast<const void*>(Base::GetOriginal(const_cast<void*>(ptr)));
294 auto allocator = borrowable_.acquire();
295 return BlockAllocatorType::GetInfo(*allocator, info_type, ptr);
296}
297
298} // 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:253
void * ValidateAll()
Definition: guarded_allocator.h:216
bool DoResize(void *ptr, size_t new_size) override
Definition: guarded_allocator.h:272
void * DoAllocate(Layout layout) override
Definition: guarded_allocator.h:232
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: guarded_allocator.h:291
void * ValidateOne()
Definition: guarded_allocator.h:176
Definition: layout.h:58
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