@@ -8,6 +8,13 @@
use crate::bindings::{Object, ObjectClass, TypeInfo};
+unsafe extern "C" fn drop_object<T: ObjectImpl>(obj: *mut Object) {
+ // SAFETY: obj is an instance of T, since drop_object<T>
+ // is called from QOM core as the instance_finalize function
+ // for class T
+ unsafe { std::ptr::drop_in_place(obj.cast::<T>()) }
+}
+
/// Trait a type must implement to be registered with QEMU.
pub trait ObjectImpl: ClassInitImpl + Sized {
type Class;
@@ -16,7 +23,6 @@ pub trait ObjectImpl: ClassInitImpl + Sized {
const ABSTRACT: bool = false;
const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
- const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
const TYPE_INFO: TypeInfo = TypeInfo {
name: Self::TYPE_NAME.as_ptr(),
@@ -29,7 +35,7 @@ pub trait ObjectImpl: ClassInitImpl + Sized {
instance_align: core::mem::align_of::<Self>(),
instance_init: Self::INSTANCE_INIT,
instance_post_init: Self::INSTANCE_POST_INIT,
- instance_finalize: Self::INSTANCE_FINALIZE,
+ instance_finalize: Some(drop_object::<Self>),
abstract_: Self::ABSTRACT,
class_size: core::mem::size_of::<Self::Class>(),
class_init: <Self as ClassInitImpl>::CLASS_INIT,
Replace the customizable INSTANCE_FINALIZE with a generic function that drops the Rust object. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> --- rust/qemu-api/src/definitions.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)