C/C++ API Reference
Loading...
Searching...
No Matches
first_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 <limits>
17
18#include "pw_allocator/block/detailed_block.h"
19#include "pw_allocator/block_allocator.h"
20#include "pw_allocator/bucket/sequenced.h"
21
22namespace pw::allocator {
23
25
27template <typename OffsetType>
29
40template <typename BlockType = FirstFitBlock<uintptr_t>>
41class FirstFitAllocator : public BlockAllocator<BlockType> {
42 public:
44
46 constexpr FirstFitAllocator() = default;
47
54
61 FirstFitAllocator(ByteSpan region, size_t threshold);
62
64 void set_threshold(size_t threshold) { bucket_.set_threshold(threshold); }
65
66 private:
68 size_t DoGetMaxAllocatable() override;
69
72
74 void ReserveBlock(BlockType& block) override {
75 std::ignore = bucket_.Remove(block);
76 }
77
79 void RecycleBlock(BlockType& block) override {
80 std::ignore = bucket_.Add(block);
81 }
82
84};
85
87
88// Template method implementations.
89
90template <typename BlockType>
92 size_t threshold) {
93 Base::Init(region);
94 bucket_.set_threshold(threshold);
95}
96
97template <typename BlockType>
99 const BlockType* largest = bucket_.FindLargest();
100 return largest == nullptr ? 0 : largest->InnerSize();
101}
102
103template <typename BlockType>
105 Layout layout) {
106 BlockType* block = bucket_.RemoveCompatible(layout);
107 if (block == nullptr) {
108 return BlockResult<BlockType>(nullptr, Status::NotFound());
109 }
110 if (layout.size() < bucket_.threshold()) {
111 return BlockType::AllocLast(std::move(block), layout);
112 } else {
113 return BlockType::AllocFirst(std::move(block), layout);
114 }
115}
116
117} // namespace pw::allocator
static constexpr Status NotFound()
Definition: status.h:190
Definition: block_allocator.h:98
void Init(ByteSpan region)
Definition: block_allocator.h:303
Definition: result.h:106
Definition: detailed_block.h:88
Definition: first_fit.h:41
FirstFitAllocator(ByteSpan region)
Definition: first_fit.h:53
void ReserveBlock(BlockType &block) override
Definition: first_fit.h:74
constexpr FirstFitAllocator()=default
Constexpr constructor. Callers must explicitly call Init.
void set_threshold(size_t threshold)
Sets the threshold value for which requests are considered "large".
Definition: first_fit.h:64
size_t DoGetMaxAllocatable() override
Definition: first_fit.h:98
BlockResult< BlockType > ChooseBlock(Layout layout) override
Definition: first_fit.h:104
void RecycleBlock(BlockType &block) override
Definition: first_fit.h:79
FirstFitAllocator(ByteSpan region, size_t threshold)
Definition: first_fit.h:91
Definition: layout.h:64
Definition: sequenced.h:46