C/C++ API Reference
Loading...
Searching...
No Matches
alignable.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 <cstdint>
18
19#include "lib/stdcompat/bit.h"
20#include "pw_allocator/block/allocatable.h"
21#include "pw_allocator/block/result.h"
22#include "pw_allocator/hardening.h"
23#include "pw_allocator/layout.h"
24#include "pw_bytes/alignment.h"
25#include "pw_status/status.h"
26
27namespace pw::allocator {
28namespace internal {
29
30// Trivial base class for trait support.
31struct AlignableBase {};
32
33} // namespace internal
34
36
43template <typename Derived>
45 protected:
46 constexpr explicit AlignableBlock() {
47 // Assert within a function, since `Derived` is not complete when this type
48 // is defined.
49 static_assert(is_allocatable_v<Derived>,
50 "Types derived from AlignableBlock must also derive from "
51 "AllocatableBlock");
52 }
53
55 constexpr StatusWithSize DoCanAlloc(Layout layout) const;
56
58 static constexpr BlockResult<Derived> DoAllocFirst(Derived*&& block,
59 Layout layout);
60
62 static constexpr BlockResult<Derived> DoAllocLast(Derived*&& block,
63 Layout layout);
64
65 private:
66 constexpr const Derived* derived() const {
67 return static_cast<const Derived*>(this);
68 }
69
71 static constexpr BlockResult<Derived> DoAllocAligned(Derived*&& block,
72 size_t leading,
73 size_t new_inner_size);
74
75 // BlockWithLayout calls DoAllocFirst
76 template <typename>
77 friend class BlockWithLayout;
78};
79
81template <typename BlockType>
82struct is_alignable : std::is_base_of<internal::AlignableBase, BlockType> {};
83
85template <typename BlockType>
87
89
90// Template method implementations.
91
92template <typename Derived>
94 Layout layout) const {
95 // How much extra space is available?
96 auto result = derived()->AllocatableBlock<Derived>::DoCanAlloc(layout);
97 if (!result.ok()) {
98 return result;
99 }
100 size_t extra = result.size();
101
102 // Is the default alignment sufficient?
103 if (layout.alignment() <= Derived::kAlignment) {
104 return StatusWithSize(extra);
105 }
106
107 // What is the last aligned address within the leading extra space?
108 auto addr = cpp20::bit_cast<uintptr_t>(derived()->UsableSpace());
109 uintptr_t aligned_addr = addr;
110 Hardening::Increment(aligned_addr, extra);
111 aligned_addr = AlignDown(aligned_addr, layout.alignment());
112
113 // Is there an aligned address within the extra space?
114 if (aligned_addr < addr) {
115 return StatusWithSize::ResourceExhausted();
116 }
117
118 // If splitting the first block, is there enough extra for a valid block?
119 size_t leading_outer_size = aligned_addr - addr;
120 if (leading_outer_size != 0 && leading_outer_size < Derived::kMinOuterSize &&
121 derived()->Prev() == nullptr) {
122 return StatusWithSize::ResourceExhausted();
123 }
124
125 return StatusWithSize(leading_outer_size);
126}
127
128template <typename Derived>
130 Derived*&& block, Layout layout) {
131 // Is the default alignment sufficient?
132 if (layout.alignment() <= Derived::kAlignment) {
133 return AllocatableBlock<Derived>::DoAllocFirst(std::move(block), layout);
134 }
135
136 // What is the first aligned address within the leading extra space?
137 size_t size = AlignUp(layout.size(), Derived::kAlignment);
138 layout = Layout(size, layout.alignment());
139 StatusWithSize can_alloc = block->DoCanAlloc(layout);
140 if (!can_alloc.ok()) {
141 return BlockResult<Derived>(block, can_alloc.status());
142 }
143 size_t extra = can_alloc.size();
144 size_t leading_outer_size = extra - AlignDown(extra, layout.alignment());
145
146 // If splitting the first block, there must be enough for a valid block.
147 if (leading_outer_size != 0 && leading_outer_size <= Derived::kMinOuterSize &&
148 block->Prev() == nullptr) {
149 leading_outer_size += AlignUp(Derived::kMinOuterSize - leading_outer_size,
150 layout.alignment());
151 }
152 if (leading_outer_size > extra) {
154 }
155
156 // Allocate the aligned block.
157 return DoAllocAligned(std::move(block), leading_outer_size, layout.size());
158}
159
160template <typename Derived>
162 Derived*&& block, Layout layout) {
163 // Is the default alignment sufficient?
164 if (layout.alignment() <= Derived::kAlignment) {
165 return AllocatableBlock<Derived>::DoAllocLast(std::move(block), layout);
166 }
167
168 // What is the last aligned address within the leading extra space?
169 size_t size = AlignUp(layout.size(), Derived::kAlignment);
170 layout = Layout(size, layout.alignment());
171 StatusWithSize can_alloc = block->DoCanAlloc(layout);
172 if (!can_alloc.ok()) {
173 return BlockResult<Derived>(block, can_alloc.status());
174 }
175 size_t leading_outer_size = can_alloc.size();
176
177 // Allocate the aligned block.
178 return DoAllocAligned(std::move(block), leading_outer_size, layout.size());
179}
180
181template <typename Derived>
183 Derived*&& block, size_t leading_outer_size, size_t new_inner_size) {
184 // Allocate everything after aligned address.
185 Layout layout(block->InnerSize() - leading_outer_size, Derived::kAlignment);
186 auto alloc_result =
187 AllocatableBlock<Derived>::DoAllocLast(std::move(block), layout);
188 if (!alloc_result.ok()) {
189 return alloc_result;
190 }
191 block = alloc_result.block();
192
193 // Resize the allocation to the requested size.
194 auto resize_result = block->DoResize(new_inner_size);
195 if (!resize_result.ok()) {
196 return resize_result;
197 }
198
199 return BlockResult<Derived>(
200 block, alloc_result.prev(), resize_result.next(), alloc_result.size());
201}
202
203} // namespace pw::allocator
static constexpr Status ResourceExhausted()
Definition: status.h:157
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: alignable.h:44
constexpr StatusWithSize DoCanAlloc(Layout layout) const
Definition: alignable.h:93
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: alignable.h:129
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: alignable.h:161
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:336
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:301
Definition: result.h:116
Definition: with_layout.h:50
Definition: layout.h:58
constexpr bool is_alignable_v
Helper variable template for is_alignable<BlockType>::value.
Definition: alignable.h:86
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 size_t AlignDown(uintptr_t value, size_t alignment)
Returns the value rounded down to the nearest multiple of alignment.
Definition: alignment.h:41
Definition: alignable.h:31
Trait type that allows interrogating a block as to whether it is alignable.
Definition: alignable.h:82