Move init implementation into hba and introduce volatile.

This commit is contained in:
Drew Galbraith 2025-01-31 18:03:04 -08:00
parent 34bd3b80d3
commit 741b605a1c
3 changed files with 86 additions and 30 deletions

View file

@ -2,6 +2,8 @@ use crate::cap::Capability;
use crate::syscall;
use crate::zion::ZError;
use alloc::slice;
use core::fmt::Debug;
use core::ptr::{addr_of, addr_of_mut};
#[cfg(feature = "hosted")]
use linked_list_allocator::LockedHeap;
@ -111,3 +113,42 @@ impl Drop for MemoryRegion {
syscall::address_space_unmap(self.virt_addr, max).expect("Failed to unmap memory");
}
}
pub struct Volatile<T> {
/// TODO: This should maybe be MaybeUninit.
data: T,
}
impl<T> Volatile<T> {
pub fn read(&self) -> T
where
T: Copy,
{
unsafe { addr_of!(self.data).cast::<T>().read_volatile() }
}
pub fn write(&mut self, data: T) {
unsafe {
addr_of_mut!(self.data).cast::<T>().write_volatile(data);
}
}
pub fn update<F>(&mut self, func: F)
where
T: Copy,
F: Fn(&mut T),
{
let mut data = self.read();
func(&mut data);
self.write(data);
}
}
impl<T> Debug for Volatile<T>
where
T: Debug + Copy,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:?}", self.read())
}
}