On Sat, Jul 11, 2026 at 10:34:54PM +0000, Alexander Graf wrote: > We already have two ways of creating a QOM object with a parent set > from birth: object_initialize_child() for objects embedded in the > parent struct, and object_new_with_props() for heap-allocated objects > that also want a list of string properties applied. What we do not > have is the plain heap-allocated dual of object_initialize_child(): > create the object, add it as a child<> of a given parent under a given > name, and hand the reference to the parent. > > Board and composite-device code that heap-allocates children today > therefore either open-codes object_new()+object_property_add_child() > +object_unref(), or skips the parenting step entirely and lets > device_set_realized() dump the object into /machine/unattached. > > Add object_new_child(parent, id, typename) as that missing primitive. > The reference created by object_new() is transferred to the parent's > child<> property, so on return the sole reference is held by @parent > and the caller does not need to unref. Later patches build the qdev > and per-bus creation helpers on top of this. > > Suggested-by: Markus Armbruster <[email protected]> > Link: https://lore.kernel.org/qemu-devel/[email protected]/ > Assisted-by: Kiro
QEMU does not currently permit contributions assisted by AI coding tools. There is an open discussion about altering the policy but that is not conclude with agreement yet. > Signed-off-by: Alexander Graf <[email protected]> > --- > include/qom/object.h | 25 +++++++++++++++++++++++++ > qom/object.c | 13 +++++++++++++ > 2 files changed, 38 insertions(+) > > diff --git a/include/qom/object.h b/include/qom/object.h > index 11f55613fc..ce6a23b5a1 100644 > --- a/include/qom/object.h > +++ b/include/qom/object.h > @@ -632,6 +632,31 @@ Object *object_new_with_class(ObjectClass *klass); > */ > Object *object_new(const char *typename); > > +/** > + * object_new_child: > + * @parent: the QOM composition-tree parent > + * @id: the child<> property name; the object's canonical QOM path > + * becomes @parent's path plus "/" plus @id > + * @typename: the name of the type of the object to instantiate > + * > + * Create a heap-allocated object and immediately add it to the QOM > + * composition tree as a child of @parent. > + * > + * This is the heap-allocated dual of object_initialize_child(): use > + * this when the child is a pointer member of the parent, and use > + * object_initialize_child() when the child is embedded in the parent > + * struct. See also object_new_with_props() for the variant that > + * additionally sets a list of properties. > + * > + * The reference returned by object_new() is transferred to the child<> > + * property of @parent, so the object has a reference count of 1 held > + * by @parent, and the caller does not need to unref. > + * > + * Returns: the newly allocated, instantiated and parented object. > + */ > +Object *object_new_child(Object *parent, const char *id, > + const char *typename); > + > /** > * object_new_with_props: > * @typename: The name of the type of the object to instantiate. > diff --git a/qom/object.c b/qom/object.c > index f79b2cf361..b4d181b617 100644 > --- a/qom/object.c > +++ b/qom/object.c > @@ -718,6 +718,19 @@ Object *object_new(const char *typename) > return object_new_with_type(ti); > } > > +Object *object_new_child(Object *parent, const char *id, > + const char *typename) > +{ > + Object *obj; > + > + g_assert(parent); > + g_assert(id); > + > + obj = object_new(typename); > + object_property_add_child(parent, id, obj); > + object_unref(obj); > + return obj; > +} > > Object *object_new_with_props(const char *typename, > Object *parent, > -- > 2.47.1 > With regards, Daniel -- |: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
