Pigweed
 
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
30class UnorderedItem : public IntrusiveForwardList<UnorderedItem>::Item {};
31
39template <typename BlockType>
40class UnorderedBucket : public internal::BucketBase<UnorderedBucket<BlockType>,
41 BlockType,
42 UnorderedItem> {
43 private:
44 using Base = internal::
45 BucketBase<UnorderedBucket<BlockType>, BlockType, UnorderedItem>;
46 friend Base;
47
48 public:
50
51 private:
53 void DoAdd(BlockType& block) {
54 auto* item = new (block.UsableSpace()) UnorderedItem();
55 items_.push_front(*item);
56 }
57
59 BlockType* DoRemoveAny() {
60 UnorderedItem& item = items_.front();
61 items_.pop_front();
62 return BlockType::FromUsableSpace(&item);
63 }
64
66 bool DoRemove(BlockType& block) {
67 return items_.remove(Base::GetItemFrom(block));
68 }
69
71 BlockType* DoRemoveCompatible(Layout layout) {
72 auto prev = Base::FindPrevIf(items_.before_begin(),
73 items_.end(),
75 auto* block = Base::GetBlockFromPrev(prev, items_.end());
76 if (block != nullptr) {
77 items_.erase_after(prev);
78 }
79 return block;
80 }
81
83};
84
85} // namespace pw::allocator
Definition: intrusive_forward_list.h:86
Definition: layout.h:56
Definition: unordered.h:42
Definition: unordered.h:30
constexpr BlockType * GetBlockFromPrev(Iterator prev, Iterator last)
Definition: base.h:174
void Clear()
Removes all blocks from this bucket.
Definition: base.h:124
static Iterator FindPrevIf(Iterator before_first, Iterator last, Predicate predicate)
Definition: base.h:138