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;
176template <
typename BlockAllocatorType,
typename LockType>
178 auto allocator = borrowable_.acquire();
181 auto range = allocator->blocks();
182 BlockType* begin = *(range.begin());
183 BlockType* end = *(range.end());
191 if (block_ ==
nullptr || block_ == end) {
196 BlockType* prev = block_;
197 while (block_->IsFree()) {
198 BlockType* next = block_->Next();
211 void* ptr = block_->UsableSpace();
212 size_t size = block_->InnerSize();
213 return Base::CheckPrefixAndSuffix(ptr, size) ? nullptr : ptr;
216template <
typename BlockAllocatorType,
typename LockType>
218 auto allocator = borrowable_.acquire();
219 for (BlockType* block : allocator->blocks()) {
220 if (block->IsFree()) {
223 void* ptr = block->UsableSpace();
224 size_t size = block->InnerSize();
225 if (!Base::CheckPrefixAndSuffix(ptr, size)) {
232template <
typename BlockAllocatorType,
typename LockType>
235 layout = Base::AdjustLayout(layout);
236 auto allocator = borrowable_.acquire();
237 void* ptr = allocator->Allocate(layout);
238 if (ptr ==
nullptr) {
241 auto* block = BlockType::FromUsableSpace(ptr);
244 BlockType* prev = block->Prev();
245 if (prev !=
nullptr && !prev->IsFree()) {
246 Base::AddSuffix(prev->UsableSpace(), prev->InnerSize());
249 Base::AddSuffix(block->UsableSpace(), block->InnerSize());
250 return Base::AddPrefix(ptr, layout.alignment());
253template <
typename BlockAllocatorType,
typename LockType>
255 ptr = Base::GetOriginal(ptr);
258 auto allocator = borrowable_.acquire();
259 auto* block = BlockType::FromUsableSpace(ptr);
260 BlockType* prev = block->Prev();
261 if (block_ == block || block_ == block->Next()) {
264 allocator->Deallocate(ptr);
267 if (prev !=
nullptr && !prev->IsFree()) {
268 Base::AddSuffix(prev->UsableSpace(), prev->InnerSize());
272template <
typename BlockAllocatorType,
typename LockType>
275 ptr = Base::GetOriginal(ptr);
276 new_size = Base::AdjustSize(ptr, new_size);
279 auto allocator = borrowable_.acquire();
280 auto* block = BlockType::FromUsableSpace(ptr);
281 if (block_ == block->Next()) {
284 if (!allocator->Resize(ptr, new_size)) {
287 Base::AddSuffix(block->UsableSpace(), block->InnerSize());
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);
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: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: 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)