Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
48template <typename Derived>
50 protected:
51 constexpr explicit AllocatableBlock() {
52 // Assert within a function, since `Derived` is not complete when this type
53 // is defined.
54 static_assert(is_contiguous_v<Derived>,
55 "Types derived from AllocatableBlock must also derive from "
56 "ContiguousBlock");
57 }
58
59 public:
61 constexpr bool IsFree() const;
62
66 constexpr bool Used() const { return !IsFree(); }
67
90 constexpr StatusWithSize CanAlloc(Layout layout) const;
91
125 static constexpr BlockResult<Derived> AllocFirst(Derived*&& block,
126 Layout layout);
127
157 static constexpr BlockResult<Derived> AllocLast(Derived*&& block,
158 Layout layout);
159
186 constexpr BlockResult<Derived> Resize(size_t new_inner_size);
187
196 static constexpr BlockResult<Derived> Free(Derived*&& block);
197
198 protected:
200 constexpr StatusWithSize DoCanAlloc(Layout layout) const;
201
203 static constexpr BlockResult<Derived> DoAllocFirst(Derived*&& block,
204 Layout layout);
205
207 static constexpr BlockResult<Derived> DoAllocLast(Derived*&& block,
208 Layout layout);
209
211 constexpr BlockResult<Derived> DoResize(size_t new_inner_size,
212 bool shifted = false);
213
215 static constexpr BlockResult<Derived> DoFree(Derived*&& block);
216
217 private:
218 using BlockResultPrev = internal::GenericBlockResult::Prev;
219 using BlockResultNext = internal::GenericBlockResult::Next;
220
221 constexpr Derived* derived() { return static_cast<Derived*>(this); }
222 constexpr const Derived* derived() const {
223 return static_cast<const Derived*>(this);
224 }
225
226 // AlignableBlock calls DoCanAlloc, DoAllocLast, DoResize
227 template <typename>
228 friend class AlignableBlock;
229
230 // BlockWithLayout calls DoFree, DoResize
231 template <typename>
232 friend class BlockWithLayout;
233};
234
237template <typename BlockType>
238struct is_allocatable : std::is_base_of<internal::AllocatableBase, BlockType> {
239};
240
242template <typename BlockType>
243constexpr bool is_allocatable_v = is_allocatable<BlockType>::value;
244
245// Template method implementations.
246
247template <typename Derived>
248constexpr bool AllocatableBlock<Derived>::IsFree() const {
249 if constexpr (Hardening::kIncludesDebugChecks) {
250 derived()->CheckInvariants();
251 }
252 return derived()->IsFreeUnchecked();
253}
254
255template <typename Derived>
257 Layout layout) const {
258 if constexpr (Hardening::kIncludesDebugChecks) {
259 derived()->CheckInvariants();
260 }
261 return derived()->DoCanAlloc(layout);
262}
263
264template <typename Derived>
266 Layout layout) const {
267 if (!derived()->IsFree()) {
268 return StatusWithSize::FailedPrecondition();
269 }
270 if (layout.size() == 0) {
271 return StatusWithSize::InvalidArgument();
272 }
273 size_t extra = derived()->InnerSize();
274 size_t new_inner_size = AlignUp(layout.size(), Derived::kAlignment);
275 if (PW_SUB_OVERFLOW(extra, new_inner_size, &extra)) {
276 return StatusWithSize::ResourceExhausted();
277 }
278 return StatusWithSize(extra);
279}
280
281template <typename Derived>
283 Derived*&& block, Layout layout) {
284 if (block == nullptr || layout.size() == 0) {
285 return BlockResult<Derived>(block, Status::InvalidArgument());
286 }
287 if constexpr (Hardening::kIncludesRobustChecks) {
288 block->CheckInvariants();
289 }
290 if (!block->IsFree()) {
291 return BlockResult<Derived>(block, Status::FailedPrecondition());
292 }
293 return Derived::DoAllocFirst(std::move(block), layout);
294}
295
296template <typename Derived>
298 Derived*&& block, Layout layout) {
299 size_t size = AlignUp(layout.size(), Derived::kAlignment);
300 layout = Layout(size, layout.alignment());
301 StatusWithSize can_alloc = block->DoCanAlloc(layout);
302 if (!can_alloc.ok()) {
303 return BlockResult<Derived>(block, can_alloc.status());
304 }
305 size_t extra = can_alloc.size();
306 BlockResult<Derived> result(block);
307 if (extra >= Derived::kMinOuterSize) {
308 // Split the large padding off the back.
309 block->DoSplitFirst(block->InnerSize() - extra);
310 result = BlockResult<Derived>(block, BlockResultNext::kSplitNew);
311 }
312 block->SetFree(false);
313 return result;
314}
315
316template <typename Derived>
318 Derived*&& block, Layout layout) {
319 if (block == nullptr || layout.size() == 0) {
320 return BlockResult<Derived>(block, Status::InvalidArgument());
321 }
322 if constexpr (Hardening::kIncludesRobustChecks) {
323 block->CheckInvariants();
324 }
325 if (!block->IsFree()) {
326 return BlockResult<Derived>(block, Status::FailedPrecondition());
327 }
328 return Derived::DoAllocLast(std::move(block), layout);
329}
330
331template <typename Derived>
333 Derived*&& block, Layout layout) {
334 size_t size = AlignUp(layout.size(), Derived::kAlignment);
335 layout = Layout(size, layout.alignment());
336 StatusWithSize can_alloc = block->DoCanAlloc(layout);
337 if (!can_alloc.ok()) {
338 return BlockResult<Derived>(block, can_alloc.status());
339 }
340 size_t extra = can_alloc.size();
341 BlockResult<Derived> result(block);
342 Derived* prev = block->Prev();
343 if (extra >= Derived::kMinOuterSize) {
344 // Split the large padding off the front.
345 block = block->DoSplitLast(layout.size());
346 prev = block->Prev();
347 result = BlockResult<Derived>(block, BlockResultPrev::kSplitNew);
348
349 } else if (extra != 0 && prev != nullptr) {
350 // The small amount of padding can be appended to the previous block.
351 prev->DoResize(prev->InnerSize() + extra, true).IgnoreUnlessStrict();
352 block = prev->Next();
353 result =
354 BlockResult<Derived>(block, BlockResultPrev::kResizedLarger, extra);
355 }
356 block->SetFree(false);
357 return result;
358}
359
360template <typename Derived>
362 size_t new_inner_size) {
363 if constexpr (Hardening::kIncludesRobustChecks) {
364 derived()->CheckInvariants();
365 }
366 if (derived()->IsFree()) {
367 return BlockResult<Derived>(derived(), Status::FailedPrecondition());
368 }
369 return derived()->DoResize(new_inner_size, /* shifted: */ false);
370}
371
372template <typename Derived>
374 size_t new_inner_size, bool) {
375 size_t old_inner_size = derived()->InnerSize();
376 new_inner_size = AlignUp(new_inner_size, Derived::kAlignment);
377 if (old_inner_size == new_inner_size) {
378 return BlockResult<Derived>(derived());
379 }
380
381 // Treat the block as free and try to combine it with the next block. At most
382 // one free block is expected to follow this block.
383 derived()->SetFree(true);
384 Derived* next = derived()->Next();
385 BlockResult<Derived> result(derived());
386 if (next != nullptr && next->IsFree()) {
387 derived()->DoMergeNext();
388 result = BlockResult<Derived>(derived(), BlockResultNext::kMerged);
389 }
390 size_t merged_inner_size = derived()->InnerSize();
391 if (merged_inner_size < new_inner_size) {
392 // The merged block is too small for the resized block. Restore the original
393 // blocks as needed.
394 if (merged_inner_size != old_inner_size) {
395 derived()->DoSplitFirst(old_inner_size);
396 }
397 derived()->SetFree(false);
398 return BlockResult<Derived>(derived(), Status::ResourceExhausted());
399 }
400 if (new_inner_size + Derived::kMinOuterSize <= merged_inner_size) {
401 // There is enough room after the resized block for another trailing block.
402 derived()->DoSplitFirst(new_inner_size);
403 result = result.next() == BlockResultNext::kMerged
404 ? BlockResult<Derived>(derived(), BlockResultNext::kResized)
405 : BlockResult<Derived>(derived(), BlockResultNext::kSplitNew);
406 }
407 derived()->SetFree(false);
408 return result;
409}
410
411template <typename Derived>
413 Derived*&& block) {
414 if (block == nullptr) {
415 return BlockResult<Derived>(block, Status::InvalidArgument());
416 }
417 if constexpr (Hardening::kIncludesRobustChecks) {
418 block->CheckInvariants();
419 }
420 return Derived::DoFree(std::move(block));
421}
422
423template <typename Derived>
425 Derived*&& block) {
426 block->SetFree(true);
427 BlockResult<Derived> result(block);
428
429 // Try to merge the previous block with this one.
430 Derived* prev = block->Prev();
431 if (prev != nullptr && prev->IsFree()) {
432 prev->DoMergeNext();
433 block = prev;
434 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
435 }
436
437 // Try to merge this block with the next one.
438 Derived* next = block->Next();
439 if (next != nullptr && next->IsFree()) {
440 block->DoMergeNext();
441 result = BlockResult<Derived>(block, BlockResultNext::kMerged);
442 }
443
444 if constexpr (Hardening::kIncludesDebugChecks) {
445 block->CheckInvariants();
446 }
447 return result;
448}
449
450} // namespace pw::allocator
Definition: status_with_size.h:49
constexpr bool ok() const
True if status() == OkStatus().
Definition: status_with_size.h:152
constexpr size_t size() const
Definition: status_with_size.h:146
Definition: allocatable.h:49
constexpr BlockResult< Derived > Resize(size_t new_inner_size)
Definition: allocatable.h:361
constexpr bool IsFree() const
Definition: allocatable.h:248
constexpr bool Used() const
Definition: allocatable.h:66
static constexpr BlockResult< Derived > DoFree(Derived *&&block)
Definition: allocatable.h:424
constexpr StatusWithSize CanAlloc(Layout layout) const
Definition: allocatable.h:256
static constexpr BlockResult< Derived > AllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:282
constexpr BlockResult< Derived > DoResize(size_t new_inner_size, bool shifted=false)
Definition: allocatable.h:373
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:332
static constexpr BlockResult< Derived > AllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:317
static constexpr BlockResult< Derived > Free(Derived *&&block)
Definition: allocatable.h:412
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:297
constexpr StatusWithSize DoCanAlloc(Layout layout) const
Definition: allocatable.h:265
Definition: result.h:114
Definition: layout.h:56
#define PW_SUB_OVERFLOW(a, b, out)
Definition: compiler.h:294
constexpr size_t AlignUp(uintptr_t value, size_t alignment)
Returns the value rounded up to the nearest multiple of alignment.
Definition: alignment.h:52
Definition: allocatable.h:30
Definition: allocatable.h:238