feat(ida): IDA内部改为使用XArray实现 (#934)

目前可以记录哪些ID已经分配,支持了ID释放的功能.

Signed-off-by: longjin <longjin@DragonOS.org>
This commit is contained in:
LoGin
2024-09-25 11:20:52 +08:00
committed by GitHub
parent 9ad34ef277
commit 013ffb708f
20 changed files with 273 additions and 83 deletions

View File

@ -27,7 +27,8 @@ use system_error::SystemError;
use super::{super::device::DeviceState, platform_bus, platform_bus_device, CompatibleTable};
/// 平台设备id分配器
static PLATFORM_DEVID_IDA: IdAllocator = IdAllocator::new(0, i32::MAX as usize);
static PLATFORM_DEVID_IDA: SpinLock<IdAllocator> =
SpinLock::new(IdAllocator::new(0, i32::MAX as usize).unwrap());
#[inline(always)]
pub fn platform_device_manager() -> &'static PlatformDeviceManager {
@ -93,7 +94,10 @@ impl PlatformDeviceManager {
pdev.set_name(pdev.pdev_name().to_string());
}
PLATFORM_DEVID_AUTO => {
let id = PLATFORM_DEVID_IDA.alloc().ok_or(SystemError::EOVERFLOW)?;
let id = PLATFORM_DEVID_IDA
.lock()
.alloc()
.ok_or(SystemError::EOVERFLOW)?;
pdev.set_pdev_id(id as i32);
pdev.set_pdev_id_auto(true);
pdev.set_name(format!("{}.{}.auto", pdev.pdev_name(), pdev.pdev_id().0));
@ -112,7 +116,7 @@ impl PlatformDeviceManager {
// failed
let pdevid = pdev.pdev_id();
if pdevid.1 {
PLATFORM_DEVID_IDA.free(pdevid.0 as usize);
PLATFORM_DEVID_IDA.lock().free(pdevid.0 as usize);
pdev.set_pdev_id(PLATFORM_DEVID_AUTO);
}

View File

@ -34,7 +34,8 @@ use super::{
GeneralRtcPriority, RtcClassOps, RtcDevice,
};
static RTC_GENERAL_DEVICE_IDA: IdAllocator = IdAllocator::new(0, usize::MAX);
static RTC_GENERAL_DEVICE_IDA: SpinLock<IdAllocator> =
SpinLock::new(IdAllocator::new(0, usize::MAX).unwrap());
pub(super) const RTC_HCTOSYS_DEVICE: &str = "rtc0";
@ -63,7 +64,7 @@ impl RtcGeneralDevice {
///
/// 注意,由于还需要进行其他的初始化操作,因此这个函数并不是公开的构造函数。
fn new(priority: GeneralRtcPriority) -> Arc<Self> {
let id = RTC_GENERAL_DEVICE_IDA.alloc().unwrap();
let id = RTC_GENERAL_DEVICE_IDA.lock().alloc().unwrap();
let name = format!("rtc{}", id);
Arc::new(Self {
name,
@ -106,7 +107,7 @@ impl RtcGeneralDevice {
impl Drop for RtcGeneralDevice {
fn drop(&mut self) {
RTC_GENERAL_DEVICE_IDA.free(self.id);
RTC_GENERAL_DEVICE_IDA.lock().free(self.id);
}
}

View File

@ -30,6 +30,7 @@ use crate::{
vfs::syscall::ModeType,
},
init::initcall::INITCALL_CORE,
libs::spinlock::SpinLock,
};
use super::{VirtIODevice, VirtIODeviceIndex, VirtIODriver, VIRTIO_DEV_ANY_ID};
@ -255,7 +256,7 @@ pub struct VirtIODeviceIndexManager {
// ID分配器
///
/// ID分配器用于分配唯一的索引给VirtIO设备。
ida: IdAllocator,
ida: SpinLock<IdAllocator>,
}
// VirtIO设备索引管理器的新建实例
@ -265,7 +266,7 @@ impl VirtIODeviceIndexManager {
/// 创建一个新的VirtIO设备索引管理器实例初始时分配器从0开始直到最大usize值。
const fn new() -> Self {
Self {
ida: IdAllocator::new(0, usize::MAX),
ida: SpinLock::new(IdAllocator::new(0, usize::MAX).unwrap()),
}
}
@ -273,7 +274,7 @@ impl VirtIODeviceIndexManager {
///
/// 分配一个唯一的索引给VirtIO设备。
pub fn alloc(&self) -> VirtIODeviceIndex {
VirtIODeviceIndex(self.ida.alloc().unwrap())
VirtIODeviceIndex(self.ida.lock().alloc().unwrap())
}
// 释放一个VirtIO设备索引
@ -281,7 +282,7 @@ impl VirtIODeviceIndexManager {
/// 释放之前分配的VirtIO设备索引使其可以被重新使用。
#[allow(dead_code)]
pub fn free(&self, index: VirtIODeviceIndex) {
self.ida.free(index.0);
self.ida.lock().free(index.0);
}
}