C/C++ API Reference
Loading...
Searching...
No Matches
sorted.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
18#include "pw_allocator/bucket/base.h"
19#include "pw_containers/intrusive_forward_list.h"
20
21namespace pw::allocator {
22
24
32class SortedItem : public IntrusiveForwardList<SortedItem>::Item {};
33
34// Forward declarations.
35
36template <typename BlockType>
38
39template <typename BlockType>
41
42namespace internal {
43
54template <typename Derived, typename BlockType>
55class SortedBucketBase : public BucketBase<Derived, BlockType, SortedItem> {
56 public:
58
59 protected:
61 friend Base;
62
63 constexpr SortedBucketBase() = default;
64
65 const IntrusiveForwardList<SortedItem>& items() const { return items_; }
66
68 void DoAdd(BlockType& block) {
69 auto* item_to_add = new (block.UsableSpace()) SortedItem();
70 auto prev = Base::FindPrevIf(items_.before_begin(),
71 items_.end(),
72 Derived::MakeAddPredicate(block.InnerSize()));
73 items_.insert_after(prev, *item_to_add);
74 }
75
77 BlockType* DoRemoveAny() {
78 SortedItem& item = items_.front();
79 items_.pop_front();
80 return BlockType::FromUsableSpace(&item);
81 }
82
84 bool DoRemove(BlockType& block) {
85 return items_.remove(Base::GetItemFrom(block));
86 }
87
89 BlockType* DoRemoveCompatible(Layout layout) {
90 auto prev = Base::FindPrevIf(items_.before_begin(),
91 items_.end(),
93 auto* block = Base::GetBlockFromPrev(prev, items_.end());
94 if (block != nullptr) {
95 items_.erase_after(prev);
96 }
97 return block;
98 }
99
100 private:
102};
103
104} // namespace internal
105
113template <typename BlockType>
115 : public internal::SortedBucketBase<ForwardSortedBucket<BlockType>,
116 BlockType> {
117 private:
118 using Base =
120
121 friend Base;
122 friend typename Base::Base;
123
128 static constexpr auto MakeAddPredicate(size_t inner_size) {
129 return [inner_size](SortedItem& item) {
130 auto* block = BlockType::FromUsableSpace(&item);
131 return inner_size < block->InnerSize();
132 };
133 }
134
136 const BlockType* DoFindLargest() const {
137 const auto& items = Base::items();
138 if (items.empty()) {
139 return nullptr;
140 }
141 auto iter = items.before_begin();
142 auto prev = iter;
143 do {
144 prev = iter++;
145 } while (iter != items.end());
146 return BlockType::FromUsableSpace(&(*prev));
147 }
148};
149
157template <typename BlockType>
159 : public internal::SortedBucketBase<ReverseSortedBucket<BlockType>,
160 BlockType> {
161 private:
162 using Base =
164
165 friend Base;
166 friend typename Base::Base;
167
172 static constexpr auto MakeAddPredicate(size_t inner_size) {
173 return [inner_size](SortedItem& item) {
174 auto* block = BlockType::FromUsableSpace(&item);
175 return block->InnerSize() < inner_size;
176 };
177 }
178
180 const BlockType* DoFindLargest() const {
181 const auto& items = Base::items();
182 auto iter = items.begin();
183 return BlockType::FromUsableSpace(&(*iter));
184 }
185};
186
188
189} // namespace pw::allocator
Definition: intrusive_forward_list.h:91
Definition: sorted.h:116
Definition: layout.h:58
Definition: sorted.h:160
Definition: sorted.h:32
static constexpr BlockType * GetBlockFromPrev(Iterator prev, Iterator last)
Definition: base.h:195
static auto MakeCanAllocPredicate(Layout layout)
Definition: base.h:169
void Clear()
Removes all blocks from this bucket.
Definition: base.h:134
static ItemType & GetItemFrom(BlockType &block)
Definition: base.h:205
static Iterator FindPrevIf(Iterator before_first, Iterator last, Predicate predicate)
Definition: base.h:150
BlockType * DoRemoveCompatible(Layout layout)
Definition: sorted.h:89
bool DoRemove(BlockType &block)
Definition: sorted.h:84
BlockType * DoRemoveAny()
Definition: sorted.h:77
void DoAdd(BlockType &block)
Definition: sorted.h:68