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;
106 size_t min_outer_size_ = 0;
135template <
size_t kMinOuterSize_ =
136 internal::GenericBuddyAllocator::kDefaultMinOuterSize,
138 internal::GenericBuddyAllocator::kDefaultNumBuckets>
143 static constexpr size_t kMinOuterSize = kMinOuterSize_;
145 static_assert((kMinOuterSize & (kMinOuterSize - 1)) == 0,
146 "kMinOuterSize must be a power of 2");
171 return impl_.
Allocate(layout.Extend(1));
180 case InfoType::kUsableLayoutOf: {
182 PW_TRY_ASSIGN(layout, impl_.
GetLayout(ptr));
183 return Layout(layout.size() - 1, layout.alignment());
185 case InfoType::kAllocatedLayoutOf:
187 case InfoType::kCapacity:
189 case InfoType::kRecognizes: {
191 PW_TRY_ASSIGN(layout, impl_.
GetLayout(ptr));
194 case InfoType::kRequestedLayoutOf:
196 return Status::Unimplemented();
200 std::array<BucketType, kNumBuckets> buckets_;
Definition: allocator.h:36
Definition: status_with_size.h:49
Definition: buddy_allocator.h:139
void Init(ByteSpan region)
Definition: buddy_allocator.h:163
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: buddy_allocator.h:178
void * DoAllocate(Layout layout) override
Definition: buddy_allocator.h:169
BuddyAllocator()
Constructs an allocator. Callers must call Init.
Definition: buddy_allocator.h:149
void DoDeallocate(void *ptr) override
Definition: buddy_allocator.h:175
BuddyAllocator(ByteSpan region)
Definition: buddy_allocator.h:156
Definition: capability.h:64
Definition: unordered.h:44
Definition: unordered.h:32
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.
Definition: span_impl.h:235