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 static_assert(is_allocatable_v<Derived>,
48 "Types derived from AlignableBlock must also derive from "
49 "AllocatableBlock");
50 }
51
53 constexpr StatusWithSize DoCanAlloc(Layout layout) const;
54
56 static constexpr BlockResult<Derived> DoAllocFirst(Derived*&& block,
57 Layout layout);
58
60 static constexpr BlockResult<Derived> DoAllocLast(Derived*&& block,
61 Layout layout);
62
63 private:
64 constexpr const Derived* derived() const {
65 return static_cast<const Derived*>(this);
66 }
67
69 static constexpr BlockResult<Derived> DoAllocAligned(Derived*&& block,
70 size_t leading,
71 size_t new_inner_size);
72
73 // BlockWithLayout calls DoAllocFirst
74 template <typename>
75 friend class BlockWithLayout;
76};
77
79template <typename BlockType>
80struct is_alignable : std::is_base_of<internal::AlignableBase, BlockType> {};
81
83template <typename BlockType>
85
87
88// Template method implementations.
89
90template <typename Derived>
92 Layout layout) const {
93 // How much extra space is available?
94 auto result = derived()->AllocatableBlock<Derived>::DoCanAlloc(layout);
95 if (!result.ok()) {
96 return result;
97 }
98 size_t extra = result.size();
99
100 // Is the default alignment sufficient?
101 if (layout.alignment() <= Derived::kAlignment) {
102 return StatusWithSize(extra);
103 }
104
105 // What is the last aligned address within the leading extra space?
106 auto addr = cpp20::bit_cast<uintptr_t>(derived()->UsableSpace());
107 uintptr_t aligned_addr = addr;
108 Hardening::Increment(aligned_addr, extra);
109 aligned_addr = AlignDown(aligned_addr, layout.alignment());
110
111 // Is there an aligned address within the extra space?
112 if (aligned_addr < addr) {
113 return StatusWithSize::ResourceExhausted();
114 }
115
116 // If splitting the first block, is there enough extra for a valid block?
117 size_t leading_outer_size = aligned_addr - addr;
118 if (leading_outer_size != 0 && leading_outer_size < Derived::kMinOuterSize &&
119 derived()->Prev() == nullptr) {
120 return StatusWithSize::ResourceExhausted();
121 }
122
123 return StatusWithSize(leading_outer_size);
124}
125
126template <typename Derived>
128 Derived*&& block, Layout layout) {
129 // Is the default alignment sufficient?
130 if (layout.alignment() <= Derived::kAlignment) {
131 return AllocatableBlock<Derived>::DoAllocFirst(std::move(block), layout);
132 }
133
134 // What is the first aligned address within the leading extra space?
135 size_t size = AlignUp(layout.size(), Derived::kAlignment);
136 layout = Layout(size, layout.alignment());
137 StatusWithSize can_alloc = block->DoCanAlloc(layout);
138 if (!can_alloc.ok()) {
139 return BlockResult<Derived>(block, can_alloc.status());
140 }
141 size_t extra = can_alloc.size();
142 size_t leading_outer_size = extra - AlignDown(extra, layout.alignment());
143
144 // If splitting the first block, there must be enough for a valid block.
145 if (leading_outer_size != 0 && leading_outer_size <= Derived::kMinOuterSize &&
146 block->Prev() == nullptr) {
147 leading_outer_size += AlignUp(Derived::kMinOuterSize - leading_outer_size,
148 layout.alignment());
149 }
150 if (leading_outer_size > extra) {
152 }
153
154 // Allocate the aligned block.
155 return DoAllocAligned(std::move(block), leading_outer_size, layout.size());
156}
157
158template <typename Derived>
160 Derived*&& block, Layout layout) {
161 // Is the default alignment sufficient?
162 if (layout.alignment() <= Derived::kAlignment) {
163 return AllocatableBlock<Derived>::DoAllocLast(std::move(block), layout);
164 }
165
166 // What is the last aligned address within the leading extra space?
167 size_t size = AlignUp(layout.size(), Derived::kAlignment);
168 layout = Layout(size, layout.alignment());
169 StatusWithSize can_alloc = block->DoCanAlloc(layout);
170 if (!can_alloc.ok()) {
171 return BlockResult<Derived>(block, can_alloc.status());
172 }
173 size_t leading_outer_size = can_alloc.size();
174
175 // Allocate the aligned block.
176 return DoAllocAligned(std::move(block), leading_outer_size, layout.size());
177}
178
179template <typename Derived>
181 Derived*&& block, size_t leading_outer_size, size_t new_inner_size) {
182 // Allocate everything after aligned address.
183 Layout layout(block->InnerSize() - leading_outer_size, Derived::kAlignment);
184 auto alloc_result =
185 AllocatableBlock<Derived>::DoAllocLast(std::move(block), layout);
186 if (!alloc_result.ok()) {
187 return alloc_result;
188 }
189 block = alloc_result.block();
190
191 // Resize the allocation to the requested size.
192 auto resize_result =
193 block->AllocatableBlock<Derived>::DoResize(new_inner_size);
194 if (!resize_result.ok()) {
195 return resize_result;
196 }
197
198 return BlockResult<Derived>(
199 block, alloc_result.prev(), resize_result.next(), alloc_result.size());
200}
201
202} // namespace pw::allocator
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: alignable.h:44
constexpr StatusWithSize DoCanAlloc(Layout layout) const
Definition: alignable.h:91
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: alignable.h:127
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: alignable.h:159
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: allocatable.h:301
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: allocatable.h:266
Definition: result.h:106
Definition: with_layout.h:50
Definition: layout.h:64
constexpr bool is_alignable_v
Helper variable template for is_alignable<BlockType>::value.
Definition: alignable.h:84
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:42
Definition: alignable.h:31
Trait type that allows interrogating a block as to whether it is alignable.
Definition: alignable.h:80