C/C++ API Reference
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/abstract_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:
70 static constexpr Capabilities kCapabilities =
71 kImplementsGetUsableLayout | kImplementsGetAllocatedLayout |
72 kImplementsGetCapacity | kImplementsRecognizes;
73
74 static constexpr size_t kDefaultMinOuterSize = 16;
75 static constexpr size_t kDefaultNumBuckets = 16;
76
82 size_t min_outer_size);
83
85 void Init(ByteSpan region);
86
87 protected:
88 constexpr GenericBuddyAllocator() : AbstractAllocator(kCapabilities) {}
89
91 void* DoAllocate(Layout layout) override;
92
94 void DoDeallocate(void* ptr) override;
95
97 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override;
98
102
103 private:
105 size_t min_outer_size_ = 0;
106 ByteSpan region_;
107};
108
109} // namespace internal
110
112
134template <size_t kMinOuterSize_ =
135 internal::GenericBuddyAllocator::kDefaultMinOuterSize,
136 size_t kNumBuckets =
137 internal::GenericBuddyAllocator::kDefaultNumBuckets>
139 private:
141
142 public:
143 static constexpr size_t kMinOuterSize = kMinOuterSize_;
144
145 static_assert((kMinOuterSize & (kMinOuterSize - 1)) == 0,
146 "kMinOuterSize must be a power of 2");
147
149 constexpr BuddyAllocator() { Base::InitBuckets(buckets_, kMinOuterSize); }
150
157
159
160 private:
161 std::array<UnorderedBucket<internal::BuddyBlock>, kNumBuckets> buckets_;
162};
163
165
166} // namespace pw::allocator
Definition: result.h:145
Definition: status_with_size.h:51
Definition: abstract_allocator.h:30
Definition: basic.h:95
Definition: buddy_allocator.h:138
constexpr BuddyAllocator()
Constructs an allocator. Callers must call Init.
Definition: buddy_allocator.h:149
BuddyAllocator(ByteSpan region)
Definition: buddy_allocator.h:156
Definition: capability.h:65
Definition: layout.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 InitBuckets(span< UnorderedBucket< BuddyBlock > > buckets, size_t min_outer_size)
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
void * DoAllocate(Layout layout) override
void Init(ByteSpan region)
Sets the memory used to allocate blocks.
Definition: span_impl.h:235