Re: [PATCH v2 09/10] qom: Allow default values for instance properties
Mark Cave-Ayland <[email protected]> Thu, 11 Jun 2026 15:08:41 +0100
| Newsgroups | org.nongnu.qemu-rust,org.nongnu.qemu-arm,org.nongnu.qemu-devel |
|---|---|
| Message-ID | <[email protected]> |
On 09/06/2026 18:25, Peter Xu wrote: > ObjectProperty allows default value settings via proper setup of > prop->init() and prop->defval. It currently is only supported in class > properties, not instance properties. > > However, since QOM allows class and instance properties, it should also > allow instance to add properties during instance_init() and allow the > default values to be applied properly for this instance only. Thinking back to the earlier patches, a question that has come up before is whether we should require all properties to be class properties? IIRC one of the motivations for this move was to allow schema introspection, but I don't immediately have a reference to the original discussion. > Add the logic. It will be useful for all kinds of pointer based properties > because pointers are normally not accessble in a class but only when an > instance is available. This will be able to achieve similar function as > what qdev properties have (which works by remembering offsets within a > structure instead). > > Signed-off-by: Peter Xu <[email protected]> > --- > qom/object.c | 26 ++++++++++++++++++++++++++ > 1 file changed, 26 insertions(+) > > diff --git a/qom/object.c b/qom/object.c > index fc738549dc..eaef615924 100644 > --- a/qom/object.c > +++ b/qom/object.c > @@ -487,6 +487,20 @@ static void object_class_property_init_all(Object *obj) > } > } > > +static void object_property_init_all(Object *obj) > +{ > + GHashTableIter iter; > + gpointer key, val; > + > + g_hash_table_iter_init(&iter, obj->properties); > + while (g_hash_table_iter_next(&iter, &key, &val)) { > + ObjectProperty *prop = val; > + if (prop->init) { > + prop->init(obj, prop); > + } > + } > +} > + > static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type) > { > type_initialize(type); > @@ -498,10 +512,22 @@ static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type > memset(obj, 0, type->instance_size); > obj->class = type->class; > object_ref(obj); > + /* > + * A few steps to initialize properties for the objects: > + * > + * (1) apply default values from class properties when available > + * (2) invoke instance_init(), which may add per-instance properties, > + * which may further contain default values > + * (3) apply default values from instance properties when available > + * > + * instance_post_init() should happen at last, after the instance is > + * fully initialized. > + */ > object_class_property_init_all(obj); > obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal, > NULL, object_property_free); > object_init_with_type(obj, type); > + object_property_init_all(obj); > object_post_init_with_type(obj, type); > } Nice! ATB, Mark.