C/C++ API Reference
Loading...
Searching...
No Matches
worst_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 = WorstFitBlock<uintptr_t>>
43class WorstFitAllocator : public BlockAllocator<BlockType> {
44 private:
47
48 public:
50
52 constexpr WorstFitAllocator() = 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
79
80// Template method implementations.
81
82template <typename BlockType>
84 const BlockType* largest = large_bucket_.empty()
85 ? small_bucket_.FindLargest()
86 : large_bucket_.FindLargest();
87 return largest == nullptr ? 0 : largest->InnerSize();
88}
89
90template <typename BlockType>
92 Layout layout) {
93 BlockType* block = large_bucket_.RemoveCompatible(layout);
94 if (block != nullptr) {
95 return BlockType::AllocFirst(std::move(block), layout);
96 }
97 block = small_bucket_.RemoveCompatible(layout);
98 if (block != nullptr) {
99 return BlockType::AllocFirst(std::move(block), layout);
100 }
101 return BlockResult<BlockType>(nullptr, Status::NotFound());
102}
103
104template <typename BlockType>
106 // The small bucket is slower; skip it if we can.
107 if (!large_bucket_.Remove(block)) {
108 std::ignore = small_bucket_.Remove(block);
109 }
110}
111
112template <typename BlockType>
114 if (block.InnerSize() < sizeof(typename LargeBucket::ItemType)) {
115 std::ignore = small_bucket_.Add(block);
116 } else {
117 std::ignore = large_bucket_.Add(block);
118 }
119}
120
121} // 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: fast_sorted.h:35
Definition: layout.h:64
Definition: fast_sorted.h:114
Definition: sorted.h:125
Definition: worst_fit.h:43
constexpr WorstFitAllocator()=default
Constexpr constructor. Callers must explicitly call Init.
WorstFitAllocator(ByteSpan region)
Definition: worst_fit.h:59
size_t DoGetMaxAllocatable() override
Definition: worst_fit.h:83
BlockResult< BlockType > ChooseBlock(Layout layout) override
Definition: worst_fit.h:91
void ReserveBlock(BlockType &block) override
Definition: worst_fit.h:105
void RecycleBlock(BlockType &block) override
Definition: worst_fit.h:113