Pigweed
 
Loading...
Searching...
No Matches
stack.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 <array>
17#include <cstddef>
18#include <type_traits>
19
20#include "pw_span/span.h"
21#include "pw_thread_backend/stack.h"
22
23namespace pw {
24namespace internal {
25
26using ThreadStackPointer =
27 decltype(std::declval<thread::backend::Stack<0>>().data());
28
29// Returns a span of the thread stack, with std::byte used for void* stacks.
30template <typename T>
31constexpr auto ThreadStackSpan(T* pointer, size_t size) {
32 if constexpr (std::is_void_v<T>) {
33 return span(static_cast<std::byte*>(pointer), size);
34 } else {
35 return span(pointer, size);
36 }
37}
38
39// Gets the stack size and handles void* stacks.
40constexpr auto NativeStackSizeBytes(size_t size) {
41 using T = std::remove_pointer_t<ThreadStackPointer>;
42 return size * sizeof(std::conditional_t<std::is_void_v<T>, std::byte, T>);
43}
44
45} // namespace internal
46
59template <size_t kStackSizeBytes>
61 public:
62 constexpr ThreadStack() : native_stack_{} {}
63
67 using native = internal::ThreadStackPointer;
68
72 [[nodiscard]] constexpr native native_pointer() {
73 return native_stack_.data();
74 }
75
80 [[nodiscard]] constexpr size_t native_size() const {
81 return native_stack_.size();
82 }
83
84 private:
85 static_assert(
86 std::is_same_v<decltype(thread::backend::Stack<kStackSizeBytes>().data()),
87 native>,
88 "Stack pointer type must not change with size");
89 static_assert(std::is_pointer_v<native>, ".data() must return a pointer");
90
91 thread::backend::Stack<kStackSizeBytes> native_stack_;
92};
93
94} // namespace pw
Definition: stack.h:60
constexpr native native_pointer()
Definition: stack.h:72
constexpr size_t native_size() const
Definition: stack.h:80
internal::ThreadStackPointer native
Definition: stack.h:67
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27