C/C++ API Reference
Loading...
Searching...
No Matches
bucket_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 <cstdint>
18#include <utility>
19
20#include "pw_allocator/block/detailed_block.h"
21#include "pw_allocator/block_allocator.h"
22#include "pw_allocator/bucket/unordered.h"
23#include "pw_status/try.h"
24
25namespace pw::allocator {
26
28
31template <typename OffsetType = uintptr_t>
33
57template <typename BlockType = BucketBlock<>>
58class BasicBucketAllocator : public BlockAllocator<BlockType> {
59 private:
61
62 public:
63 constexpr BasicBucketAllocator() = default;
64
67 size_t max_inner_size);
68
69 private:
71 size_t DoGetMaxAllocatable() override;
72
75
77 void ReserveBlock(BlockType& block) override;
78
80 void RecycleBlock(BlockType& block) override;
81
83};
84
89template <typename BlockType = BucketBlock<>,
90 size_t kMinInnerSize = 32,
91 size_t kNumBuckets = 5>
92class BucketAllocator : public BasicBucketAllocator<BlockType> {
93 private:
95
96 public:
98 constexpr BucketAllocator() { Base::InitBuckets(buckets_, kMinInnerSize); }
99
107 Base::Init(region);
108 }
109
110 ~BucketAllocator() override;
111
112 private:
113 std::array<UnorderedBucket<BlockType>, kNumBuckets> buckets_;
114};
115
117
118// Template method implementations.
119
120template <typename BlockType>
122 span<UnorderedBucket<BlockType>> buckets, size_t max_inner_size) {
123 buckets_ = buckets;
124 for (auto& bucket : buckets_.subspan(0, buckets_.size() - 1)) {
125 bucket.set_max_inner_size(max_inner_size);
126 max_inner_size <<= 1;
127 }
128}
129
130template <typename BlockType>
132 for (auto b = buckets_.rbegin(); b != buckets_.rend(); ++b) {
133 const BlockType* largest = b->FindLargest();
134 if (largest != nullptr) {
135 return largest->InnerSize();
136 }
137 }
138 return 0;
139}
140
141template <typename BlockType>
143 Layout layout) {
144 for (auto& bucket : buckets_) {
145 if (bucket.max_inner_size() < layout.size()) {
146 continue;
147 }
148 BlockType* block = bucket.RemoveCompatible(layout);
149 if (block != nullptr) {
150 return BlockType::AllocFirst(std::move(block), layout);
151 }
152 }
153 return BlockResult<BlockType>(nullptr, Status::NotFound());
154}
155
156template <typename BlockType>
158 for (auto& bucket : buckets_) {
159 if (block.InnerSize() <= bucket.max_inner_size()) {
160 std::ignore = bucket.Remove(block);
161 break;
162 }
163 }
164}
165
166template <typename BlockType>
168 for (auto& bucket : buckets_) {
169 if (block.InnerSize() <= bucket.max_inner_size()) {
170 std::ignore = bucket.Add(block);
171 break;
172 }
173 }
174}
175
176template <typename BlockType, size_t kMinInnerSize, size_t kNumBuckets>
178 for (auto& bucket : buckets_) {
179 bucket.Clear();
180 }
181}
182
183} // namespace pw::allocator
static constexpr Status NotFound()
Definition: status.h:190
Definition: bucket_allocator.h:58
void ReserveBlock(BlockType &block) override
Definition: bucket_allocator.h:157
constexpr void InitBuckets(span< UnorderedBucket< BlockType > > buckets, size_t max_inner_size)
Configures bucket sizes.
Definition: bucket_allocator.h:121
void RecycleBlock(BlockType &block) override
Definition: bucket_allocator.h:167
size_t DoGetMaxAllocatable() override
Definition: bucket_allocator.h:131
BlockResult< BlockType > ChooseBlock(Layout layout) override
Definition: bucket_allocator.h:142
Definition: block_allocator.h:98
void Init(ByteSpan region)
Definition: block_allocator.h:303
Definition: result.h:106
Definition: bucket_allocator.h:92
constexpr BucketAllocator()
Constexpr constructor. Callers must explicitly call Init.
Definition: bucket_allocator.h:98
BucketAllocator(ByteSpan region)
Definition: bucket_allocator.h:106
Definition: detailed_block.h:88
Definition: layout.h:64
Definition: unordered.h:44
Definition: span_impl.h:235