18#include "pw_allocator/hardening.h"
19#include "pw_result/result.h"
21namespace pw::allocator {
28constexpr bool is_unbounded_array_v =
false;
31constexpr bool is_unbounded_array_v<T[]> =
true;
34constexpr bool is_bounded_array_v =
false;
36template <
typename T,
size_t kN>
37constexpr bool is_bounded_array_v<T[kN]> =
true;
61 constexpr explicit Layout(
size_t size)
62 :
Layout(size,
alignof(std::max_align_t)) {}
63 constexpr Layout(
size_t size,
size_t alignment)
64 : size_(size), alignment_(alignment) {}
68 static constexpr std::enable_if_t<!std::is_array_v<T>,
Layout>
Of() {
69 return Layout(
sizeof(T),
alignof(T));
74 static constexpr std::enable_if_t<internal::is_bounded_array_v<T>,
Layout>
76 return Layout(
sizeof(T),
alignof(std::remove_extent_t<T>));
81 static constexpr std::enable_if_t<internal::is_unbounded_array_v<T>,
Layout>
83 using U = std::remove_extent_t<T>;
84 size_t size =
sizeof(U);
85 Hardening::Multiply(size, count);
86 return Layout(size,
alignof(U));
92 return result.ok() ? (*result) :
Layout();
95 constexpr Layout Extend(
size_t size)
const {
96 Hardening::Increment(size, size_);
97 return Layout(size, alignment_);
100 constexpr Layout Align(
size_t alignment)
const {
101 return Layout(size_, std::max(alignment, alignment_));
104 constexpr size_t size()
const {
return size_; }
105 constexpr size_t alignment()
const {
return alignment_; }
112inline bool operator==(
const Layout& lhs,
const Layout& rhs) {
113 return lhs.size() == rhs.size() && lhs.alignment() == rhs.alignment();
116inline bool operator!=(
const Layout& lhs,
const Layout& rhs) {
117 return !(lhs == rhs);
static constexpr Layout Unwrap(const Result< Layout > &result)
Definition: layout.h:91
static constexpr std::enable_if_t< internal::is_bounded_array_v< T >, Layout > Of()
Creates a Layout for the given bounded array type, e.g. Foo[kN].
Definition: layout.h:75
static constexpr std::enable_if_t< internal::is_unbounded_array_v< T >, Layout > Of(size_t count)
Creates a Layout for the given array type, e.g. Foo[].
Definition: layout.h:82
static constexpr std::enable_if_t<!std::is_array_v< T >, Layout > Of()
Creates a Layout for the given type.
Definition: layout.h:68