19#include "pw_alignment/alignment.h"
20#include "pw_allocator/allocator.h"
21#include "pw_allocator/bucket.h"
22#include "pw_bytes/span.h"
23#include "pw_containers/vector.h"
24#include "pw_status/try.h"
26namespace pw::allocator {
39 kImplementsGetUsableLayout | kImplementsGetAllocatedLayout |
40 kImplementsGetCapacity | kImplementsRecognizes;
49 void Init(ByteSpan region);
68 span<Bucket> buckets_;
95template <
size_t kMinChunkSize,
size_t kNumBuckets>
98 static_assert((kMinChunkSize & (kMinChunkSize - 1)) == 0,
99 "kMinChunkSize must be a power of 2");
124 void* DoAllocate(Layout layout)
override {
126 return impl_.
Allocate(layout.Extend(1));
130 void DoDeallocate(
void* ptr)
override { impl_.
Deallocate(ptr); }
133 Result<Layout> DoGetInfo(InfoType info_type,
const void* ptr)
const override {
135 case InfoType::kUsableLayoutOf: {
137 PW_TRY_ASSIGN(layout, impl_.
GetLayout(ptr));
138 return Layout(layout.size() - 1, layout.alignment());
140 case InfoType::kAllocatedLayoutOf:
142 case InfoType::kCapacity:
144 case InfoType::kRecognizes: {
146 PW_TRY_ASSIGN(layout, impl_.
GetLayout(ptr));
149 case InfoType::kRequestedLayoutOf:
151 return Status::Unimplemented();
155 std::array<internal::Bucket, kNumBuckets> buckets_;
156 internal::GenericBuddyAllocator impl_;
Definition: allocator.h:32
Definition: buddy_allocator.h:96
BuddyAllocator()
Constructs an allocator. Callers must call Init.
Definition: buddy_allocator.h:102
BuddyAllocator(ByteSpan region)
Definition: buddy_allocator.h:110
void Init(ByteSpan region)
Definition: buddy_allocator.h:118
Definition: capability.h:64
Definition: buddy_allocator.h:36
void * Allocate(Layout layout)
size_t GetCapacity() const
Returns the total capacity of this allocator.
Definition: buddy_allocator.h:58
Result< Layout > GetLayout(const void *ptr) const
Returns the allocated layout for a given pointer.
GenericBuddyAllocator(span< Bucket > buckets, size_t min_chunk_size)
void Deallocate(void *ptr)
void Init(ByteSpan region)
Sets the memory used to allocate chunks.