summary refs log tree commit diff stats
path: root/rust/qemu-api-macros/src
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2024-12-11 11:38:20 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2025-01-10 23:34:43 +0100
commitca0d60a6ad777ab617cbc4e6f328eaff60617b3f (patch)
treecf2734268009ecb6b4faeee812cd8e05aae9472c /rust/qemu-api-macros/src
parent1d03e9771e05685e11bbd3cc8cdd072c02cf580d (diff)
downloadfocaccia-qemu-ca0d60a6ad777ab617cbc4e6f328eaff60617b3f.tar.gz
focaccia-qemu-ca0d60a6ad777ab617cbc4e6f328eaff60617b3f.zip
rust: qom: add ParentField
Add a type that, together with the C function object_deinit, ensures the
correct drop order for QOM objects relative to their superclasses.

Right now it is not possible to implement the Drop trait for QOM classes
that are defined in Rust, as the drop() function would not be called when
the object goes away; instead what is called is ObjectImpl::INSTANCE_FINALIZE.
It would be nice for INSTANCE_FINALIZE to just drop the object, but this has
a problem: suppose you have

   pub struct MySuperclass {
       parent: DeviceState,
       field: Box<MyData>,
       ...
   }

   impl Drop for MySuperclass {
       ...
   }

   pub struct MySubclass {
       parent: MySuperclass,
       ...
   }

and an instance_finalize implementation that is like

    unsafe extern "C" fn drop_object<T: ObjectImpl>(obj: *mut Object) {
        unsafe { std::ptr::drop_in_place(obj.cast::<T>()) }
    }

When instance_finalize is called for MySubclass, it will walk the struct's
list of fields and call the drop method for MySuperclass.  Then, object_deinit
recurses to the superclass and calls the same drop method again.  This
will cause double-freeing of the Box<Data>.

What's happening here is that QOM wants to control the drop order of
MySuperclass and MySubclass's fields.  To do so, the parent field must
be marked ManuallyDrop<>, which is quite ugly.  Instead, add a wrapper
type ParentField<> that is specific to QOM.  This hides the implementation
detail of *what* is special about the ParentField, and will also be easy
to check in the #[derive(Object)] macro.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'rust/qemu-api-macros/src')
0 files changed, 0 insertions, 0 deletions