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/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:
91
92 private:
94 void* DoAllocate(Layout layout) override;
95
97 void DoDeallocate(void* ptr) override;
98
100 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override;
101
103 size_t min_outer_size_ = 0;
104 ByteSpan region_;
105};
106
107} // namespace internal
108
110
132template <size_t kMinOuterSize_ =
133 internal::GenericBuddyAllocator::kDefaultMinOuterSize,
134 size_t kNumBuckets =
135 internal::GenericBuddyAllocator::kDefaultNumBuckets>
137 private:
139
140 public:
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 constexpr BuddyAllocator() { Base::InitBuckets(buckets_, kMinOuterSize); }
148
155
157
158 private:
159 std::array<UnorderedBucket<internal::BuddyBlock>, kNumBuckets> buckets_;
160};
161
163
164} // namespace pw::allocator
Definition: allocator.h:42
Definition: result.h:145
Definition: status_with_size.h:51
Definition: basic.h:95
Definition: buddy_allocator.h:136
constexpr BuddyAllocator()
Constructs an allocator. Callers must call Init.
Definition: buddy_allocator.h:147
BuddyAllocator(ByteSpan region)
Definition: buddy_allocator.h:154
Definition: capability.h:65
Definition: layout.h:58
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