Pigweed
 
Loading...
Searching...
No Matches
base.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/poisonable.h"
20#include "pw_allocator/config.h"
21#include "pw_allocator/hardening.h"
22#include "pw_allocator/layout.h"
23#include "pw_assert/assert.h"
24#include "pw_bytes/alignment.h"
25
26namespace pw::allocator::internal {
27
54template <typename Derived, typename BlockType_, typename ItemType_>
56 public:
57 using BlockType = BlockType_;
58 using ItemType = ItemType_;
59
60 constexpr BucketBase() {
61 if constexpr (is_poisonable_v<BlockType>) {
62 static_assert(BlockType::kPoisonOffset >= sizeof(ItemType),
63 "Block type does not reserve sufficient space for an item");
64 }
65 }
66
68 constexpr bool empty() const { return derived()->items_.empty(); }
69
71 constexpr size_t max_inner_size() const { return max_inner_size_; }
72
76 constexpr void set_max_inner_size(size_t max_inner_size) {
77 PW_ASSERT(empty());
78 max_inner_size_ = max_inner_size;
79 }
80
85 [[nodiscard]] bool Add(BlockType& block) {
86 if (block.InnerSize() < sizeof(ItemType)) {
87 return false;
88 }
89 if constexpr (Hardening::kIncludesDebugChecks) {
90 PW_ASSERT(block.InnerSize() <= max_inner_size_);
91 PW_ASSERT(IsAlignedAs<ItemType>(block.UsableSpace()));
92 }
93 derived()->DoAdd(block);
94 return true;
95 }
96
100 [[nodiscard]] BlockType* RemoveAny() {
101 return empty() ? nullptr : derived()->DoRemoveAny();
102 }
103
108 [[nodiscard]] bool Remove(BlockType& block) {
109 return block.InnerSize() >= sizeof(ItemType) && derived()->DoRemove(block);
110 }
111
119 [[nodiscard]] BlockType* RemoveCompatible(Layout layout) {
120 return derived()->DoRemoveCompatible(layout);
121 }
122
124 void Clear() { derived()->items_.clear(); }
125
137 template <typename Iterator, typename Predicate>
138 static Iterator FindPrevIf(Iterator before_first,
139 Iterator last,
140 Predicate predicate) {
141 Iterator prev = before_first;
142 Iterator iter = prev;
143 ++iter;
144 for (; iter != last; ++iter) {
145 if (predicate(*iter)) {
146 break;
147 }
148 prev = iter;
149 }
150 return prev;
151 }
152
157 static auto MakeCanAllocPredicate(Layout layout) {
158 return [layout](ItemType& item) {
159 auto* block = BlockType::FromUsableSpace(&item);
160 return block->CanAlloc(layout).ok();
161 };
162 }
163
166 template <typename Iterator>
167 constexpr BlockType* GetBlockFromIterator(Iterator iter, Iterator last) {
168 return iter != last ? BlockType::FromUsableSpace(&(*iter)) : nullptr;
169 }
170
173 template <typename Iterator>
174 constexpr BlockType* GetBlockFromPrev(Iterator prev, Iterator last) {
175 return GetBlockFromIterator(++prev, last);
176 }
177
178 protected:
185 static ItemType& GetItemFrom(BlockType& block) {
186 return *(std::launder(reinterpret_cast<ItemType*>(block.UsableSpace())));
187 }
188
189 private:
190 constexpr Derived* derived() { return static_cast<Derived*>(this); }
191 constexpr const Derived* derived() const {
192 return static_cast<const Derived*>(this);
193 }
194
196 size_t max_inner_size_ = std::numeric_limits<size_t>::max();
197};
198
199} // namespace pw::allocator::internal
Definition: layout.h:56
constexpr void set_max_inner_size(size_t max_inner_size)
Definition: base.h:76
constexpr BlockType * GetBlockFromIterator(Iterator iter, Iterator last)
Definition: base.h:167
bool Remove(BlockType &block)
Definition: base.h:108
constexpr size_t max_inner_size() const
Returns the configured maximum inner size for blocks in this bucket.
Definition: base.h:71
BlockType * RemoveAny()
Definition: base.h:100
static auto MakeCanAllocPredicate(Layout layout)
Definition: base.h:157
constexpr BlockType * GetBlockFromPrev(Iterator prev, Iterator last)
Definition: base.h:174
void Clear()
Removes all blocks from this bucket.
Definition: base.h:124
static ItemType & GetItemFrom(BlockType &block)
Definition: base.h:185
bool Add(BlockType &block)
Definition: base.h:85
static Iterator FindPrevIf(Iterator before_first, Iterator last, Predicate predicate)
Definition: base.h:138
constexpr bool empty() const
Returns whether this buckets contains any free blocks.
Definition: base.h:68
BlockType * RemoveCompatible(Layout layout)
Definition: base.h:119