[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 13/26] rust: qom: automatically use Drop trait to implement i
From: |
Paolo Bonzini |
Subject: |
Re: [PATCH 13/26] rust: qom: automatically use Drop trait to implement instance_finalize |
Date: |
Wed, 11 Dec 2024 13:42:32 +0100 |
On Tue, Dec 10, 2024 at 4:58 PM Zhao Liu <zhao1.liu@intel.com> wrote:
> Great idea. It nicely balances the differences between Rust and C QOM
> conventions.
Except it does not work. :( Suppose you have
pub struct MySuperclass {
parent: DeviceState,
field: Box<MyData>,
...
}
impl Drop for MySuperclass {
...
}
pub struct MySubclass {
parent: MySuperclass,
...
}
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<MyData>, or
more in general double-dropping.
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. Perhaps we can add a
wrapper type ParentField<> that is specific to QOM. This hides the
implementation detail of *what* is special about the ParentField, and
it will also be easy to check for in the #[derive(Object)] macro.
Maybe in the future it will even make sense to have special functions
implemented on ParentField, I don't know...
Paolo
- [PATCH 08/26] rust: qom: rename Class trait to ClassInitImpl, (continued)
- [PATCH 08/26] rust: qom: rename Class trait to ClassInitImpl, Paolo Bonzini, 2024/12/09
- [PATCH 10/26] rust: qom: move ClassInitImpl to the instance side, Paolo Bonzini, 2024/12/09
- [PATCH 09/26] rust: qom: convert type_info! macro to an associated const, Paolo Bonzini, 2024/12/09
- [PATCH 11/26] rust: qdev: move device_class_init! body to generic function, ClassInitImpl implementation to macro, Paolo Bonzini, 2024/12/09
- [PATCH 13/26] rust: qom: automatically use Drop trait to implement instance_finalize, Paolo Bonzini, 2024/12/09
- [PATCH 16/26] rust: qom: change the parent type to an associated type, Paolo Bonzini, 2024/12/09
- [PATCH 17/26] rust: qom: put class_init together from multiple ClassInitImpl<>, Paolo Bonzini, 2024/12/09
- [PATCH 20/26] rust: re-export C types from qemu-api submodules, Paolo Bonzini, 2024/12/09
- [PATCH 14/26] rust: qom: move bridge for TypeInfo functions out of pl011, Paolo Bonzini, 2024/12/09