use hashbrown::HashMap; use spin::Lazy; use crate::{ filesystem::{StorageDevice, FileDescriptor}, handle::{Handle, HandleResource}, }; pub static KERNEL_STATE: Lazy> = Lazy::new(|| spin::Mutex::new(KernelInternalState::new())); pub struct KernelInternalState { pub hostname: String, storage_devices: HashMap>, fd_table: HashMap, should_shutdown: bool, } impl KernelInternalState { pub fn new() -> Self { Self { should_shutdown: false, storage_devices: HashMap::new(), fd_table: HashMap::new(), hostname: "".to_string(), } } pub fn set_hostname(&mut self, hostname: String) { self.hostname = hostname; } pub fn add_storage_device(&mut self, device: impl StorageDevice + Send + 'static) -> Handle { let handle = Handle::new(HandleResource::StorageDevice); self.storage_devices.insert(handle, Box::new(device)); handle } pub fn get_storage_device(&self, handle: Handle) -> Option<&dyn StorageDevice> { self.storage_devices.get(&handle).map(|d| &**d) } pub fn add_file_descriptor(&mut self, fd: FileDescriptor) -> Handle { let handle = Handle::new(HandleResource::FileDescriptor); self.fd_table.insert(handle, fd); handle } pub fn shutdown(&mut self) { self.should_shutdown = true; } pub fn update_state(&mut self) { if self.should_shutdown { crate::arch::shutdown(); } } } impl Default for KernelInternalState { fn default() -> Self { Self::new() } }