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_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"
25
26namespace pw::allocator {
27namespace internal {
28
37 public:
38 static constexpr Capabilities kCapabilities =
39 kImplementsGetUsableLayout | kImplementsGetAllocatedLayout |
40 kImplementsGetCapacity | kImplementsRecognizes;
41
46 GenericBuddyAllocator(span<Bucket> buckets, size_t min_chunk_size);
47
49 void Init(ByteSpan region);
50
52 void* Allocate(Layout layout);
53
55 void Deallocate(void* ptr);
56
58 size_t GetCapacity() const { return region_.size(); }
59
61 Result<Layout> GetLayout(const void* ptr) const;
62
66
67 private:
68 span<Bucket> buckets_;
69 ByteSpan region_;
70};
71
72} // namespace internal
73
95template <size_t kMinChunkSize, size_t kNumBuckets>
96class BuddyAllocator : public Allocator {
97 public:
98 static_assert((kMinChunkSize & (kMinChunkSize - 1)) == 0,
99 "kMinChunkSize must be a power of 2");
100
102 BuddyAllocator() : impl_(buckets_, kMinChunkSize) {}
103
110 BuddyAllocator(ByteSpan region) : BuddyAllocator() { Init(region); }
111
118 void Init(ByteSpan region) { impl_.Init(region); }
119
120 ~BuddyAllocator() override { impl_.CrashIfAllocated(); }
121
122 private:
124 void* DoAllocate(Layout layout) override {
125 // Reserve one byte to save the bucket index.
126 return impl_.Allocate(layout.Extend(1));
127 }
128
130 void DoDeallocate(void* ptr) override { impl_.Deallocate(ptr); }
131
133 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override {
134 switch (info_type) {
135 case InfoType::kUsableLayoutOf: {
136 Layout layout;
137 PW_TRY_ASSIGN(layout, impl_.GetLayout(ptr));
138 return Layout(layout.size() - 1, layout.alignment());
139 }
140 case InfoType::kAllocatedLayoutOf:
141 return impl_.GetLayout(ptr);
142 case InfoType::kCapacity:
143 return Layout(impl_.GetCapacity(), kMinChunkSize);
144 case InfoType::kRecognizes: {
145 Layout layout;
146 PW_TRY_ASSIGN(layout, impl_.GetLayout(ptr));
147 return Layout();
148 }
149 case InfoType::kRequestedLayoutOf:
150 default:
151 return Status::Unimplemented();
152 }
153 }
154
155 std::array<internal::Bucket, kNumBuckets> buckets_;
156 internal::GenericBuddyAllocator impl_;
157};
158
159} // namespace pw::allocator
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: layout.h:39
Definition: buddy_allocator.h:36
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 Init(ByteSpan region)
Sets the memory used to allocate chunks.