18#include "pw_allocator/allocator.h"
19#include "pw_allocator/block/basic.h"
20#include "pw_allocator/block/iterable.h"
21#include "pw_allocator/block/poisonable.h"
22#include "pw_allocator/block/result.h"
23#include "pw_allocator/block/with_layout.h"
24#include "pw_allocator/capability.h"
25#include "pw_allocator/config.h"
26#include "pw_allocator/fragmentation.h"
27#include "pw_allocator/hardening.h"
28#include "pw_assert/assert.h"
29#include "pw_bytes/span.h"
30#include "pw_result/result.h"
31#include "pw_status/status.h"
33namespace pw::allocator {
54 template <
typename BlockType>
57 kImplementsGetAllocatedLayout |
58 kImplementsGetCapacity | kImplementsRecognizes;
59 if constexpr (has_layout_v<BlockType>) {
60 return common | kImplementsGetRequestedLayout;
89template <
typename,
size_t>
105template <
typename BlockType_>
111 using BlockType = BlockType_;
112 using Range =
typename BlockType::Range;
115 Base::GetCapabilities<BlockType>();
180 template <
typename Ptr>
190 using BlockResultPrev = internal::GenericBlockResult::Prev;
191 using BlockResultNext = internal::GenericBlockResult::Next;
194 template <
typename,
size_t>
207 bool DoResize(
void* ptr,
size_t new_size)
override;
259 static bool PrevIsFree(
const BlockType* block) {
260 auto* prev = block->Prev();
261 return prev !=
nullptr && prev->IsFree();
265 static bool NextIsFree(
const BlockType* block) {
266 auto* next = block->Next();
267 return next !=
nullptr && next->IsFree();
272 void UpdateLast(BlockType* block);
275 size_t capacity_ = 0;
276 size_t allocated_ = 0;
277 BlockType* first_ =
nullptr;
278 BlockType* last_ =
nullptr;
279 uint16_t unpoisoned_ = 0;
286template <
typename BlockType>
287BlockAllocator<BlockType>::~BlockAllocator() {
288 if constexpr (Hardening::kIncludesRobustChecks) {
289 for (
auto* block : blocks()) {
290 if (!block->IsFree()) {
291 CrashOnAllocated(block);
297template <
typename BlockType>
300 return Range(first_);
303template <
typename BlockType>
309template <
typename BlockType>
311 if constexpr (Hardening::kIncludesRobustChecks) {
312 PW_ASSERT(begin !=
nullptr);
313 PW_ASSERT(begin->Prev() ==
nullptr);
316 for (
auto* block : blocks()) {
318 capacity_ += block->OuterSize();
319 if (block->IsFree()) {
320 RecycleBlock(*block);
325template <
typename BlockType>
327 if (capacity_ == 0) {
332 if constexpr (Hardening::kIncludesDebugChecks) {
333 PW_ASSERT(last_->Next() ==
nullptr);
335 auto result = ChooseBlock(layout);
340 BlockType* block = result.block();
341 allocated_ += block->OuterSize();
342 switch (result.prev()) {
343 case BlockResultPrev::kSplitNew:
345 RecycleBlock(*(block->Prev()));
347 case BlockResultPrev::kResizedLarger:
349 allocated_ += result.size();
351 case BlockResultPrev::kUnchanged:
352 case BlockResultPrev::kResizedSmaller:
355 if (result.next() == BlockResultNext::kSplitNew) {
356 RecycleBlock(*(block->Next()));
360 if constexpr (Hardening::kIncludesDebugChecks) {
361 PW_ASSERT(block <= last_);
364 return block->UsableSpace();
367template <
typename BlockType>
369 BlockType* block = FromUsableSpace(ptr);
370 if (block->IsFree()) {
371 if constexpr (Hardening::kIncludesBasicChecks) {
372 CrashOnDoubleFree(block);
377 DeallocateBlock(std::move(block));
380template <
typename BlockType>
383 if (
auto* prev = block->Prev(); prev !=
nullptr && prev->IsFree()) {
386 if (
auto* next = block->Next(); next !=
nullptr && next->IsFree()) {
391 allocated_ -= block->OuterSize();
392 auto free_result = BlockType::Free(std::move(block));
393 block = free_result.block();
396 if (free_result.prev() == BlockResultPrev::kResizedSmaller) {
398 allocated_ -= free_result.size();
401 if constexpr (is_poisonable_v<BlockType> && kPoisonInterval != 0) {
403 if (unpoisoned_ >= kPoisonInterval) {
409 RecycleBlock(*block);
412template <
typename BlockType>
414 BlockType* block = FromUsableSpace(ptr);
417 if (
auto* next = block->Next(); next !=
nullptr && next->IsFree()) {
421 size_t old_size = block->OuterSize();
422 if (!block->Resize(new_size).ok()) {
425 allocated_ -= old_size;
426 allocated_ += block->OuterSize();
429 if (
auto* next = block->Next(); next !=
nullptr && next->IsFree()) {
436template <
typename BlockType>
438 const void* ptr)
const {
440 if (info_type == InfoType::kCapacity) {
444 if (ptr < first_->UsableSpace() || last_->UsableSpace() < ptr) {
447 const auto* block = BlockType::FromUsableSpace(ptr);
448 if (!block->IsValid()) {
451 if (block->IsFree()) {
454 if constexpr (kCapabilities.has(kImplementsGetRequestedLayout)) {
455 if (info_type == InfoType::kRequestedLayoutOf) {
456 return block->RequestedLayout();
460 case InfoType::kUsableLayoutOf:
461 return Layout(block->InnerSize(), BlockType::kAlignment);
462 case InfoType::kAllocatedLayoutOf:
463 return Layout(block->OuterSize(), BlockType::kAlignment);
464 case InfoType::kRecognizes:
466 case InfoType::kCapacity:
467 case InfoType::kRequestedLayoutOf:
473template <
typename BlockType>
476 for (
auto block : blocks()) {
477 if (block->IsFree()) {
478 fragmentation.AddFragment(block->InnerSize() / BlockType::kAlignment);
481 return fragmentation;
484template <
typename BlockType>
485template <
typename Ptr>
486internal::copy_const_ptr_t<Ptr, BlockType*>
488 if (ptr < first_->UsableSpace() || last_->UsableSpace() < ptr) {
489 if constexpr (Hardening::kIncludesBasicChecks) {
490 CrashOnOutOfRange(ptr);
494 auto* block = BlockType::FromUsableSpace(ptr);
495 if (!block->IsValid()) {
496 if constexpr (Hardening::kIncludesBasicChecks) {
497 block->CheckInvariants();
504template <
typename BlockType>
506 BlockType* next = block->Next();
507 if (next ==
nullptr) {
509 }
else if (next->Next() ==
nullptr) {
Definition: allocator.h:36
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
static constexpr Status DataLoss()
Definition: status.h:316
static constexpr Status Unimplemented()
Definition: status.h:280
static constexpr Status FailedPrecondition()
Definition: status.h:243
static constexpr Status NotFound()
Definition: status.h:190
Definition: block_allocator.h:106
void * DoAllocate(Layout layout) override
Definition: block_allocator.h:326
size_t DoGetAllocated() const override
Definition: block_allocator.h:210
Fragmentation MeasureFragmentation() const
Returns fragmentation information for the block allocator's memory region.
Definition: block_allocator.h:474
virtual size_t DoGetMaxAllocatable()=0
internal::copy_const_ptr_t< Ptr, BlockType * > FromUsableSpace(Ptr ptr) const
Definition: block_allocator.h:487
virtual BlockResult< BlockType > ChooseBlock(Layout layout)=0
virtual void ReserveBlock(BlockType &)
Definition: block_allocator.h:234
void DoDeallocate(void *ptr) override
Definition: block_allocator.h:368
virtual void DeallocateBlock(BlockType *&&block)
Definition: block_allocator.h:381
bool DoResize(void *ptr, size_t new_size) override
Definition: block_allocator.h:413
void Init(ByteSpan region)
Definition: block_allocator.h:304
void DoDeallocate(void *ptr, Layout) override
Definition: block_allocator.h:204
void Init(BlockType *begin)
Definition: block_allocator.h:310
virtual void Flush()
Definition: block_allocator.h:256
size_t GetMaxAllocatable()
Definition: block_allocator.h:152
virtual void RecycleBlock(BlockType &)
Definition: block_allocator.h:242
Range blocks() const
Returns a Range of blocks tracking the memory of this allocator.
Definition: block_allocator.h:298
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: block_allocator.h:437
Definition: capability.h:65
Definition: detailed_block.h:88
Definition: block_allocator.h:45
static void CrashOnOutOfRange(const void *freed)
static void CrashOnDoubleFree(const void *freed)
Crashes with an informational message that a given block was freed twice.
static void CrashOnAllocated(const void *allocated)
Definition: block_allocator.h:90
#define PW_ALLOCATOR_BLOCK_POISON_INTERVAL
Definition: config.h:30
Definition: fragmentation.h:46