Now that we call object_property_add() with exactly the same arguments as object_property_add_field() does, we can just reuse the function. We can now use a stack variable for the new Property struct, because object_property_add_field() will copy the struct.
Signed-off-by: Eduardo Habkost <ehabk...@redhat.com> --- Changes v2 -> v3: * Fix memory leak from v2, after making object_property_add_field() copy the Property struct Changes v1 -> v2: * Now we don't need to hack ObjectProperty.release anymore, patch became trivial --- Cc: Paolo Bonzini <pbonz...@redhat.com> Cc: "Daniel P. Berrangé" <berra...@redhat.com> Cc: Eduardo Habkost <ehabk...@redhat.com> Cc: qemu-devel@nongnu.org --- hw/core/qdev-properties.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index 451bb54cf6..83fd45add0 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -576,23 +576,17 @@ static void set_prop_arraylen(Object *obj, Visitor *v, const char *name, *arrayptr = eltptr = g_malloc0(*alenptr * prop->arrayfieldsize); for (i = 0; i < *alenptr; i++, eltptr += prop->arrayfieldsize) { g_autofree char *propname = g_strdup_printf("%s[%d]", arrayname, i); - Property *arrayprop = g_new0(Property, 1); - ObjectProperty *elmop; - arrayprop->info = prop->arrayinfo; + Property arrayprop = { }; + arrayprop.info = prop->arrayinfo; /* This ugly piece of pointer arithmetic sets up the offset so * that when the underlying get/set hooks call qdev_get_prop_ptr * they get the right answer despite the array element not actually * being inside the device struct. */ - arrayprop->offset = eltptr - (void *)obj; - assert(object_field_prop_ptr(obj, arrayprop) == eltptr); - elmop = object_property_add(obj, propname, - arrayprop->info->name, - field_prop_getter(arrayprop->info), - field_prop_setter(arrayprop->info), - static_prop_release_dynamic_prop, - arrayprop); - elmop->allow_set = op->allow_set; + arrayprop.offset = eltptr - (void *)obj; + assert(object_field_prop_ptr(obj, &arrayprop) == eltptr); + object_property_add_field(obj, propname, &arrayprop, + op->allow_set); } } -- 2.28.0