On 13/7/26 18:49, Richard Henderson wrote:
On 7/11/26 15:34, 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
Signed-off-by: Alexander Graf <[email protected]>
---
include/qom/object.h | 25 +++++++++++++++++++++++++
qom/object.c | 13 +++++++++++++
2 files changed, 38 insertions(+)
Reviewed-by: Richard Henderson <[email protected]>
+Object *object_new_child(Object *parent, const char *id,
+ const char *typename)
+{
+ Object *obj;
+
+ g_assert(parent);
+ g_assert(id);
Of course, typename can't be null either.
But it reminds me that gcc and llvm have attributes nonnull and
returns_nonnull; we should be using those, both for their Werror and
documentation values.
'nonnull' attribute is not useful at runtime, and the odd of writing
code with NULL argument are low.
'returns_nonnull' is a hint to optimizers and don't help protecting
us from bad code / pattern.
OTOH the one I find useful is 'warn_unused'.