Pigweed
 
Loading...
Searching...
No Matches
layout.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/hardening.h"
19#include "pw_result/result.h"
20
21namespace pw::allocator {
22namespace internal {
23
24// Helper variables to determine when a template parameter is an array type.
25// Based on the sample implementation found at
26// https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique.
27template <typename>
28constexpr bool is_unbounded_array_v = false;
29
30template <typename T>
31constexpr bool is_unbounded_array_v<T[]> = true;
32
33template <typename>
34constexpr bool is_bounded_array_v = false;
35
36template <typename T, size_t kN>
37constexpr bool is_bounded_array_v<T[kN]> = true;
38
39} // namespace internal
40
56class Layout {
57 public:
58 constexpr Layout() : Layout(0) {}
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) {}
63
65 template <typename T>
66 static constexpr std::enable_if_t<!std::is_array_v<T>, Layout> Of() {
67 return Layout(sizeof(T), alignof(T));
68 }
69
71 template <typename T>
72 static constexpr std::enable_if_t<internal::is_bounded_array_v<T>, Layout>
73 Of() {
74 return Layout(sizeof(T), alignof(std::remove_extent_t<T>));
75 }
76
78 template <typename T>
79 static constexpr std::enable_if_t<internal::is_unbounded_array_v<T>, Layout>
80 Of(size_t count) {
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));
85 }
86
89 static constexpr Layout Unwrap(const Result<Layout>& result) {
90 return result.ok() ? (*result) : Layout();
91 }
92
93 constexpr Layout Extend(size_t size) const {
94 Hardening::Increment(size, size_);
95 return Layout(size, alignment_);
96 }
97
98 constexpr Layout Align(size_t alignment) const {
99 return Layout(size_, std::max(alignment, alignment_));
100 }
101
102 constexpr size_t size() const { return size_; }
103 constexpr size_t alignment() const { return alignment_; }
104
105 private:
106 size_t size_;
107 size_t alignment_;
108};
109
110inline bool operator==(const Layout& lhs, const Layout& rhs) {
111 return lhs.size() == rhs.size() && lhs.alignment() == rhs.alignment();
112}
113
114inline bool operator!=(const Layout& lhs, const Layout& rhs) {
115 return !(lhs == rhs);
116}
117
118} // namespace pw::allocator
Definition: layout.h:56
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