C/C++ API Reference
Loading...
Searching...
No Matches
unordered.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 UnorderedItem : public IntrusiveForwardList<UnorderedItem>::Item {};
33
41template <typename BlockType>
42class UnorderedBucket : public internal::BucketBase<UnorderedBucket<BlockType>,
43 BlockType,
44 UnorderedItem> {
45 private:
46 using Base = internal::
47 BucketBase<UnorderedBucket<BlockType>, BlockType, UnorderedItem>;
48 friend Base;
49
50 public:
52
53 private:
55 void DoAdd(BlockType& block) {
56 auto* item = new (block.UsableSpace()) UnorderedItem();
57 items_.push_front(*item);
58 }
59
61 BlockType* DoRemoveAny() {
62 UnorderedItem& item = items_.front();
63 items_.pop_front();
64 return BlockType::FromUsableSpace(&item);
65 }
66
68 const BlockType* DoFindLargest() const {
69 auto iter = std::max_element(items_.begin(), items_.end(), Base::Compare);
70 return BlockType::FromUsableSpace(&(*iter));
71 }
72
74 bool DoRemove(BlockType& block) {
75 return items_.remove(Base::GetItemFrom(block));
76 }
77
79 BlockType* DoRemoveCompatible(Layout layout) {
80 auto prev = Base::FindPrevIf(items_.before_begin(),
81 items_.end(),
83 auto* block = Base::GetBlockFromPrev(prev, items_.end());
84 if (block != nullptr) {
85 items_.erase_after(prev);
86 }
87 return block;
88 }
89
91};
92
94
95} // namespace pw::allocator
Definition: intrusive_forward_list.h:91
Definition: layout.h:58
Definition: unordered.h:44
Definition: unordered.h:32
static constexpr BlockType * GetBlockFromPrev(Iterator prev, Iterator last)
Definition: base.h:195
static bool Compare(const ItemType &item1, const ItemType &item2)
Definition: base.h:178
void Clear()
Removes all blocks from this bucket.
Definition: base.h:134
static Iterator FindPrevIf(Iterator before_first, Iterator last, Predicate predicate)
Definition: base.h:150