19#include "pw_allocator/allocator.h"
20#include "pw_allocator/block/basic.h"
21#include "pw_allocator/bucket/unordered.h"
22#include "pw_bytes/span.h"
23#include "pw_containers/vector.h"
24#include "pw_status/try.h"
26namespace pw::allocator {
53 static constexpr size_t DefaultAlignment() {
return 1; }
54 static constexpr size_t BlockOverhead() {
return sizeof(
BuddyBlock); }
55 static constexpr size_t MinInnerSize() {
return sizeof(
UnorderedItem); }
56 constexpr size_t OuterSizeUnchecked()
const {
return 1U << outer_size_log2_; }
58 uint8_t outer_size_log2_;
73 kImplementsGetUsableLayout | kImplementsGetAllocatedLayout |
74 kImplementsGetCapacity | kImplementsRecognizes;
76 static constexpr size_t kDefaultMinOuterSize = 16;
77 static constexpr size_t kDefaultNumBuckets = 16;
86 void Init(ByteSpan region);
105 span<BucketType> buckets_;
106 size_t min_outer_size_ = 0;
133template <
size_t kMinOuterSize_ =
134 internal::GenericBuddyAllocator::kDefaultMinOuterSize,
136 internal::GenericBuddyAllocator::kDefaultNumBuckets>
141 static constexpr size_t kMinOuterSize = kMinOuterSize_;
143 static_assert((kMinOuterSize & (kMinOuterSize - 1)) == 0,
144 "kMinOuterSize must be a power of 2");
169 return impl_.
Allocate(layout.Extend(1));
176 Result<Layout>
DoGetInfo(InfoType info_type,
const void* ptr)
const override {
178 case InfoType::kUsableLayoutOf: {
180 PW_TRY_ASSIGN(layout, impl_.
GetLayout(ptr));
181 return Layout(layout.size() - 1, layout.alignment());
183 case InfoType::kAllocatedLayoutOf:
185 case InfoType::kCapacity:
187 case InfoType::kRecognizes: {
189 PW_TRY_ASSIGN(layout, impl_.
GetLayout(ptr));
192 case InfoType::kRequestedLayoutOf:
194 return Status::Unimplemented();
198 std::array<BucketType, kNumBuckets> buckets_;
Definition: allocator.h:34
Definition: status_with_size.h:49
Definition: buddy_allocator.h:137
void Init(ByteSpan region)
Definition: buddy_allocator.h:161
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: buddy_allocator.h:176
void * DoAllocate(Layout layout) override
Definition: buddy_allocator.h:167
BuddyAllocator()
Constructs an allocator. Callers must call Init.
Definition: buddy_allocator.h:147
void DoDeallocate(void *ptr) override
Definition: buddy_allocator.h:173
BuddyAllocator(ByteSpan region)
Definition: buddy_allocator.h:154
Definition: capability.h:62
Definition: unordered.h:42
Definition: unordered.h:30
Definition: buddy_allocator.h:35
static BuddyBlock * Merge(BuddyBlock *&&left, BuddyBlock *&&right)
Merges two blocks together.
StatusWithSize CanAlloc(Layout layout) const
BuddyBlock * Split()
Splits a block in half and returns the new block.
Definition: buddy_allocator.h:68
void * Allocate(Layout layout)
size_t GetCapacity() const
Returns the total capacity of this allocator.
Definition: buddy_allocator.h:95
GenericBuddyAllocator(span< BucketType > buckets, size_t min_outer_size)
Result< Layout > GetLayout(const void *ptr) const
Returns the allocated layout for a given pointer.
void Deallocate(void *ptr)
void Init(ByteSpan region)
Sets the memory used to allocate blocks.