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;
59 constexpr explicit Layout(
size_t size)
60 :
Layout(size,
alignof(std::max_align_t)) {}
61 constexpr Layout(
size_t size,
size_t alignment)
62 : size_(size), alignment_(alignment) {}
66 static constexpr std::enable_if_t<!std::is_array_v<T>,
Layout>
Of() {
67 return Layout(
sizeof(T),
alignof(T));
72 static constexpr std::enable_if_t<internal::is_bounded_array_v<T>,
Layout>
74 return Layout(
sizeof(T),
alignof(std::remove_extent_t<T>));
79 static constexpr std::enable_if_t<internal::is_unbounded_array_v<T>,
Layout>
81 using U = std::remove_extent_t<T>;
82 size_t size =
sizeof(U);
83 Hardening::Multiply(size, count);
84 return Layout(size,
alignof(U));
90 return result.ok() ? (*result) :
Layout();
93 constexpr Layout Extend(
size_t size)
const {
94 Hardening::Increment(size, size_);
95 return Layout(size, alignment_);
98 constexpr Layout Align(
size_t alignment)
const {
99 return Layout(size_, std::max(alignment, alignment_));
102 constexpr size_t size()
const {
return size_; }
103 constexpr size_t alignment()
const {
return alignment_; }
110inline bool operator==(
const Layout& lhs,
const Layout& rhs) {
111 return lhs.size() == rhs.size() && lhs.alignment() == rhs.alignment();
114inline bool operator!=(
const Layout& lhs,
const Layout& rhs) {
115 return !(lhs == rhs);
static constexpr Layout Unwrap(const Result< Layout > &result)
Definition: layout.h:89
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:73
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:80
static constexpr std::enable_if_t<!std::is_array_v< T >, Layout > Of()
Creates a Layout for the given type.
Definition: layout.h:66