19#include "pw_allocator/allocator.h"
20#include "pw_allocator/block/basic.h"
21#include "pw_allocator/block/iterable.h"
22#include "pw_allocator/block/poisonable.h"
23#include "pw_allocator/block/result.h"
24#include "pw_allocator/block/with_layout.h"
25#include "pw_allocator/capability.h"
26#include "pw_allocator/config.h"
27#include "pw_allocator/fragmentation.h"
28#include "pw_allocator/hardening.h"
29#include "pw_assert/assert.h"
30#include "pw_bytes/span.h"
31#include "pw_result/result.h"
32#include "pw_status/status.h"
34namespace pw::allocator {
55 template <
typename BlockType>
58 kImplementsGetAllocatedLayout |
59 kImplementsGetCapacity | kImplementsRecognizes;
60 if constexpr (has_layout_v<BlockType>) {
61 return common | kImplementsGetRequestedLayout;
90template <
typename,
size_t>
106template <
typename BlockType_>
112 using BlockType = BlockType_;
113 using Range =
typename BlockType::Range;
116 Base::GetCapabilities<BlockType>();
186 template <
typename Ptr>
196 using BlockResultPrev = internal::GenericBlockResult::Prev;
197 using BlockResultNext = internal::GenericBlockResult::Next;
200 template <
typename,
size_t>
210 bool DoResize(
void* ptr,
size_t new_size)
override;
262 static bool PrevIsFree(
const BlockType* block) {
263 auto* prev = block->Prev();
264 return prev !=
nullptr && prev->IsFree();
268 static bool NextIsFree(
const BlockType* block) {
269 auto* next = block->Next();
270 return next !=
nullptr && next->IsFree();
275 void UpdateLast(BlockType* block);
278 size_t capacity_ = 0;
279 size_t allocated_ = 0;
280 BlockType* first_ =
nullptr;
281 BlockType* last_ =
nullptr;
282 uint16_t unpoisoned_ = 0;
289template <
typename BlockType>
290BlockAllocator<BlockType>::~BlockAllocator() {
291 if constexpr (Hardening::kIncludesRobustChecks) {
292 for (
auto* block : blocks()) {
293 if (!block->IsFree()) {
294 CrashOnAllocated(block);
300template <
typename BlockType>
303 return Range(first_);
306template <
typename BlockType>
309 PW_ASSERT(result.
ok());
313template <
typename BlockType>
315 if constexpr (Hardening::kIncludesRobustChecks) {
316 PW_ASSERT(begin !=
nullptr);
317 PW_ASSERT(begin->Prev() ==
nullptr);
320 for (
auto* block : blocks()) {
322 capacity_ += block->OuterSize();
323 if (block->IsFree()) {
324 RecycleBlock(*block);
329template <
typename BlockType>
331 if (capacity_ == 0) {
336 if constexpr (Hardening::kIncludesDebugChecks) {
337 PW_ASSERT(last_->Next() ==
nullptr);
339 auto result = ChooseBlock(layout);
344 BlockType* block = result.block();
345 allocated_ += block->OuterSize();
346 switch (result.prev()) {
347 case BlockResultPrev::kSplitNew:
349 RecycleBlock(*(block->Prev()));
351 case BlockResultPrev::kResizedLarger:
353 allocated_ += result.size();
355 case BlockResultPrev::kUnchanged:
356 case BlockResultPrev::kResizedSmaller:
359 if (result.next() == BlockResultNext::kSplitNew) {
360 RecycleBlock(*(block->Next()));
364 if constexpr (Hardening::kIncludesDebugChecks) {
365 PW_ASSERT(block <= last_);
368 return block->UsableSpace();
371template <
typename BlockType>
373 BlockType* block = FromUsableSpace(ptr);
374 if (block->IsFree()) {
375 if constexpr (Hardening::kIncludesBasicChecks) {
376 CrashOnDoubleFree(block);
381 DeallocateBlock(std::move(block));
384template <
typename BlockType>
387 if (
auto* prev = block->Prev(); prev !=
nullptr && prev->IsFree()) {
390 if (
auto* next = block->Next(); next !=
nullptr && next->IsFree()) {
395 allocated_ -= block->OuterSize();
396 auto free_result = BlockType::Free(std::move(block));
397 block = free_result.block();
400 if (free_result.prev() == BlockResultPrev::kResizedSmaller) {
402 allocated_ -= free_result.size();
405 if constexpr (is_poisonable_v<BlockType> && kPoisonInterval != 0) {
407 if (unpoisoned_ >= kPoisonInterval) {
413 RecycleBlock(*block);
416template <
typename BlockType>
418 BlockType* block = FromUsableSpace(ptr);
421 if (
auto* next = block->Next(); next !=
nullptr && next->IsFree()) {
425 size_t old_size = block->OuterSize();
426 if (!block->Resize(new_size).ok()) {
429 allocated_ -= old_size;
430 allocated_ += block->OuterSize();
433 if (
auto* next = block->Next(); next !=
nullptr && next->IsFree()) {
440template <
typename BlockType>
442 const void* ptr)
const {
444 if (info_type == InfoType::kCapacity) {
448 if (ptr < first_->UsableSpace() || last_->UsableSpace() < ptr) {
451 const auto* block = BlockType::FromUsableSpace(ptr);
452 if (!block->IsValid()) {
455 if (block->IsFree()) {
458 if constexpr (kCapabilities.has(kImplementsGetRequestedLayout)) {
459 if (info_type == InfoType::kRequestedLayoutOf) {
460 return block->RequestedLayout();
464 case InfoType::kUsableLayoutOf:
465 return Layout(block->InnerSize(), BlockType::kAlignment);
466 case InfoType::kAllocatedLayoutOf:
467 return Layout(block->OuterSize(), BlockType::kAlignment);
468 case InfoType::kRecognizes:
470 case InfoType::kCapacity:
471 case InfoType::kRequestedLayoutOf:
477template <
typename BlockType>
480 for (
auto block : blocks()) {
481 if (block->IsFree()) {
482 fragmentation.AddFragment(block->InnerSize() / BlockType::kAlignment);
485 return fragmentation;
488template <
typename BlockType>
489template <
typename Ptr>
490internal::copy_const_ptr_t<Ptr, BlockType*>
492 if (ptr < first_->UsableSpace() || last_->UsableSpace() < ptr) {
493 if constexpr (Hardening::kIncludesBasicChecks) {
494 CrashOnOutOfRange(ptr);
498 auto* block = BlockType::FromUsableSpace(ptr);
499 if (!block->IsValid()) {
500 if constexpr (Hardening::kIncludesBasicChecks) {
501 block->CheckInvariants();
508template <
typename BlockType>
510 BlockType* next = block->Next();
511 if (next ==
nullptr) {
513 }
else if (next->Next() ==
nullptr) {
Definition: allocator.h:42
constexpr bool ok() const
Definition: result.h:451
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:107
void * DoAllocate(Layout layout) override
Definition: block_allocator.h:330
size_t DoGetAllocated() const override
Definition: block_allocator.h:213
std::optional< Fragmentation > DoMeasureFragmentation() const override
Definition: block_allocator.h:162
Fragmentation MeasureFragmentation() const
Returns fragmentation information for the block allocator's memory region.
Definition: block_allocator.h:478
virtual size_t DoGetMaxAllocatable()=0
internal::copy_const_ptr_t< Ptr, BlockType * > FromUsableSpace(Ptr ptr) const
Definition: block_allocator.h:491
virtual BlockResult< BlockType > ChooseBlock(Layout layout)=0
virtual void ReserveBlock(BlockType &)
Definition: block_allocator.h:237
void DoDeallocate(void *ptr) override
Definition: block_allocator.h:372
virtual void DeallocateBlock(BlockType *&&block)
Definition: block_allocator.h:385
bool DoResize(void *ptr, size_t new_size) override
Definition: block_allocator.h:417
void Init(ByteSpan region)
Definition: block_allocator.h:307
void Init(BlockType *begin)
Definition: block_allocator.h:314
virtual void Flush()
Definition: block_allocator.h:259
size_t GetMaxAllocatable()
Definition: block_allocator.h:153
virtual void RecycleBlock(BlockType &)
Definition: block_allocator.h:245
Range blocks() const
Returns a Range of blocks tracking the memory of this allocator.
Definition: block_allocator.h:301
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: block_allocator.h:441
Definition: capability.h:65
Definition: detailed_block.h:88
Definition: block_allocator.h:46
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:91
#define PW_ALLOCATOR_BLOCK_POISON_INTERVAL
Definition: config.h:30
Definition: fragmentation.h:46