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/layout.h"
21 #include "pw_assert/assert.h"
22 #include "pw_bytes/alignment.h"
23 #include "pw_status/status.h"
24 #include "pw_status/status_with_size.h"
25
26 namespace pw::allocator {
27 namespace internal {
28
29 // Trivial base class for trait support.
30 struct AllocatableBase {};
31
32 } // namespace internal
33
34 /// Mix-in for blocks that can be allocated and freed.
35 ///
36 /// Block mix-ins are stateless and trivially constructible. See `BasicBlock`
37 /// for details on how mix-ins can be combined to implement blocks.
38 ///
39 /// This mix-in requires its derived type also derive from `ContiguousBlock`
40 /// and provide the following symbols:
41 ///
42 /// - size_t kMinOuterSize
43 /// - Size of the smallest block that can be allocated.
44 /// - bool IsFreeUnchecked() const
45 /// - Returns whether the block is free or in use.
46 /// - void SetFree(bool)
47 /// - Sets whether the block is free or in use.
48 template <typename Derived>
49 class AllocatableBlock : public internal::AllocatableBase {
50 protected:
AllocatableBlock()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:
60 /// @returns whether this block is free or is in use.
61 inline bool IsFree() const;
62
63 /// Indicates whether the block is in use is free.
64 ///
65 /// This method will eventually be deprecated. Prefer `IsFree`.
Used()66 inline bool Used() const { return !IsFree(); }
67
68 /// Checks if a block could be split from the block.
69 ///
70 /// On error, this method will return the same status as `AllocFirst` or
71 /// `AllocLast` without performing any modifications.
72 ///
73 /// @pre The block must not be in use.
74 ///
75 /// @returns @rst
76 ///
77 /// .. pw-status-codes::
78 ///
79 /// OK: Returns the number of bytes to shift this block in order to align
80 /// its usable space.
81 ///
82 /// FAILED_PRECONDITION: This block is in use and cannot be split.
83 ///
84 /// RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
85 /// request. This may be due to a large requested size, or insufficient
86 /// remaining space to fulfill the requested alignment create a valid
87 /// leading block, and/or create a valid trailing block.
88 ///
89 /// @endrst
90 StatusWithSize CanAlloc(Layout layout) const;
91
92 /// Splits an aligned block from the start of the block, and marks it as used.
93 ///
94 /// If successful, `block` will be replaced by a block that has an inner
95 /// size of at least `inner_size`, and whose starting address is aligned to an
96 /// `alignment` boundary. If unsuccessful, `block` will be unmodified.
97 ///
98 /// This method is static in order to consume the given block pointer.
99 /// On success, a pointer to the new, smaller block is returned. In total, up
100 /// to two additional blocks may be created: one to pad the returned block to
101 /// an alignment boundary and one for the trailing space. On error, the
102 /// original pointer is returned.
103 ///
104 /// For larger alignments, the `AllocLast` method is generally preferable to
105 /// this method, as this method may create an additional fragments both before
106 /// and after the returned block in order to align the usable space.
107 ///
108 /// @pre The block must not be in use.
109 ///
110 /// @returns @rst
111 ///
112 /// .. pw-status-codes::
113 ///
114 /// OK: The split completed successfully. The `BlockAllocType` indicates
115 /// how extra memory was distributed to other blocks.
116 ///
117 /// FAILED_PRECONDITION: This block is in use and cannot be split.
118 ///
119 /// RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
120 /// request. This may be due to a large requested size, or insufficient
121 /// remaining space to fulfill the requested alignment create a valid
122 /// leading block, and/or create a valid trailing block.
123 ///
124 /// @endrst
125 static BlockResult<Derived> AllocFirst(Derived*&& block, Layout layout);
126
127 /// Splits an aligned block from the end of the block, and marks it as used.
128 ///
129 /// If successful, `block` will be replaced by a block that has an inner
130 /// size of at least `inner_size`, and whose starting address is aligned to an
131 /// `alignment` boundary. If unsuccessful, `block` will be unmodified.
132 ///
133 /// This method is static in order to consume the given block pointer.
134 /// On success, a pointer to the new, smaller block is returned. In total, up
135 /// to two additional blocks may be created: one to pad the returned block to
136 /// an alignment boundary and one for the trailing space. On error, the
137 /// original pointer is returned.
138 ///
139 /// @pre The block must not be in use.
140 ///
141 /// @returns @rst
142 ///
143 /// .. pw-status-codes::
144 ///
145 /// OK: The split completed successfully. The `BlockAllocType` indicates
146 /// how extra memory was distributed to other blocks.
147 ///
148 /// FAILED_PRECONDITION: This block is in use and cannot be split.
149 ///
150 /// RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
151 /// request. This may be due to a large requested size, or insufficient
152 /// remaining space to fulfill the requested alignment create a valid
153 /// leading block, and/or create a valid trailing block.
154 ///
155 /// @endrst
156 static BlockResult<Derived> AllocLast(Derived*&& block, Layout layout);
157
158 /// Grows or shrinks the block.
159 ///
160 /// If successful, `block` may be merged with the block after it in order to
161 /// provide additional memory (when growing) or to merge released memory (when
162 /// shrinking). If unsuccessful, `block` will be unmodified.
163 ///
164 /// Note: Resizing may modify the block following this one if it is free.
165 /// Allocators that track free blocks based on their size must be prepared to
166 /// handle this size change.
167 ///
168 /// @pre The block must be in use.
169 ///
170 /// @returns @rst
171 ///
172 /// .. pw-status-codes::
173 ///
174 /// OK: The resize completed successfully.
175 ///
176 /// FAILED_PRECONDITION: This block is not in use.
177 ///
178 /// RESOURCE_EXHAUSTED: The available space is insufficient to fulfill the
179 /// request. This may be due to a large requested size, or insufficient
180 /// remaining space to fulfill the requested alignment create a valid
181 /// leading block, and/or create a valid trailing block.
182 ///
183 /// @endrst
184 BlockResult<Derived> Resize(size_t new_inner_size);
185
186 /// Marks the block as free.
187 ///
188 /// This method is static in order to consume the given block pointer. It
189 /// returns a pointer to a freed block that is the result of merging the given
190 /// block with either or both of its neighbors, if they were free.
191 ///
192 /// Note: Freeing may modify the adjacent blocks if they are free.
193 /// Allocators that track free blocks must be prepared to handle this merge.
194 static BlockResult<Derived> Free(Derived*&& block);
195
196 protected:
197 /// @copydoc CanAlloc
198 StatusWithSize DoCanAlloc(Layout layout) const;
199
200 /// @copydoc AllocFirst
201 static BlockResult<Derived> DoAllocFirst(Derived*&& block, Layout layout);
202
203 /// @copydoc AllocLast
204 static BlockResult<Derived> DoAllocLast(Derived*&& block, Layout layout);
205
206 /// @copydoc Resize
207 BlockResult<Derived> DoResize(size_t new_inner_size, bool shifted = false);
208
209 /// @copydoc Free
210 static BlockResult<Derived> DoFree(Derived*&& block);
211
212 private:
213 using BlockResultPrev = internal::GenericBlockResult::Prev;
214 using BlockResultNext = internal::GenericBlockResult::Next;
215
derived()216 constexpr Derived* derived() { return static_cast<Derived*>(this); }
derived()217 constexpr const Derived* derived() const {
218 return static_cast<const Derived*>(this);
219 }
220
221 // AlignableBlock calls DoCanAlloc, DoAllocLast, DoResize
222 template <typename>
223 friend class AlignableBlock;
224
225 // BlockWithLayout calls DoFree, DoResize
226 template <typename>
227 friend class BlockWithLayout;
228 };
229
230 /// Trait type that allows interrogating a block as to whether it is
231 /// allocatable.
232 template <typename BlockType>
233 struct is_allocatable : std::is_base_of<internal::AllocatableBase, BlockType> {
234 };
235
236 /// Helper variable template for `is_allocatable<BlockType>::value`.
237 template <typename BlockType>
238 inline constexpr bool is_allocatable_v = is_allocatable<BlockType>::value;
239
240 // Template method implementations.
241
242 template <typename Derived>
IsFree()243 bool AllocatableBlock<Derived>::IsFree() const {
244 derived()->CheckInvariantsIfStrict();
245 return derived()->IsFreeUnchecked();
246 }
247
248 template <typename Derived>
CanAlloc(Layout layout)249 StatusWithSize AllocatableBlock<Derived>::CanAlloc(Layout layout) const {
250 derived()->CheckInvariantsIfStrict();
251 return derived()->DoCanAlloc(layout);
252 }
253
254 template <typename Derived>
DoCanAlloc(Layout layout)255 StatusWithSize AllocatableBlock<Derived>::DoCanAlloc(Layout layout) const {
256 if (!derived()->IsFree()) {
257 return StatusWithSize::FailedPrecondition();
258 }
259 if (layout.size() == 0) {
260 return StatusWithSize::InvalidArgument();
261 }
262 size_t extra;
263 size_t new_inner_size = AlignUp(layout.size(), Derived::kAlignment);
264 if (PW_SUB_OVERFLOW(derived()->InnerSize(), new_inner_size, &extra)) {
265 return StatusWithSize::ResourceExhausted();
266 }
267 return StatusWithSize(extra);
268 }
269
270 template <typename Derived>
AllocFirst(Derived * && block,Layout layout)271 BlockResult<Derived> AllocatableBlock<Derived>::AllocFirst(Derived*&& block,
272 Layout layout) {
273 if (block == nullptr || layout.size() == 0) {
274 return BlockResult(block, Status::InvalidArgument());
275 }
276 block->CheckInvariants(/* crash_on_failure: */ true);
277 if (!block->IsFree()) {
278 return BlockResult(block, Status::FailedPrecondition());
279 }
280 return Derived::DoAllocFirst(std::move(block), layout);
281 }
282
283 template <typename Derived>
DoAllocFirst(Derived * && block,Layout layout)284 BlockResult<Derived> AllocatableBlock<Derived>::DoAllocFirst(Derived*&& block,
285 Layout layout) {
286 size_t size = AlignUp(layout.size(), Derived::kAlignment);
287 layout = Layout(size, layout.alignment());
288 StatusWithSize can_alloc = block->DoCanAlloc(layout);
289 if (!can_alloc.ok()) {
290 return BlockResult(block, can_alloc.status());
291 }
292 size_t extra = can_alloc.size();
293 BlockResult result(block);
294 if (extra >= Derived::kMinOuterSize) {
295 // Split the large padding off the back.
296 block->DoSplitFirst(block->InnerSize() - extra);
297 result = BlockResult(block, BlockResultNext::kSplitNew);
298 }
299 block->SetFree(false);
300 return result;
301 }
302
303 template <typename Derived>
AllocLast(Derived * && block,Layout layout)304 BlockResult<Derived> AllocatableBlock<Derived>::AllocLast(Derived*&& block,
305 Layout layout) {
306 if (block == nullptr || layout.size() == 0) {
307 return BlockResult(block, Status::InvalidArgument());
308 }
309 block->CheckInvariants(/* crash_on_failure: */ true);
310 if (!block->IsFree()) {
311 return BlockResult(block, Status::FailedPrecondition());
312 }
313 return Derived::DoAllocLast(std::move(block), layout);
314 }
315
316 template <typename Derived>
DoAllocLast(Derived * && block,Layout layout)317 BlockResult<Derived> AllocatableBlock<Derived>::DoAllocLast(Derived*&& block,
318 Layout layout) {
319 size_t size = AlignUp(layout.size(), Derived::kAlignment);
320 layout = Layout(size, layout.alignment());
321 StatusWithSize can_alloc = block->DoCanAlloc(layout);
322 if (!can_alloc.ok()) {
323 return BlockResult(block, can_alloc.status());
324 }
325 size_t extra = can_alloc.size();
326 BlockResult result(block);
327 Derived* prev = block->Prev();
328 if (extra >= Derived::kMinOuterSize) {
329 // Split the large padding off the front.
330 block = block->DoSplitLast(layout.size());
331 result = BlockResult(block, BlockResultPrev::kSplitNew);
332
333 } else if (extra != 0 && prev != nullptr) {
334 // The small amount of padding can be appended to the previous block.
335 prev->DoResize(prev->InnerSize() + extra, true).IgnoreUnlessStrict();
336 block = prev->Next();
337 result = BlockResult(block, BlockResultPrev::kResizedLarger, extra);
338 }
339 block->SetFree(false);
340 return result;
341 }
342
343 template <typename Derived>
Resize(size_t new_inner_size)344 BlockResult<Derived> AllocatableBlock<Derived>::Resize(size_t new_inner_size) {
345 derived()->CheckInvariants(/* crash_on_failure: */ true);
346 if (derived()->IsFree()) {
347 return BlockResult(derived(), Status::FailedPrecondition());
348 }
349 return derived()->DoResize(new_inner_size, /* shifted: */ false);
350 }
351
352 template <typename Derived>
DoResize(size_t new_inner_size,bool)353 BlockResult<Derived> AllocatableBlock<Derived>::DoResize(size_t new_inner_size,
354 bool) {
355 size_t old_inner_size = derived()->InnerSize();
356 new_inner_size = AlignUp(new_inner_size, Derived::kAlignment);
357 if (old_inner_size == new_inner_size) {
358 return BlockResult(derived());
359 }
360
361 // Treat the block as free and try to combine it with the next block. At most
362 // one free block is expected to follow this block.
363 derived()->SetFree(true);
364 Derived* next = derived()->Next();
365 BlockResult result(derived());
366 if (next != nullptr && next->IsFree()) {
367 derived()->DoMergeNext();
368 result = BlockResult(derived(), BlockResultNext::kMerged);
369 }
370 size_t merged_inner_size = derived()->InnerSize();
371 if (merged_inner_size < new_inner_size) {
372 // The merged block is too small for the resized block. Restore the original
373 // blocks as needed.
374 if (merged_inner_size != old_inner_size) {
375 derived()->DoSplitFirst(old_inner_size);
376 }
377 derived()->SetFree(false);
378 return BlockResult(derived(), Status::ResourceExhausted());
379 }
380 if (new_inner_size + Derived::kMinOuterSize <= merged_inner_size) {
381 // There is enough room after the resized block for another trailing block.
382 derived()->DoSplitFirst(new_inner_size);
383 result = result.next() == BlockResultNext::kMerged
384 ? BlockResult(derived(), BlockResultNext::kResized)
385 : BlockResult(derived(), BlockResultNext::kSplitNew);
386 }
387 derived()->SetFree(false);
388 return result;
389 }
390
391 template <typename Derived>
Free(Derived * && block)392 BlockResult<Derived> AllocatableBlock<Derived>::Free(Derived*&& block) {
393 if (block == nullptr) {
394 return BlockResult(block, Status::InvalidArgument());
395 }
396 block->CheckInvariants(/* crash_on_failure: */ true);
397 return Derived::DoFree(std::move(block));
398 }
399
400 template <typename Derived>
DoFree(Derived * && block)401 BlockResult<Derived> AllocatableBlock<Derived>::DoFree(Derived*&& block) {
402 block->SetFree(true);
403 BlockResult result(block);
404
405 // Try to merge the previous block with this one.
406 Derived* prev = block->Prev();
407 if (prev != nullptr && prev->IsFree()) {
408 prev->DoMergeNext();
409 block = prev;
410 result = BlockResult(block, BlockResultNext::kMerged);
411 }
412
413 // Try to merge this block with the next one.
414 Derived* next = block->Next();
415 if (next != nullptr && next->IsFree()) {
416 block->DoMergeNext();
417 result = BlockResult(block, BlockResultNext::kMerged);
418 }
419
420 block->CheckInvariantsIfStrict();
421 return result;
422 }
423
424 } // namespace pw::allocator
425