18#include "pw_allocator/block/alignable.h"
19#include "pw_allocator/block/result.h"
20#include "pw_allocator/layout.h"
21#include "pw_assert/assert.h"
23namespace pw::allocator {
47template <
typename Derived>
53 static_assert(is_alignable_v<Derived>,
54 "Types derived from BlockWithLayout must also derive from "
76 bool shifted =
false);
82 using BlockResultPrev = internal::GenericBlockResult::Prev;
84 constexpr Derived* derived() {
return static_cast<Derived*
>(
this); }
85 constexpr const Derived* derived()
const {
86 return static_cast<const Derived*
>(
this);
92template <
typename BlockType>
93struct has_layout : std::is_base_of<internal::BaseWithLayout, BlockType> {};
96template <
typename BlockType>
101template <
typename Derived>
103 if constexpr (Hardening::kIncludesDebugChecks) {
104 derived()->CheckInvariants();
106 if constexpr (Hardening::kIncludesRobustChecks) {
107 PW_ASSERT(!derived()->IsFree());
109 return Layout(derived()->RequestedSize(), derived()->RequestedAlignment());
112template <
typename Derived>
114 Derived*&& block,
Layout layout) {
119 block = result.block();
120 block->SetRequestedSize(layout.size());
121 block->SetRequestedAlignment(layout.alignment());
125template <
typename Derived>
127 Derived*&& block,
Layout layout) {
132 block = result.block();
133 block->SetRequestedSize(layout.size());
134 block->SetRequestedAlignment(layout.alignment());
138template <
typename Derived>
140 size_t new_inner_size,
bool shifted) {
141 size_t old_size = derived()->RequestedSize();
143 derived()->AllocatableBlock<Derived>::DoResize(new_inner_size, shifted);
144 if (result.ok() && !shifted) {
145 derived()->SetRequestedSize(new_inner_size);
147 derived()->SetRequestedSize(old_size);
152template <
typename Derived>
159 block = result.block();
160 Derived* prev = block->Prev();
161 if (prev ==
nullptr) {
164 size_t prev_size = prev->RequestedSize();
165 if (prev->InnerSize() - prev_size < Derived::kAlignment) {
169 size_t old_prev_size = prev->OuterSize();
170 prev->DoResize(prev_size,
true).IgnoreUnlessStrict();
172 BlockResultPrev::kResizedSmaller,
173 old_prev_size - prev->OuterSize());
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: alignable.h:125
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: alignable.h:157
static constexpr BlockResult< Derived > DoFree(Derived *&&block)
Definition: allocatable.h:423
Definition: with_layout.h:48
constexpr BlockResult< Derived > DoResize(size_t new_inner_size, bool shifted=false)
Definition: with_layout.h:139
constexpr Layout RequestedLayout() const
Definition: with_layout.h:102
static constexpr BlockResult< Derived > DoFree(Derived *&&block)
Definition: with_layout.h:153
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: with_layout.h:113
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: with_layout.h:126
Definition: with_layout.h:93
Definition: with_layout.h:27