diff --git a/framework/jinux-frame/src/panicking.rs b/framework/jinux-frame/src/panicking.rs index b3f00b8b..57d8a066 100644 --- a/framework/jinux-frame/src/panicking.rs +++ b/framework/jinux-frame/src/panicking.rs @@ -53,7 +53,9 @@ fn print_stack_trace() { let fde_initial_address = _Unwind_FindEnclosingFunction(pc as *mut c_void) as usize; early_println!( "{:4}: fn {:#18x} - pc {:#18x} / registers:", - data.counter, fde_initial_address, pc, + data.counter, + fde_initial_address, + pc, ); // Print the first 8 general registers for any architecture. The register number follows // the DWARF standard. diff --git a/services/comps/block/src/lib.rs b/services/comps/block/src/lib.rs index 730387fc..c1410e17 100644 --- a/services/comps/block/src/lib.rs +++ b/services/comps/block/src/lib.rs @@ -26,20 +26,30 @@ pub trait BlockDevice: Send + Sync + Any + Debug { } pub fn register_device(name: String, device: Arc) { - COMPONENT.get().unwrap().devices.lock().insert(name, device); + COMPONENT + .get() + .unwrap() + .block_device_table + .lock() + .insert(name, device); } -pub fn get_device(str: &String) -> Option> { - COMPONENT.get().unwrap().devices.lock().get(str).cloned() +pub fn get_device(str: &str) -> Option> { + COMPONENT + .get() + .unwrap() + .block_device_table + .lock() + .get(str) + .cloned() } pub fn all_devices() -> Vec<(String, Arc)> { - let lock = COMPONENT.get().unwrap().devices.lock(); - let mut vec = Vec::new(); - for (name, device) in lock.iter() { - vec.push((name.clone(), device.clone())); - } - vec + let block_devs = COMPONENT.get().unwrap().block_device_table.lock(); + block_devs + .iter() + .map(|(name, device)| (name.clone(), device.clone())) + .collect() } static COMPONENT: Once = Once::new(); @@ -53,13 +63,13 @@ fn component_init() -> Result<(), ComponentInitError> { #[derive(Debug)] struct Component { - devices: SpinLock>>, + block_device_table: SpinLock>>, } impl Component { pub fn init() -> Result { Ok(Self { - devices: SpinLock::new(BTreeMap::new()), + block_device_table: SpinLock::new(BTreeMap::new()), }) } } diff --git a/services/comps/console/src/lib.rs b/services/comps/console/src/lib.rs index 111a0e86..169f93ce 100644 --- a/services/comps/console/src/lib.rs +++ b/services/comps/console/src/lib.rs @@ -25,7 +25,7 @@ pub fn register_device(name: String, device: Arc) { COMPONENT .get() .unwrap() - .console_table + .console_device_table .lock() .insert(name, device); } @@ -34,15 +34,15 @@ pub fn get_device(str: &str) -> Option> { COMPONENT .get() .unwrap() - .console_table + .console_device_table .lock() .get(str) .cloned() } pub fn all_devices() -> Vec<(String, Arc)> { - let consoles = COMPONENT.get().unwrap().console_table.lock(); - consoles + let console_devs = COMPONENT.get().unwrap().console_device_table.lock(); + console_devs .iter() .map(|(name, device)| (name.clone(), device.clone())) .collect() @@ -59,13 +59,13 @@ fn component_init() -> Result<(), ComponentInitError> { #[derive(Debug)] struct Component { - console_table: SpinLock>>, + console_device_table: SpinLock>>, } impl Component { pub fn init() -> Result { Ok(Self { - console_table: SpinLock::new(BTreeMap::new()), + console_device_table: SpinLock::new(BTreeMap::new()), }) } } diff --git a/services/comps/input/src/lib.rs b/services/comps/input/src/lib.rs index 896a3c5e..1644e88a 100644 --- a/services/comps/input/src/lib.rs +++ b/services/comps/input/src/lib.rs @@ -24,20 +24,30 @@ pub trait InputDevice: Send + Sync + Any + Debug { } pub fn register_device(name: String, device: Arc) { - COMPONENT.get().unwrap().devices.lock().insert(name, device); + COMPONENT + .get() + .unwrap() + .input_device_table + .lock() + .insert(name, device); } -pub fn get_device(str: &String) -> Option> { - COMPONENT.get().unwrap().devices.lock().get(str).cloned() +pub fn get_device(str: &str) -> Option> { + COMPONENT + .get() + .unwrap() + .input_device_table + .lock() + .get(str) + .cloned() } pub fn all_devices() -> Vec<(String, Arc)> { - let lock = COMPONENT.get().unwrap().devices.lock(); - let mut vec = Vec::new(); - for (name, device) in lock.iter() { - vec.push((name.clone(), device.clone())); - } - vec + let input_devs = COMPONENT.get().unwrap().input_device_table.lock(); + input_devs + .iter() + .map(|(name, device)| (name.clone(), device.clone())) + .collect() } static COMPONENT: Once = Once::new(); @@ -51,13 +61,13 @@ fn component_init() -> Result<(), ComponentInitError> { #[derive(Debug)] struct Component { - devices: SpinLock>>, + input_device_table: SpinLock>>, } impl Component { pub fn init() -> Result { Ok(Self { - devices: SpinLock::new(BTreeMap::new()), + input_device_table: SpinLock::new(BTreeMap::new()), }) } } diff --git a/services/comps/network/src/driver.rs b/services/comps/network/src/driver.rs index 7b8f0cbd..b2d2b9c4 100644 --- a/services/comps/network/src/driver.rs +++ b/services/comps/network/src/driver.rs @@ -3,10 +3,10 @@ use smoltcp::{phy, time::Instant}; use crate::{ buffer::{RxBuffer, TxBuffer}, - NetworkDevice, + AnyNetworkDevice, }; -impl phy::Device for dyn NetworkDevice { +impl phy::Device for dyn AnyNetworkDevice { type RxToken<'a> = RxToken; type TxToken<'a> = TxToken<'a>; @@ -43,7 +43,7 @@ impl phy::RxToken for RxToken { } } -pub struct TxToken<'a>(&'a mut dyn NetworkDevice); +pub struct TxToken<'a>(&'a mut dyn AnyNetworkDevice); impl<'a> phy::TxToken for TxToken<'a> { fn consume(self, len: usize, f: F) -> R diff --git a/services/comps/network/src/lib.rs b/services/comps/network/src/lib.rs index 03e11100..1614acb4 100644 --- a/services/comps/network/src/lib.rs +++ b/services/comps/network/src/lib.rs @@ -35,7 +35,7 @@ pub enum VirtioNetError { Unknown, } -pub trait NetworkDevice: Send + Sync + Any + Debug { +pub trait AnyNetworkDevice: Send + Sync + Any + Debug { // ================Device Information================= fn mac_addr(&self) -> EthernetAddr; @@ -54,33 +54,33 @@ pub trait NetworkDevice: Send + Sync + Any + Debug { pub trait NetDeviceIrqHandler = Fn() + Send + Sync + 'static; -pub fn register_device(name: String, device: Arc>>) { +pub fn register_device(name: String, device: Arc>>) { COMPONENT .get() .unwrap() - .devices + .network_device_table .lock() .insert(name, (Arc::new(SpinLock::new(Vec::new())), device)); } -pub fn get_device(str: &String) -> Option>>> { - let lock = COMPONENT.get().unwrap().devices.lock(); +pub fn get_device(str: &str) -> Option>>> { + let lock = COMPONENT.get().unwrap().network_device_table.lock(); let Some((_, device)) = lock.get(str) else { return None; }; Some(device.clone()) } -pub fn register_recv_callback(name: &String, callback: impl NetDeviceIrqHandler) { - let lock = COMPONENT.get().unwrap().devices.lock(); +pub fn register_recv_callback(name: &str, callback: impl NetDeviceIrqHandler) { + let lock = COMPONENT.get().unwrap().network_device_table.lock(); let Some((callbacks, _)) = lock.get(name) else { return; }; callbacks.lock().push(Arc::new(callback)); } -pub fn handle_recv_irq(name: &String) { - let lock = COMPONENT.get().unwrap().devices.lock(); +pub fn handle_recv_irq(name: &str) { + let lock = COMPONENT.get().unwrap().network_device_table.lock(); let Some((callbacks, _)) = lock.get(name) else { return; }; @@ -92,12 +92,11 @@ pub fn handle_recv_irq(name: &String) { } pub fn all_devices() -> Vec<(String, NetworkDeviceRef)> { - let lock = COMPONENT.get().unwrap().devices.lock(); - let mut vec = Vec::new(); - for (name, (_, device)) in lock.iter() { - vec.push((name.clone(), device.clone())); - } - vec + let network_devs = COMPONENT.get().unwrap().network_device_table.lock(); + network_devs + .iter() + .map(|(name, (_, device))| (name.clone(), device.clone())) + .collect() } static COMPONENT: Once = Once::new(); @@ -113,17 +112,18 @@ fn init() -> Result<(), ComponentInitError> { } type NetDeviceIrqHandlerListRef = Arc>>>; -type NetworkDeviceRef = Arc>>; +type NetworkDeviceRef = Arc>>; struct Component { /// Device list, the key is device name, value is (callbacks, device); - devices: SpinLock>, + network_device_table: + SpinLock>, } impl Component { pub fn init() -> Result { Ok(Self { - devices: SpinLock::new(BTreeMap::new()), + network_device_table: SpinLock::new(BTreeMap::new()), }) } } diff --git a/services/comps/virtio/src/device/block/device.rs b/services/comps/virtio/src/device/block/device.rs index 6da51e00..9b9773a9 100644 --- a/services/comps/virtio/src/device/block/device.rs +++ b/services/comps/virtio/src/device/block/device.rs @@ -95,7 +95,7 @@ impl BlockDevice { .unwrap(); fn handle_block_device(_: &TrapFrame) { - jinux_block::get_device(&(super::DEVICE_NAME.to_string())) + jinux_block::get_device(super::DEVICE_NAME) .unwrap() .handle_irq(); } diff --git a/services/comps/virtio/src/device/console/device.rs b/services/comps/virtio/src/device/console/device.rs index 29f9bee4..d37fe215 100644 --- a/services/comps/virtio/src/device/console/device.rs +++ b/services/comps/virtio/src/device/console/device.rs @@ -112,14 +112,14 @@ impl ConsoleDevice { callbacks: SpinLock::new(Vec::new()), }; - let mut recv_lock = device.receive_queue.lock(); - recv_lock + let mut receive_queue = device.receive_queue.lock(); + receive_queue .add(&[], &[device.buffer.lock().as_mut()]) .unwrap(); - if recv_lock.should_notify() { - recv_lock.notify(); + if receive_queue.should_notify() { + receive_queue.notify(); } - drop(recv_lock); + drop(receive_queue); device .transport .register_queue_callback(RECV0_QUEUE_INDEX, Box::new(handle_console_input), false) @@ -137,9 +137,7 @@ impl ConsoleDevice { } fn handle_console_input(_: &TrapFrame) { - jinux_console::get_device(&DEVICE_NAME.to_string()) - .unwrap() - .handle_irq(); + jinux_console::get_device(DEVICE_NAME).unwrap().handle_irq(); } fn config_space_change(_: &TrapFrame) { diff --git a/services/comps/virtio/src/device/input/device.rs b/services/comps/virtio/src/device/input/device.rs index 4132ae0b..70ecb2e5 100644 --- a/services/comps/virtio/src/device/input/device.rs +++ b/services/comps/virtio/src/device/input/device.rs @@ -111,7 +111,7 @@ impl InputDevice { fn handle_input(_: &TrapFrame) { debug!("Handle Virtio input interrupt"); - let device = jinux_input::get_device(&(super::DEVICE_NAME.to_string())).unwrap(); + let device = jinux_input::get_device(super::DEVICE_NAME).unwrap(); device.handle_irq().unwrap(); } diff --git a/services/comps/virtio/src/device/network/device.rs b/services/comps/virtio/src/device/network/device.rs index 14806a7e..b4b320c8 100644 --- a/services/comps/virtio/src/device/network/device.rs +++ b/services/comps/virtio/src/device/network/device.rs @@ -4,7 +4,7 @@ use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec}; use jinux_frame::{offset_of, sync::SpinLock, trap::TrapFrame}; use jinux_network::{ buffer::{RxBuffer, TxBuffer}, - EthernetAddr, NetDeviceIrqHandler, VirtioNetError, + AnyNetworkDevice, EthernetAddr, NetDeviceIrqHandler, VirtioNetError, }; use jinux_util::{field_ptr, slot_vec::SlotVec}; use log::debug; @@ -86,7 +86,7 @@ impl NetworkDevice { /// Interrupt handler if network device receives some packet fn handle_network_event(_: &TrapFrame) { - jinux_network::handle_recv_irq(&(super::DEVICE_NAME.to_string())); + jinux_network::handle_recv_irq(super::DEVICE_NAME); } device @@ -169,7 +169,7 @@ fn queue_to_network_error(err: QueueError) -> VirtioNetError { } } -impl jinux_network::NetworkDevice for NetworkDevice { +impl AnyNetworkDevice for NetworkDevice { fn mac_addr(&self) -> EthernetAddr { self.mac_addr } diff --git a/services/libs/comp-sys/component-macro/src/priority.rs b/services/libs/comp-sys/component-macro/src/priority.rs index 04f7243f..931f2719 100644 --- a/services/libs/comp-sys/component-macro/src/priority.rs +++ b/services/libs/comp-sys/component-macro/src/priority.rs @@ -119,7 +119,7 @@ fn is_component(package: &JsonValue) -> bool { } /// Get all the components name, this function will also check if the Components.toml contain all the components. -fn get_components_name(workspace_root: &String, packages: &JsonValue) -> Vec { +fn get_components_name(workspace_root: &str, packages: &JsonValue) -> Vec { let file_components_name = read_component_file(workspace_root); let mut comps_name = Vec::new(); for package in packages.members() { diff --git a/services/libs/jinux-std/src/net/iface/virtio.rs b/services/libs/jinux-std/src/net/iface/virtio.rs index e2239637..9b9c82e1 100644 --- a/services/libs/jinux-std/src/net/iface/virtio.rs +++ b/services/libs/jinux-std/src/net/iface/virtio.rs @@ -1,6 +1,6 @@ use crate::prelude::*; use jinux_frame::sync::SpinLock; -use jinux_network::NetworkDevice; +use jinux_network::AnyNetworkDevice; use jinux_virtio::device::network::DEVICE_NAME; use smoltcp::{ iface::{Config, Routes, SocketHandle, SocketSet}, @@ -11,7 +11,7 @@ use smoltcp::{ use super::{common::IfaceCommon, internal::IfaceInternal, Iface}; pub struct IfaceVirtio { - driver: Arc>>, + driver: Arc>>, common: IfaceCommon, dhcp_handle: SocketHandle, weak_self: Weak, @@ -19,7 +19,7 @@ pub struct IfaceVirtio { impl IfaceVirtio { pub fn new() -> Arc { - let virtio_net = jinux_network::get_device(&(DEVICE_NAME).to_string()).unwrap(); + let virtio_net = jinux_network::get_device(DEVICE_NAME).unwrap(); let interface = { let mac_addr = virtio_net.lock().mac_addr(); let ip_addr = IpCidr::new(wire::IpAddress::Ipv4(wire::Ipv4Address::UNSPECIFIED), 0);