sel4_root_task/
heap.rs

1//
2// Copyright 2023, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7use sel4_immediate_sync_once_cell::ImmediateSyncOnceCell;
8use sel4_panicking_env::abort;
9
10static GLOBAL_ALLOCATOR_MUTEX_NOTIFICATION: ImmediateSyncOnceCell<sel4::cap::Notification> =
11    ImmediateSyncOnceCell::new();
12
13/// Provides the global allocator with a [`sel4::cap::Notification`] to use as a mutex..
14///
15/// Until this function is used, contention in the global allocator will result in a panic. This is
16/// only useful for multi-threaded root tasks.
17pub fn set_global_allocator_mutex_notification(nfn: sel4::cap::Notification) {
18    GLOBAL_ALLOCATOR_MUTEX_NOTIFICATION
19        .set(nfn)
20        .unwrap_or_else(|_| abort!("global allocator mutex notification already initialized"))
21}
22
23#[doc(hidden)]
24pub fn get_global_allocator_mutex_notification() -> sel4::cap::Notification {
25    *GLOBAL_ALLOCATOR_MUTEX_NOTIFICATION
26        .get()
27        .unwrap_or_else(|| {
28            abort!("global allocator contention before mutex notification initialization")
29        })
30}
31
32#[doc(hidden)]
33#[macro_export]
34macro_rules! declare_heap {
35    ($size:expr) => {
36        const _: () = {
37            mod _scope {
38                mod size_scope {
39                    use super::super::*;
40                    pub(super) const SIZE: usize = $size;
41                }
42
43                use $crate::_private::heap::*;
44
45                static STATIC_HEAP: StaticHeap<{ size_scope::SIZE }> = StaticHeap::new();
46
47                #[global_allocator]
48                static GLOBAL_ALLOCATOR: StaticDlmalloc<RawLazyNotificationMutex> =
49                    StaticDlmalloc::new_with_raw_mutex(
50                        RawLazyNotificationMutex::new(get_global_allocator_mutex_notification),
51                        STATIC_HEAP.bounds(),
52                    );
53            }
54        };
55    };
56}
57
58pub mod _private {
59    pub use sel4_dlmalloc::{StaticDlmalloc, StaticHeap};
60    pub use sel4_sync::RawLazyNotificationMutex;
61
62    pub use super::get_global_allocator_mutex_notification;
63}