rust: qom: add reference counting functionality

Add a smart pointer that allows to add and remove references from
QOM objects.  It's important to note that while all QOM objects have a
reference count, in practice not all of them have their lifetime guarded
by it.  Embedded objects, specifically, are confined to the lifetime of
the owner.

When writing Rust bindings this is important, because embedded objects are
*never* used through the "Owned<>" smart pointer that is introduced here.

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2025-01-17 12:00:01 +01:00
parent df45e26a81
commit 0fcccf3ff0
3 changed files with 178 additions and 7 deletions

View file

@ -15,7 +15,7 @@ use qemu_api::{
declare_properties, define_property,
prelude::*,
qdev::{DeviceClass, DeviceImpl, DeviceState, Property},
qom::{ClassInitImpl, ObjectImpl, ParentField},
qom::{ClassInitImpl, ObjectImpl, Owned, ParentField},
vmstate::VMStateDescription,
zeroable::Zeroable,
};
@ -138,6 +138,17 @@ fn test_object_new() {
}
}
#[test]
#[allow(clippy::redundant_clone)]
/// Create, clone and then drop an instance.
fn test_clone() {
init_qom();
let p: *mut DummyState = unsafe { object_new(DummyState::TYPE_NAME.as_ptr()).cast() };
let p = unsafe { Owned::from_raw(p) };
assert_eq!(p.clone().typename(), "dummy");
drop(p);
}
#[test]
/// Try invoking a method on an object.
fn test_typename() {