aboutsummaryrefslogtreecommitdiff
path: root/pw_allocator/freelist_heap.cc
diff options
context:
space:
mode:
Diffstat (limited to 'pw_allocator/freelist_heap.cc')
-rw-r--r--pw_allocator/freelist_heap.cc47
1 files changed, 23 insertions, 24 deletions
diff --git a/pw_allocator/freelist_heap.cc b/pw_allocator/freelist_heap.cc
index fcb8f3c50..c57eeba6a 100644
--- a/pw_allocator/freelist_heap.cc
+++ b/pw_allocator/freelist_heap.cc
@@ -23,13 +23,15 @@ namespace pw::allocator {
FreeListHeap::FreeListHeap(span<std::byte> region, FreeList& freelist)
: freelist_(freelist), heap_stats_() {
- Block* block;
+ auto result = BlockType::Init(region);
PW_CHECK_OK(
- Block::Init(region, &block),
+ result.status(),
"Failed to initialize FreeListHeap region; misaligned or too small");
+ BlockType* block = *result;
+ block->CrashIfInvalid();
freelist_.AddChunk(BlockToSpan(block))
- .IgnoreError(); // TODO(b/242598609): Handle Status properly
+ .IgnoreError(); // TODO: b/242598609 - Handle Status properly
region_ = region;
heap_stats_.total_bytes = region.size();
@@ -44,18 +46,17 @@ void* FreeListHeap::Allocate(size_t size) {
return nullptr;
}
freelist_.RemoveChunk(chunk)
- .IgnoreError(); // TODO(b/242598609): Handle Status properly
+ .IgnoreError(); // TODO: b/242598609 - Handle Status properly
- Block* chunk_block = Block::FromUsableSpace(chunk.data());
+ BlockType* chunk_block = BlockType::FromUsableSpace(chunk.data());
chunk_block->CrashIfInvalid();
// Split that chunk. If there's a leftover chunk, add it to the freelist
- Block* leftover;
- auto status = chunk_block->Split(size, &leftover);
- if (status == PW_STATUS_OK) {
- freelist_.AddChunk(BlockToSpan(leftover))
- .IgnoreError(); // TODO(b/242598609): Handle Status properly
+ Result<BlockType*> result = BlockType::Split(chunk_block, size);
+ if (result.ok()) {
+ freelist_.AddChunk(BlockToSpan(*result))
+ .IgnoreError(); // TODO: b/242598609 - Handle Status properly
}
chunk_block->MarkUsed();
@@ -75,7 +76,7 @@ void FreeListHeap::Free(void* ptr) {
return;
}
- Block* chunk_block = Block::FromUsableSpace(bytes);
+ BlockType* chunk_block = BlockType::FromUsableSpace(bytes);
chunk_block->CrashIfInvalid();
size_t size_freed = chunk_block->InnerSize();
@@ -86,8 +87,8 @@ void FreeListHeap::Free(void* ptr) {
}
chunk_block->MarkFree();
// Can we combine with the left or right blocks?
- Block* prev = chunk_block->Prev();
- Block* next = nullptr;
+ BlockType* prev = chunk_block->Prev();
+ BlockType* next = nullptr;
if (!chunk_block->Last()) {
next = chunk_block->Next();
@@ -96,23 +97,21 @@ void FreeListHeap::Free(void* ptr) {
if (prev != nullptr && !prev->Used()) {
// Remove from freelist and merge
freelist_.RemoveChunk(BlockToSpan(prev))
- .IgnoreError(); // TODO(b/242598609): Handle Status properly
- chunk_block->MergePrev()
- .IgnoreError(); // TODO(b/242598609): Handle Status properly
-
- // chunk_block is now invalid; prev now encompasses it.
- chunk_block = prev;
+ .IgnoreError(); // TODO: b/242598609 - Handle Status properly
+ chunk_block = chunk_block->Prev();
+ BlockType::MergeNext(chunk_block)
+ .IgnoreError(); // TODO: b/242598609 - Handle Status properly
}
if (next != nullptr && !next->Used()) {
freelist_.RemoveChunk(BlockToSpan(next))
- .IgnoreError(); // TODO(b/242598609): Handle Status properly
- chunk_block->MergeNext()
- .IgnoreError(); // TODO(b/242598609): Handle Status properly
+ .IgnoreError(); // TODO: b/242598609 - Handle Status properly
+ BlockType::MergeNext(chunk_block)
+ .IgnoreError(); // TODO: b/242598609 - Handle Status properly
}
// Add back to the freelist
freelist_.AddChunk(BlockToSpan(chunk_block))
- .IgnoreError(); // TODO(b/242598609): Handle Status properly
+ .IgnoreError(); // TODO: b/242598609 - Handle Status properly
heap_stats_.bytes_allocated -= size_freed;
heap_stats_.cumulative_freed += size_freed;
@@ -139,7 +138,7 @@ void* FreeListHeap::Realloc(void* ptr, size_t size) {
return nullptr;
}
- Block* chunk_block = Block::FromUsableSpace(bytes);
+ BlockType* chunk_block = BlockType::FromUsableSpace(bytes);
if (!chunk_block->Used()) {
return nullptr;
}