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 {
49template <
typename Derived>
55 static_assert(is_alignable_v<Derived>,
56 "Types derived from BlockWithLayout must also derive from "
78 bool shifted =
false);
84 using BlockResultPrev = internal::GenericBlockResult::Prev;
86 constexpr Derived* derived() {
return static_cast<Derived*
>(
this); }
87 constexpr const Derived* derived()
const {
88 return static_cast<const Derived*
>(
this);
94template <
typename BlockType>
95struct has_layout : std::is_base_of<internal::BaseWithLayout, BlockType> {};
98template <
typename BlockType>
105template <
typename Derived>
107 if constexpr (Hardening::kIncludesDebugChecks) {
108 derived()->CheckInvariants();
110 if constexpr (Hardening::kIncludesRobustChecks) {
111 PW_ASSERT(!derived()->IsFree());
113 return Layout(derived()->RequestedSize(), derived()->RequestedAlignment());
116template <
typename Derived>
118 Derived*&& block,
Layout layout) {
123 block = result.block();
124 block->SetRequestedSize(layout.size());
125 block->SetRequestedAlignment(layout.alignment());
129template <
typename Derived>
131 Derived*&& block,
Layout layout) {
136 block = result.block();
137 block->SetRequestedSize(layout.size());
138 block->SetRequestedAlignment(layout.alignment());
142template <
typename Derived>
144 size_t new_inner_size,
bool shifted) {
145 size_t old_size = derived()->RequestedSize();
147 derived()->AllocatableBlock<Derived>::DoResize(new_inner_size, shifted);
148 if (result.ok() && !shifted) {
149 derived()->SetRequestedSize(new_inner_size);
151 derived()->SetRequestedSize(old_size);
156template <
typename Derived>
163 block = result.block();
164 Derived* prev = block->Prev();
165 if (prev ==
nullptr) {
168 size_t prev_size = prev->RequestedSize();
169 if (prev->InnerSize() - prev_size < Derived::kAlignment) {
173 size_t old_prev_size = prev->OuterSize();
174 prev->DoResize(prev_size,
true).IgnoreUnlessStrict();
176 BlockResultPrev::kResizedSmaller,
177 old_prev_size - prev->OuterSize());
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 > DoFree(Derived *&&block)
Definition: allocatable.h:428
Definition: with_layout.h:50
constexpr BlockResult< Derived > DoResize(size_t new_inner_size, bool shifted=false)
Definition: with_layout.h:143
constexpr Layout RequestedLayout() const
Definition: with_layout.h:106
static constexpr BlockResult< Derived > DoFree(Derived *&&block)
Definition: with_layout.h:157
static constexpr BlockResult< Derived > DoAllocFirst(Derived *&&block, Layout layout)
Definition: with_layout.h:117
static constexpr BlockResult< Derived > DoAllocLast(Derived *&&block, Layout layout)
Definition: with_layout.h:130
constexpr bool has_layout_v
Helper variable template for has_layout<BlockType>::value.
Definition: with_layout.h:99
Definition: with_layout.h:95
Definition: with_layout.h:27