On 2/5/25 10:13, Zhao Liu wrote:
* The use of from():
let clk = bindings::qdev_init_clock_in(...)
Owned::from(&*clk)
In this case the C side wants to manage the reference that
qdev_init_clock_in() returns; it is dropped in
qdev_finalize_clocklist(). So Rust code needs to increase the
refcount.
Pls forgive me for one more question about qdev_init_clock_in() on the C
side. :-)
qdev_init_clock_in() didn't unref `clk` after object_property_add_child(),
so it is intentional, to make the ref count of `clk` be 2:
* 1 count is held by clocklist until qdev_finalize_clocklist().
* another 1 is held by its parent via QOM Child<>.
Am I understanding it correctly?
Yes, that's more precise. In Rust it will be 3, the two above plus the
Owned<Clock>.
Ah wait... qdev_finalize_clocklist() is only called _after_ the Rust
struct is Drop::drop-ped, because device_finalize() is called after the
subclass's instance_finalize.
So the result of qdev_init_clock_in(), strictly speaking, does not have
to be an Owned<Clock>. It can also be a &'device Clock; either is
possible. Would you prefer that, or do you think it's enough to add a
comment?
Paolo
Then the comment "the clock is heap allocated and does not have
a reference" sounds like a conflict. I'm sure I'm missing something. :-(
Changed:
// SAFETY: the clock is heap allocated, but qdev_init_clock_in()
// does not gift the reference to its caller; so use Owned::from to
// add one. the callback is disabled automatically when the clock
// is unparented, which happens before the device is finalized.
LGTM.
Thank you very much for your patience. I think I understand ref count
now.
Regards,
Zhao