C/C++ API Reference
Loading...
Searching...
No Matches
allocatable.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/block/contiguous.h"
19#include "pw_allocator/block/result.h"
20#include "pw_allocator/hardening.h"
21#include "pw_allocator/layout.h"
22#include "pw_bytes/alignment.h"
23#include "pw_status/status.h"
24#include "pw_status/status_with_size.h"
25
26namespace pw::allocator {
27namespace internal {
28
29// Trivial base class for trait support.
31
32} // namespace internal
33
35
50template <typename Derived>
52 protected:
53 constexpr explicit AllocatableBlock() {
54 // Assert within a function, since `Derived` is not complete when this type
55 // is defined.
56 static_assert(is_contiguous_v<Derived>,
57 "Types derived from AllocatableBlock must also derive from "
58 "ContiguousBlock");
59 }
60
61 public:
63 constexpr bool IsFree() const;
64
65 // clang-format off
79 // clang-format on
80 constexpr StatusWithSize CanAlloc(Layout layout) const;
81
108 static constexpr BlockResult<Derived> AllocFirst(Derived*&& block,
109 Layout layout);
110
133 static constexpr BlockResult<Derived> AllocLast(Derived*&& block,
134 Layout layout);
135
155 constexpr BlockResult<Derived> Resize(size_t new_inner_size);
156
165 static constexpr BlockResult<Derived> Free(Derived*&& block);
166
167 protected:
169 constexpr StatusWithSize DoCanAlloc(Layout layout) const;
170
172 static constexpr BlockResult<Derived> DoAllocFirst(Derived*&& block,
173 Layout layout);
174
176 static constexpr BlockResult<Derived> DoAllocLast(Derived*&& block,
177 Layout layout);
178
180 constexpr BlockResult<Derived> DoResize(size_t new_inner_size,
181 bool shifted = false);
182
184 static constexpr BlockResult<Derived> DoFree(Derived*&& block);
185
186 private:
187 using BlockResultPrev = internal::GenericBlockResult::Prev;
188 using BlockResultNext = internal::GenericBlockResult::Next;
189
190 constexpr Derived* derived() { return static_cast<Derived*>(this); }
191 constexpr const Derived* derived() const {
192 return static_cast<const Derived*>(this);
193 }
194
195 // AlignableBlock calls DoCanAlloc, DoAllocLast, DoResize
196 template <typename>
197 friend class AlignableBlock;
198
199 // BlockWithLayout calls DoFree, DoResize
200 template <typename>
201 friend class BlockWithLayout;
202};
203
206template <typename BlockType>
207struct is_allocatable : std::is_base_of<internal::AllocatableBase, BlockType> {
208};
209
211template <typename BlockType>
213
215
216// Template method implementations.
217
218template <typename Derived>
219constexpr bool AllocatableBlock<Derived>::IsFree() const {
220 if constexpr (Hardening::kIncludesDebugChecks) {
221 derived()->CheckInvariants();
222 }
223 return derived()->IsFreeUnchecked();
224}
225
226template <typename Derived>
228 Layout layout) const {
229 if constexpr (Hardening::kIncludesDebugChecks) {
230 derived()->CheckInvariants();
231 }
232 return derived()->DoCanAlloc(layout);
233}
234
235template <typename Derived>
237 Layout layout) const {
238 if (!derived()->IsFree()) {
239 return StatusWithSize::FailedPrecondition();
240 }
241 if (layout.size() == 0) {
242 return StatusWithSize::InvalidArgument();
243 }
244 size_t extra = derived()->InnerSize();
245 size_t new_inner_size = AlignUp(layout.size(), Derived::kAlignment);
246 if (!CheckedSub(extra, new_inner_size, extra)) {
247 return StatusWithSize::ResourceExhausted();
248 }
249 return StatusWithSize(extra);
250}
251
252template <typename Derived>
254 Derived*&& block, Layout layout) {
255 if (block == nullptr || layout.size() == 0) {
257 }
258 if constexpr (Hardening::kIncludesRobustChecks) {
259 block->CheckInvariants();
260 }
261 if (!block->IsFree()) {
263 }
264 return Derived::DoAllocFirst(std::move(block), layout);
265}
266
267template <typename Derived>
269 Derived*&& block, Layout layout) {
270 size_t size = AlignUp(layout.size(), Derived::kAlignment);
271 layout = Layout(size, layout.alignment());
272 StatusWithSize can_alloc = block->DoCanAlloc(layout);
273 if (!can_alloc.ok()) {
274 return BlockResult<Derived>(block, can_alloc.status());
275 }
276 size_t extra = can_alloc.size();
277 BlockResult<Derived> result(block);
278 if (extra >= Derived::kMinOuterSize) {
279 // Split the large padding off the back.
280 block->DoSplitFirst(block->InnerSize() - extra);
281 result = BlockResult<Derived>(block, BlockResultNext::kSplitNew);
282 }
283 block->SetFree(false);
284 return result;
285}
286
287template <typename Derived>
289 Derived*&& block, Layout layout) {
290 if (block == nullptr || layout.size() == 0) {
292 }
293 if constexpr (Hardening::kIncludesRobustChecks) {
294 block->CheckInvariants();
295 }
296 if (!block->IsFree()) {
298 }
299 return Derived::DoAllocLast(std::move(block), layout);
300}
301
302template <typename Derived>
304 Derived*&& block, Layout layout) {
305 size_t size = AlignUp(layout.size(), Derived::kAlignment);
306 layout = Layout(size, layout.alignment());
307 StatusWithSize can_alloc = block->DoCanAlloc(layout);
308 if (!can_alloc.ok()) {
309 return BlockResult<Derived>(block, can_alloc.status());
310 }
311 size_t extra = can_alloc.size();
312 BlockResult<Derived> result(block);
313 Derived* prev = block->Prev();
314 if (extra >= Derived::kMinOuterSize) {
315 // Split the large padding off the front.
316 block = block->DoSplitLast(layout.size());
317 prev = block->Prev();
318 result = BlockResult<Derived>(block, BlockResultPrev::kSplitNew);
319
320 } else if (extra != 0 && prev != nullptr) {
321 // The small amount of padding can be appended to the previous block.
322 prev->DoResize(prev->InnerSize() + extra, true).IgnoreUnlessStrict();
323 block = prev->Next();
324 result =
325 BlockResult<Derived>(block, BlockResultPrev::kResizedLarger, extra);
326 }
327 block->SetFree(false);
328 return result;
329}
330
331template <typename Derived>
333 size_t new_inner_size) {
334 if constexpr (Hardening::kIncludesRobustChecks) {
335 derived()->CheckInvariants();
336 }
337 if (derived()->IsFree()) {
339 }
340 return derived()->DoResize(new_inner_size, /* shifted: */ false);
341}
342
343template <typename Derived>
345 size_t new_inner_size, bool) {
346 size_t old_inner_size = derived()->InnerSize();
347 new_inner_size = AlignUp(new_inner_size, Derived::kAlignment);
348 if (old_inner_size == new_inner_size) {
349 return BlockResult<Derived>(derived());
350 }
351
352 // Treat the block as free and try to combine it with the next block. At most
353 // one free block is expected to follow this block.
354 derived()->SetFree(true);
355 Derived* next = derived()->Next();
356 BlockResult<Derived> result(derived());
357 if (next != nullptr && next->IsFree()) {
358 derived()->DoMergeNext();
359 result = BlockResult<Derived>(derived(), BlockResultNext::kMerged);
360 }
361 size_t merged_inner_size = derived()->InnerSize();
362 if (merged_inner_size < new_inner_size) {
363 // The merged block is too small for the resized block. Restore the original
364 // blocks as needed.
365 if (merged_inner_size != old_inner_size) {
366 derived()->DoSplitFirst(old_inner_size);
367 }
368 derived()->SetFree(false);
370 }
371 if (new_inner_size + Derived::kMinOuterSize <= merged_inner_size) {
372 // There is enough room after the resized block for another trailing block.
373 derived()->DoSplitFirst(new_inner_size);
374 result = result.next() == BlockResultNext::kMerged
375 ? BlockResult<Derived>(derived(), BlockResultNext::kResized)
376 : BlockResult<Derived>(derived(), BlockResultNext::kSplitNew);
377 }
378 derived()->SetFree(false);
379 return result;
380}
381
382template <typename Derived>
384 Derived*&& block) {
385 if (block == nullptr) {
387 }
388 if constexpr (Hardening::kIncludesRobustChecks) {
389 block->CheckInvariants();
390 }
391 return Derived::DoFree(std::move(block));
392}
393
394template <typename Derived>
396 Derived*&& block) {
397 block->SetFree(true);
398 BlockResult<Derived> result(block);
399
400 // Try to merge the previous block with this one.
401 Derived* prev = block->Prev();
402 if (prev != nullptr && prev->IsFree()) {
403 prev->DoMergeNext();
404 block = prev;
405 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
406 }
407
408 // Try to merge this block with the next one.
409 Derived* next = block->Next();
410 if (next != nullptr && next->IsFree()) {
411 block->DoMergeNext();
412 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
413 }
414
415 if constexpr (Hardening::kIncludesDebugChecks) {
416 block->CheckInvariants();
417 }
418 return result;
419}
420
421} // namespace pw::allocator
static constexpr Status InvalidArgument()
Definition: status.h:164
static constexpr Status FailedPrecondition()
Definition: status.h:243
static constexpr Status ResourceExhausted()
Definition: status.h:230
Definition: status_with_size.h:51
constexpr bool ok() const
True if status() == OkStatus().
Definition: status_with_size.h:154
constexpr size_t size() const
Definition: status_with_size.h:148
Definition: allocatable.h:51
constexpr BlockResult< Derived > Resize(size_t new_inner_size)
Definition: allocatable.h:332
constexpr bool IsFree() const
Definition: allocatable.h:219
static constexpr BlockResult< Derived > DoFree(Derived *&&block)
Definition: allocatable.h:395
constexpr StatusWithSize CanAlloc(Layout layout) const
Definition: allocatable.h:227
static constexpr BlockResult< Derived > AllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:253
constexpr BlockResult< Derived > DoResize(size_t new_inner_size, bool shifted=false)
Definition: allocatable.h:344
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:303
static constexpr BlockResult< Derived > AllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:288
static constexpr BlockResult< Derived > Free(Derived *&&block)
Definition: allocatable.h:383
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:268
constexpr StatusWithSize DoCanAlloc(Layout layout) const
Definition: allocatable.h:236
Definition: result.h:116
Definition: layout.h:58
constexpr bool is_allocatable_v
Helper variable template for is_allocatable<BlockType>::value.
Definition: allocatable.h:212
constexpr size_t AlignUp(uintptr_t value, size_t alignment)
Returns the value rounded up to the nearest multiple of alignment.
Definition: alignment.h:54
constexpr bool CheckedSub(A a, B b, T &result)
Definition: checked_arithmetic.h:138
Definition: allocatable.h:30
Definition: allocatable.h:207