Skip to content

Commit 85248d9

Browse files
committed
feat: 重构 VDeviceList 的 add_device 方法,支持通过闭包构建设备
1 parent 88900b9 commit 85248d9

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/arch/aarch64/plat.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ impl PlatData {
3535
let gicr = regs.get(1).ok_or(anyhow!("No GICR reg"))?;
3636
let plat = self.vdev.new_plat();
3737

38-
let gic = v3::VGic::new(
39-
(gicd.address as usize).into(),
40-
(gicr.address as usize).into(),
41-
plat.clone(),
42-
);
43-
44-
self.vdev.add_device(&plat, gic);
38+
self.vdev.add_device(|plat| {
39+
let gic = v3::VGic::new(
40+
(gicd.address as usize).into(),
41+
(gicr.address as usize).into(),
42+
plat.clone(),
43+
);
44+
Ok(gic)
45+
});
4546

4647
Ok(())
4748
}

src/vm/vdev.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct VDevice {
1313
}
1414

1515
impl VDevice {
16-
pub fn new(id: u32, raw: impl VirtDeviceOp + 'static) -> Self {
16+
pub fn new(id: u32, raw: impl VirtDeviceOp) -> Self {
1717
Self {
1818
id,
1919
raw: Arc::new(Mutex::new(Box::new(raw))),
@@ -67,12 +67,19 @@ impl VDeviceList {
6767
inner.deivces.get(&id).cloned()
6868
}
6969

70-
pub fn add_device(&self, plat: &VDevPlat, device: impl VirtDeviceOp + 'static) -> u32 {
70+
pub fn add_device<F, D>(&self, builder: F) -> anyhow::Result<()>
71+
where
72+
D: VirtDeviceOp,
73+
F: FnOnce(&VDevPlat) -> anyhow::Result<D>,
74+
{
75+
let plat = self.new_plat();
7176
let id = plat.id;
77+
let device = builder(&plat)?;
7278
let mut inner = self.inner.write();
7379
let vdev = VDevice::new(id, device);
7480
inner.deivces.insert(id, vdev);
75-
id
81+
82+
Ok(())
7683
}
7784

7885
pub fn handle_mmio_read(&self, addr: GuestPhysAddr, width: AccessWidth) -> Option<usize> {

0 commit comments

Comments
 (0)