sel4_initialize_tls/
on_heap.rs

1//
2// Copyright 2024, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7use core::alloc::Layout;
8
9use crate::{Region, TlsImage};
10
11pub struct HeapTlsReservation {
12    start: *mut u8,
13    layout: Layout,
14    thread_pointer: usize,
15}
16
17impl HeapTlsReservation {
18    fn initialize(tls_image: &TlsImage) -> Self {
19        let layout = tls_image.reservation_layout().footprint();
20        let start = unsafe { ::alloc::alloc::alloc(layout) };
21        let region = Region::new(start, layout.size());
22        let thread_pointer =
23            unsafe { tls_image.initialize_exact_reservation_region(&region) }.unwrap();
24        Self {
25            start,
26            layout,
27            thread_pointer,
28        }
29    }
30
31    pub fn thread_pointer(&self) -> usize {
32        self.thread_pointer
33    }
34}
35
36impl Drop for HeapTlsReservation {
37    fn drop(&mut self) {
38        unsafe {
39            ::alloc::alloc::dealloc(self.start, self.layout);
40        }
41    }
42}
43
44impl TlsImage {
45    pub fn initialize_on_heap(&self) -> HeapTlsReservation {
46        HeapTlsReservation::initialize(self)
47    }
48}