Pigweed
 
Loading...
Searching...
No Matches
buddy_allocator.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <array>
17#include <cstddef>
18
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"
25
26namespace pw::allocator {
27namespace internal {
28
35class BuddyBlock : public BasicBlock<BuddyBlock> {
36 public:
37 BuddyBlock(size_t outer_size);
38
42
45
47 static BuddyBlock* Merge(BuddyBlock*&& left, BuddyBlock*&& right);
48
49 private:
50 // `Basic` required methods.
52 friend Basic;
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_; }
57
58 uint8_t outer_size_log2_;
59};
60
69 public:
71
72 static constexpr Capabilities kCapabilities =
73 kImplementsGetUsableLayout | kImplementsGetAllocatedLayout |
74 kImplementsGetCapacity | kImplementsRecognizes;
75
76 static constexpr size_t kDefaultMinOuterSize = 16;
77 static constexpr size_t kDefaultNumBuckets = 16;
78
83 GenericBuddyAllocator(span<BucketType> buckets, size_t min_outer_size);
84
86 void Init(ByteSpan region);
87
89 void* Allocate(Layout layout);
90
92 void Deallocate(void* ptr);
93
95 size_t GetCapacity() const { return region_.size(); }
96
98 Result<Layout> GetLayout(const void* ptr) const;
99
103
104 private:
105 span<BucketType> buckets_;
106 size_t min_outer_size_ = 0;
107 ByteSpan region_;
108};
109
110} // namespace internal
111
133template <size_t kMinOuterSize_ =
134 internal::GenericBuddyAllocator::kDefaultMinOuterSize,
135 size_t kNumBuckets =
136 internal::GenericBuddyAllocator::kDefaultNumBuckets>
137class BuddyAllocator : public Allocator {
138 public:
140
141 static constexpr size_t kMinOuterSize = kMinOuterSize_;
142
143 static_assert((kMinOuterSize & (kMinOuterSize - 1)) == 0,
144 "kMinOuterSize must be a power of 2");
145
147 BuddyAllocator() : impl_(buckets_, kMinOuterSize) {}
148
154 BuddyAllocator(ByteSpan region) : BuddyAllocator() { Init(region); }
155
161 void Init(ByteSpan region) { impl_.Init(region); }
162
163 ~BuddyAllocator() override { impl_.CrashIfAllocated(); }
164
165 private:
167 void* DoAllocate(Layout layout) override {
168 // Reserve one byte to save the bucket index.
169 return impl_.Allocate(layout.Extend(1));
170 }
171
173 void DoDeallocate(void* ptr) override { impl_.Deallocate(ptr); }
174
176 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override {
177 switch (info_type) {
178 case InfoType::kUsableLayoutOf: {
179 Layout layout;
180 PW_TRY_ASSIGN(layout, impl_.GetLayout(ptr));
181 return Layout(layout.size() - 1, layout.alignment());
182 }
183 case InfoType::kAllocatedLayoutOf:
184 return impl_.GetLayout(ptr);
185 case InfoType::kCapacity:
186 return Layout(impl_.GetCapacity(), kMinOuterSize);
187 case InfoType::kRecognizes: {
188 Layout layout;
189 PW_TRY_ASSIGN(layout, impl_.GetLayout(ptr));
190 return Layout();
191 }
192 case InfoType::kRequestedLayoutOf:
193 default:
194 return Status::Unimplemented();
195 }
196 }
197
198 std::array<BucketType, kNumBuckets> buckets_;
200};
201
202} // namespace pw::allocator
Definition: allocator.h:34
Definition: status_with_size.h:49
Definition: basic.h:93
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: layout.h:56
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
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 Init(ByteSpan region)
Sets the memory used to allocate blocks.