mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-07 01:33:56 -06:00
rust: qom: move bridge for TypeInfo functions out of pl011
Allow the ObjectImpl trait to expose Rust functions that avoid raw pointers (though INSTANCE_INIT for example is still unsafe). ObjectImpl::TYPE_INFO adds thunks around the functions in ObjectImpl. While at it, document `TypeInfo`. Reviewed-by: Zhao Liu <zhao1.liu@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
f75fb90ff2
commit
1f9d52c938
2 changed files with 69 additions and 32 deletions
|
@ -110,7 +110,7 @@ impl ObjectImpl for PL011State {
|
|||
type Class = PL011Class;
|
||||
const TYPE_NAME: &'static CStr = crate::TYPE_PL011;
|
||||
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_SYS_BUS_DEVICE);
|
||||
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = Some(pl011_init);
|
||||
const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
|
@ -615,19 +615,6 @@ pub unsafe extern "C" fn pl011_create(
|
|||
}
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
///
|
||||
/// We expect the FFI user of this function to pass a valid pointer, that has
|
||||
/// the same size as [`PL011State`]. We also expect the device is
|
||||
/// readable/writeable from one thread at any time.
|
||||
pub unsafe extern "C" fn pl011_init(obj: *mut Object) {
|
||||
unsafe {
|
||||
debug_assert!(!obj.is_null());
|
||||
let mut state = NonNull::new_unchecked(obj.cast::<PL011State>());
|
||||
state.as_mut().init();
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, qemu_api_macros::Object)]
|
||||
/// PL011 Luminary device model.
|
||||
|
@ -640,19 +627,16 @@ pub struct PL011LuminaryClass {
|
|||
_inner: [u8; 0],
|
||||
}
|
||||
|
||||
/// Initializes a pre-allocated, unitialized instance of `PL011Luminary`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// We expect the FFI user of this function to pass a valid pointer, that has
|
||||
/// the same size as [`PL011Luminary`]. We also expect the device is
|
||||
/// readable/writeable from one thread at any time.
|
||||
pub unsafe extern "C" fn pl011_luminary_init(obj: *mut Object) {
|
||||
unsafe {
|
||||
debug_assert!(!obj.is_null());
|
||||
let mut state = NonNull::new_unchecked(obj.cast::<PL011Luminary>());
|
||||
let state = state.as_mut();
|
||||
state.parent_obj.device_id = DeviceId::Luminary;
|
||||
impl PL011Luminary {
|
||||
/// Initializes a pre-allocated, unitialized instance of `PL011Luminary`.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// We expect the FFI user of this function to pass a valid pointer, that
|
||||
/// has the same size as [`PL011Luminary`]. We also expect the device is
|
||||
/// readable/writeable from one thread at any time.
|
||||
unsafe fn init(&mut self) {
|
||||
self.parent_obj.device_id = DeviceId::Luminary;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -660,7 +644,7 @@ impl ObjectImpl for PL011Luminary {
|
|||
type Class = PL011LuminaryClass;
|
||||
const TYPE_NAME: &'static CStr = crate::TYPE_PL011_LUMINARY;
|
||||
const PARENT_TYPE_NAME: Option<&'static CStr> = Some(crate::TYPE_PL011);
|
||||
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = Some(pl011_luminary_init);
|
||||
const INSTANCE_INIT: Option<unsafe fn(&mut Self)> = Some(Self::init);
|
||||
}
|
||||
|
||||
impl DeviceImpl for PL011Luminary {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue