C/C++ API Reference
Loading...
Searching...
No Matches
best_fit.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 <cstddef>
17#include <limits>
18
19#include "pw_allocator/block/detailed_block.h"
20#include "pw_allocator/block_allocator.h"
21#include "pw_allocator/bucket/fast_sorted.h"
22#include "pw_allocator/bucket/sorted.h"
23#include "pw_allocator/config.h"
24
25namespace pw::allocator {
26
28
30template <typename OffsetType>
32
42template <typename BlockType = BestFitBlock<uintptr_t>>
43class BestFitAllocator : public BlockAllocator<BlockType> {
44 private:
47
48 public:
50
52 constexpr BestFitAllocator() = default;
53
60
61 private:
63 size_t DoGetMaxAllocatable() override;
64
67
69 void ReserveBlock(BlockType& block) override;
70
72 void RecycleBlock(BlockType& block) override;
73
74 SmallBucket small_bucket_;
75 LargeBucket large_bucket_;
76};
77
78// Template method implementations.
79
80template <typename BlockType>
82 const BlockType* largest = large_bucket_.empty()
83 ? small_bucket_.FindLargest()
84 : large_bucket_.FindLargest();
85 return largest == nullptr ? 0 : largest->InnerSize();
86}
87
88template <typename BlockType>
90 // The small bucket is slower; skip it if we can.
91 BlockType* block = nullptr;
92 if (layout.size() < sizeof(typename LargeBucket::ItemType)) {
93 block = small_bucket_.RemoveCompatible(layout);
94 if (block != nullptr) {
95 return BlockType::AllocFirst(std::move(block), layout);
96 }
97 }
98 block = large_bucket_.RemoveCompatible(layout);
99 if (block != nullptr) {
100 return BlockType::AllocFirst(std::move(block), layout);
101 }
102 return BlockResult<BlockType>(nullptr, Status::NotFound());
103}
104
105template <typename BlockType>
107 // The small bucket is slower; skip it if we can.
108 if (!large_bucket_.Remove(block)) {
109 std::ignore = small_bucket_.Remove(block);
110 }
111}
112
113template <typename BlockType>
115 if (block.InnerSize() < sizeof(typename LargeBucket::ItemType)) {
116 std::ignore = small_bucket_.Add(block);
117 } else {
118 std::ignore = large_bucket_.Add(block);
119 }
120}
121
123
124} // namespace pw::allocator
static constexpr Status NotFound()
Definition: status.h:190
Definition: best_fit.h:43
constexpr BestFitAllocator()=default
Constexpr constructor. Callers must explicitly call Init.
BestFitAllocator(ByteSpan region)
Definition: best_fit.h:59
Definition: block_allocator.h:98
void Init(ByteSpan region)
Definition: block_allocator.h:303
Definition: result.h:106
Definition: detailed_block.h:88
Definition: fast_sorted.h:65
Definition: fast_sorted.h:35
Definition: sorted.h:97
Definition: layout.h:64
void RecycleBlock(BlockType &block) override
Definition: best_fit.h:114
size_t DoGetMaxAllocatable() override
Definition: best_fit.h:81
BlockResult< BlockType > ChooseBlock(Layout layout) override
Definition: best_fit.h:89
void ReserveBlock(BlockType &block) override
Definition: best_fit.h:106