sel4_initialize_tls/
on_stack.rs

1//
2// Copyright 2024, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7use sel4_alloca::with_alloca_ptr;
8
9use crate::{SetThreadPointerFn, TlsImage};
10
11impl TlsImage {
12    #[allow(clippy::missing_safety_doc)]
13    pub unsafe fn with_initialize_on_stack<R>(
14        &self,
15        set_thread_pointer_fn: SetThreadPointerFn,
16        f: impl FnOnce() -> R,
17    ) -> R {
18        with_alloca_ptr(
19            self.reservation_layout().footprint(),
20            |tls_reservation_start| {
21                self.initialize_reservation(tls_reservation_start);
22                let thread_pointer = tls_reservation_start
23                    .wrapping_byte_add(self.reservation_layout().thread_pointer_offset());
24                (set_thread_pointer_fn)(thread_pointer as usize);
25                f()
26            },
27        )
28    }
29}