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"
24namespace pw::allocator {
76 static void*
AddPrefix(
void* ptr,
size_t alignment);
116template <
typename BlockAllocatorType,
typename LockType = NoSync>
122 using BlockType =
typename BlockAllocatorType::BlockType;
125 :
Base(allocator.capabilities()), borrowable_(allocator, lock_) {
126 block_ = *(allocator.blocks().end());
157 bool DoResize(
void* ptr,
size_t new_size)
override;
175template <
typename BlockAllocatorType,
typename LockType>
177 auto allocator = borrowable_.acquire();
180 auto range = allocator->blocks();
181 BlockType* begin = *(range.begin());
182 BlockType* end = *(range.end());
190 if (block_ ==
nullptr || block_ == end) {
195 BlockType* prev = block_;
196 while (block_->IsFree()) {
197 BlockType* next = block_->Next();
210 void* ptr = block_->UsableSpace();
211 size_t size = block_->InnerSize();
212 return Base::CheckPrefixAndSuffix(ptr, size) ? nullptr : ptr;
215template <
typename BlockAllocatorType,
typename LockType>
217 auto allocator = borrowable_.acquire();
218 for (BlockType* block : allocator->blocks()) {
219 if (block->IsFree()) {
222 void* ptr = block->UsableSpace();
223 size_t size = block->InnerSize();
224 if (!Base::CheckPrefixAndSuffix(ptr, size)) {
231template <
typename BlockAllocatorType,
typename LockType>
234 layout = Base::AdjustLayout(layout);
235 auto allocator = borrowable_.acquire();
236 void* ptr = allocator->Allocate(layout);
237 if (ptr ==
nullptr) {
240 auto* block = BlockType::FromUsableSpace(ptr);
243 BlockType* prev = block->Prev();
244 if (prev !=
nullptr && !prev->IsFree()) {
245 Base::AddSuffix(prev->UsableSpace(), prev->InnerSize());
248 Base::AddSuffix(block->UsableSpace(), block->InnerSize());
249 return Base::AddPrefix(ptr, layout.alignment());
252template <
typename BlockAllocatorType,
typename LockType>
254 ptr = Base::GetOriginal(ptr);
257 auto allocator = borrowable_.acquire();
258 auto* block = BlockType::FromUsableSpace(ptr);
259 BlockType* prev = block->Prev();
260 if (block_ == block || block_ == block->Next()) {
263 allocator->Deallocate(ptr);
266 if (prev !=
nullptr && !prev->IsFree()) {
267 Base::AddSuffix(prev->UsableSpace(), prev->InnerSize());
271template <
typename BlockAllocatorType,
typename LockType>
274 ptr = Base::GetOriginal(ptr);
275 new_size = Base::AdjustSize(ptr, new_size);
278 auto allocator = borrowable_.acquire();
279 auto* block = BlockType::FromUsableSpace(ptr);
280 if (block_ == block->Next()) {
283 if (!allocator->Resize(ptr, new_size)) {
286 Base::AddSuffix(block->UsableSpace(), block->InnerSize());
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);
Definition: allocator.h:42
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
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: 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 void * GetOriginal(void *ptr)
static bool CheckPrefixAndSuffix(void *ptr, size_t size)
static Layout AdjustLayout(Layout layout)