use core::cell::UnsafeCell; use core::mem::MaybeUninit; use core::sync::atomic::{AtomicBool, Ordering}; pub struct KernelCell { initialized: AtomicBool, inner: UnsafeCell>, } // Access is serialized by the kernel lock. unsafe impl Sync for KernelCell {} impl KernelCell { /// Holds `value` from the start: built at compile time, so a large `T` /// never lives on the stack. pub const fn new(value: T) -> Self { Self { initialized: AtomicBool::new(true), inner: UnsafeCell::new(MaybeUninit::new(value)), } } /// Raw pointer to the kernel; callers must avoid aliasing `&mut`. pub fn as_ptr(&self) -> *mut T { debug_assert!(self.initialized.load(Ordering::Acquire)); unsafe { (*self.inner.get()).as_mut_ptr() } } }