Pigweed
 
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(block, Status::InvalidArgument());
286 }
287 if constexpr (Hardening::kIncludesRobustChecks) {
288 block->CheckInvariants();
289 }
290 if (!block->IsFree()) {
291 return BlockResult(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(block, can_alloc.status());
304 }
305 size_t extra = can_alloc.size();
306 BlockResult result(block);
307 if (extra >= Derived::kMinOuterSize) {
308 // Split the large padding off the back.
309 block->DoSplitFirst(block->InnerSize() - extra);
310 result = BlockResult(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(block, Status::InvalidArgument());
321 }
322 if constexpr (Hardening::kIncludesRobustChecks) {
323 block->CheckInvariants();
324 }
325 if (!block->IsFree()) {
326 return BlockResult(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(block, can_alloc.status());
339 }
340 size_t extra = can_alloc.size();
341 BlockResult 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(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 = BlockResult(block, BlockResultPrev::kResizedLarger, extra);
354 }
355 block->SetFree(false);
356 return result;
357}
358
359template <typename Derived>
361 size_t new_inner_size) {
362 if constexpr (Hardening::kIncludesRobustChecks) {
363 derived()->CheckInvariants();
364 }
365 if (derived()->IsFree()) {
366 return BlockResult(derived(), Status::FailedPrecondition());
367 }
368 return derived()->DoResize(new_inner_size, /* shifted: */ false);
369}
370
371template <typename Derived>
373 size_t new_inner_size, bool) {
374 size_t old_inner_size = derived()->InnerSize();
375 new_inner_size = AlignUp(new_inner_size, Derived::kAlignment);
376 if (old_inner_size == new_inner_size) {
377 return BlockResult(derived());
378 }
379
380 // Treat the block as free and try to combine it with the next block. At most
381 // one free block is expected to follow this block.
382 derived()->SetFree(true);
383 Derived* next = derived()->Next();
384 BlockResult result(derived());
385 if (next != nullptr && next->IsFree()) {
386 derived()->DoMergeNext();
387 result = BlockResult(derived(), BlockResultNext::kMerged);
388 }
389 size_t merged_inner_size = derived()->InnerSize();
390 if (merged_inner_size < new_inner_size) {
391 // The merged block is too small for the resized block. Restore the original
392 // blocks as needed.
393 if (merged_inner_size != old_inner_size) {
394 derived()->DoSplitFirst(old_inner_size);
395 }
396 derived()->SetFree(false);
397 return BlockResult(derived(), Status::ResourceExhausted());
398 }
399 if (new_inner_size + Derived::kMinOuterSize <= merged_inner_size) {
400 // There is enough room after the resized block for another trailing block.
401 derived()->DoSplitFirst(new_inner_size);
402 result = result.next() == BlockResultNext::kMerged
403 ? BlockResult(derived(), BlockResultNext::kResized)
404 : BlockResult(derived(), BlockResultNext::kSplitNew);
405 }
406 derived()->SetFree(false);
407 return result;
408}
409
410template <typename Derived>
412 Derived*&& block) {
413 if (block == nullptr) {
414 return BlockResult(block, Status::InvalidArgument());
415 }
416 if constexpr (Hardening::kIncludesRobustChecks) {
417 block->CheckInvariants();
418 }
419 return Derived::DoFree(std::move(block));
420}
421
422template <typename Derived>
424 Derived*&& block) {
425 block->SetFree(true);
426 BlockResult result(block);
427
428 // Try to merge the previous block with this one.
429 Derived* prev = block->Prev();
430 if (prev != nullptr && prev->IsFree()) {
431 prev->DoMergeNext();
432 block = prev;
433 result = BlockResult(block, BlockResultNext::kMerged);
434 }
435
436 // Try to merge this block with the next one.
437 Derived* next = block->Next();
438 if (next != nullptr && next->IsFree()) {
439 block->DoMergeNext();
440 result = BlockResult(block, BlockResultNext::kMerged);
441 }
442
443 if constexpr (Hardening::kIncludesDebugChecks) {
444 block->CheckInvariants();
445 }
446 return result;
447}
448
449} // 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:360
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:423
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:372
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:411
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