[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 05/10] rust: qom: automatically use Drop trait to implement insta
From: |
Paolo Bonzini |
Subject: |
[PATCH 05/10] rust: qom: automatically use Drop trait to implement instance_finalize |
Date: |
Thu, 19 Dec 2024 13:12:11 +0100 |
Replace the customizable INSTANCE_FINALIZE with a generic function
that drops the Rust object.
Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
rust/qemu-api/src/qom.rs | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/rust/qemu-api/src/qom.rs b/rust/qemu-api/src/qom.rs
index 1341a173893..861f1e50ac4 100644
--- a/rust/qemu-api/src/qom.rs
+++ b/rust/qemu-api/src/qom.rs
@@ -180,6 +180,16 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(),
fmt::Error> {
T::class_init(unsafe { &mut *klass.cast::<T::Class>() })
}
+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 the QOM core function object_deinit() as the instance_finalize
+ // function for class T. Note that while object_deinit() will drop the
+ // superclass field separately after this function returns, `T` must
+ // implement the unsafe trait ObjectType; the safety rules for the
+ // trait mandate that the parent field is manually dropped.
+ unsafe { std::ptr::drop_in_place(obj.cast::<T>()) }
+}
+
/// Trait exposed by all structs corresponding to QOM objects.
///
/// # Safety
@@ -438,7 +448,6 @@ pub trait ObjectImpl: ObjectType +
ClassInitImpl<Self::Class> {
/// Whether the object can be instantiated
const ABSTRACT: bool = false;
- const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> =
None;
/// Function that is called to initialize an object. The parent class will
/// have already been initialized so the type is only responsible for
@@ -474,7 +483,7 @@ pub trait ObjectImpl: ObjectType +
ClassInitImpl<Self::Class> {
None => None,
Some(_) => Some(rust_instance_post_init::<Self>),
},
- instance_finalize: Self::INSTANCE_FINALIZE,
+ instance_finalize: Some(drop_object::<Self>),
abstract_: Self::ABSTRACT,
class_size: core::mem::size_of::<Self::Class>(),
class_init: Some(rust_class_init::<Self>),
--
2.47.1
- [PATCH 00/10] Next round of qemu_api patches, Paolo Bonzini, 2024/12/19
- [PATCH 07/10] rust: pl011: only leave embedded object initialization in instance_init, Paolo Bonzini, 2024/12/19
- [PATCH 01/10] rust: qom: add ParentField, Paolo Bonzini, 2024/12/19
- [PATCH 03/10] rust: macros: check that the first field of a #[derive(Object)] struct is a ParentField, Paolo Bonzini, 2024/12/19
- [PATCH 06/10] rust: qom: move device_id to PL011 class side, Paolo Bonzini, 2024/12/19
- [PATCH 04/10] rust: macros: check that #[derive(Object)] requires #[repr(C)], Paolo Bonzini, 2024/12/19
- [PATCH 09/10] rust: qemu-api-macros: extend error reporting facility to parse errors, Paolo Bonzini, 2024/12/19
- [PATCH 05/10] rust: qom: automatically use Drop trait to implement instance_finalize,
Paolo Bonzini <=
- [PATCH 02/10] rust: add a utility module for compile-time type checks, Paolo Bonzini, 2024/12/19
- [PATCH 10/10] rust: qemu-api-macros: add automatic TryFrom/TryInto derivation, Paolo Bonzini, 2024/12/19
- [PATCH 08/10] rust: qom: make INSTANCE_POST_INIT take a shared reference, Paolo Bonzini, 2024/12/19