[PATCH 20/36] qdev: Reuse object_property_add_static() when adding array elements

Eduardo Habkost posted 36 patches 5 years, 3 months ago
Maintainers: Antony Pavlov <antonynpavlov@gmail.com>, Halil Pasic <pasic@linux.ibm.com>, Juan Quintela <quintela@redhat.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Cédric Le Goater" <clg@kaod.org>, Paolo Bonzini <pbonzini@redhat.com>, Jason Wang <jasowang@redhat.com>, Richard Henderson <rth@twiddle.net>, John Snow <jsnow@redhat.com>, Magnus Damm <magnus.damm@gmail.com>, Max Reitz <mreitz@redhat.com>, "Philippe Mathieu-Daudé" <philmd@redhat.com>, Alberto Garcia <berto@igalia.com>, Fabien Chouteau <chouteau@adacore.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Laurent Vivier <lvivier@redhat.com>, Sarah Harris <S.E.Harris@kent.ac.uk>, Eric Auger <eric.auger@redhat.com>, Thomas Huth <huth@tuxfamily.org>, Anthony Perard <anthony.perard@citrix.com>, Raphael Norwitz <raphael.norwitz@nutanix.com>, Jiri Pirko <jiri@resnulli.us>, "Edgar E. Iglesias" <edgar.iglesias@gmail.com>, Alistair Francis <alistair@alistair23.me>, Gerd Hoffmann <kraxel@redhat.com>, Christian Borntraeger <borntraeger@de.ibm.com>, Eduardo Habkost <ehabkost@redhat.com>, Artyom Tarasenko <atar4qemu@gmail.com>, "Michael S. Tsirkin" <mst@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, Amit Shah <amit@kernel.org>, Andrew Baumann <Andrew.Baumann@microsoft.com>, Corey Minyard <minyard@acm.org>, Joel Stanley <joel@jms.id.au>, Igor Mitsyanko <i.mitsyanko@gmail.com>, Peter Maydell <peter.maydell@linaro.org>, Alex Williamson <alex.williamson@redhat.com>, Cornelia Huck <cohuck@redhat.com>, Stefano Stabellini <sstabellini@kernel.org>, Peter Chubb <peter.chubb@nicta.com.au>, Andrzej Zaborowski <balrogg@gmail.com>, Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>, Fam Zheng <fam@euphon.net>, Kevin Wolf <kwolf@redhat.com>, David Gibson <david@gibson.dropbear.id.au>, Michael Rolnik <mrolnik@gmail.com>, Paul Durrant <paul@xen.org>, KONRAD Frederic <frederic.konrad@adacore.com>, Samuel Thibault <samuel.thibault@ens-lyon.org>, "Daniel P. Berrangé" <berrange@redhat.com>, Michael Walle <michael@walle.cc>, Yoshinori Sato <ysato@users.sourceforge.jp>, Stefan Hajnoczi <stefanha@redhat.com>, Yuval Shaia <yuval.shaia.ml@gmail.com>
There is a newer version of this series
[PATCH 20/36] qdev: Reuse object_property_add_static() when adding array elements
Posted by Eduardo Habkost 5 years, 3 months ago
Reuse function instead of calling object_property_add() directly.
We need to hack ObjectProperty.release to make sure we will free
the array property.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Cc: qemu-devel@nongnu.org
---
 hw/core/qdev-properties.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index 1f06dfb5d5..4fec9cb73b 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -600,7 +600,6 @@ static ArrayElementProperty *array_element_new(Object *obj,
 {
     char *propname = g_strdup_printf("%s[%d]", arrayname, index);
     ArrayElementProperty *arrayprop = g_new0(ArrayElementProperty, 1);
-    arrayprop->release = array_len_prop->arrayinfo->release;
     arrayprop->propname = propname;
     arrayprop->prop.info = array_len_prop->arrayinfo;
     arrayprop->prop.name = propname;
@@ -632,12 +631,12 @@ static void object_property_add_array_element(Object *obj,
                                               Property *array_len_prop,
                                               ArrayElementProperty *prop)
 {
-    object_property_add(obj, prop->prop.name,
-                        prop->prop.info->name,
-                        static_prop_getter(prop->prop.info),
-                        static_prop_setter(prop->prop.info),
-                        array_element_release,
-                        prop);
+    ObjectProperty *op = object_property_add_static(obj, &prop->prop);
+
+    assert((void *)prop == (void *)&prop->prop);
+    prop->release = op->release;
+    /* array_element_release() will call the original release function */
+    op->release = array_element_release;
 }
 
 static void set_prop_arraylen(Object *obj, Visitor *v, const char *name,
-- 
2.28.0


Re: [PATCH 20/36] qdev: Reuse object_property_add_static() when adding array elements
Posted by Marc-André Lureau 5 years, 3 months ago
On Fri, Oct 30, 2020 at 2:09 AM Eduardo Habkost <ehabkost@redhat.com> wrote:

> Reuse function instead of calling object_property_add() directly.
> We need to hack ObjectProperty.release to make sure we will free
> the array property.
>
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
>

that seems to be right, but could use more eyes... somebody may have an
idea of simplification.

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

---
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: "Daniel P. Berrangé" <berrange@redhat.com>
> Cc: Eduardo Habkost <ehabkost@redhat.com>
> Cc: qemu-devel@nongnu.org
> ---
>  hw/core/qdev-properties.c | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
>
> diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
> index 1f06dfb5d5..4fec9cb73b 100644
> --- a/hw/core/qdev-properties.c
> +++ b/hw/core/qdev-properties.c
> @@ -600,7 +600,6 @@ static ArrayElementProperty *array_element_new(Object
> *obj,
>  {
>      char *propname = g_strdup_printf("%s[%d]", arrayname, index);
>      ArrayElementProperty *arrayprop = g_new0(ArrayElementProperty, 1);
> -    arrayprop->release = array_len_prop->arrayinfo->release;
>      arrayprop->propname = propname;
>      arrayprop->prop.info = array_len_prop->arrayinfo;
>      arrayprop->prop.name = propname;
> @@ -632,12 +631,12 @@ static void object_property_add_array_element(Object
> *obj,
>                                                Property *array_len_prop,
>                                                ArrayElementProperty *prop)
>  {
> -    object_property_add(obj, prop->prop.name,
> -                        prop->prop.info->name,
> -                        static_prop_getter(prop->prop.info),
> -                        static_prop_setter(prop->prop.info),
> -                        array_element_release,
> -                        prop);
> +    ObjectProperty *op = object_property_add_static(obj, &prop->prop);
> +
> +    assert((void *)prop == (void *)&prop->prop);
> +    prop->release = op->release;
> +    /* array_element_release() will call the original release function */
> +    op->release = array_element_release;
>  }
>
>  static void set_prop_arraylen(Object *obj, Visitor *v, const char *name,
> --
> 2.28.0
>
>
>

-- 
Marc-André Lureau