[PATCH v3 26/53] qdev: Separate generic and device-specific property registration

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

object_class_property_add_field_static() and
object_property_add_field() will be generic and part of the QOM
field property API.  Note that the new functions have a `name`
parameter because the plan is to eventually get rid of the
Property.name field.

The declarations for the new functions are being added to
qdev-properties-internal.h, but they will be moved to a QOM
header later.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
Changes v2 -> v3:
* Re-added array-based array registration function, named
  as object_class_add_field_properties()
* Renamed object_class_property_add_field() to
  object_class_property_add_field_static(), to indicate that
  the function expect the Property argument to have static life
  time.
* Keep all new functions as internal API by now,
  until we decide what's going to be the preferred API for
  registering class field properties.

Changes v1 -> v2:
* Patch redone after changes in previous patches in the series
* Rename new functions to object*_property_add_field()
---
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-prop-internal.h | 42 ++++++++++++++++++++++++++++++++++++
 hw/core/qdev-properties.c    | 37 ++++++++++++++++++++++++-------
 2 files changed, 71 insertions(+), 8 deletions(-)

diff --git a/hw/core/qdev-prop-internal.h b/hw/core/qdev-prop-internal.h
index d7b77844fe..6f17ddf271 100644
--- a/hw/core/qdev-prop-internal.h
+++ b/hw/core/qdev-prop-internal.h
@@ -25,4 +25,46 @@ void qdev_propinfo_get_int32(Object *obj, Visitor *v, const char *name,
 void qdev_propinfo_get_size32(Object *obj, Visitor *v, const char *name,
                               void *opaque, Error **errp);
 
+/**
+ * object_property_add_field: Add a field property to an object instance
+ * @obj: object instance
+ * @name: property name
+ * @prop: property definition
+ *
+ * This function should not be used in new code.  Please add class properties
+ * instead, using object_class_add_field().
+ */
+ObjectProperty *
+object_property_add_field(Object *obj, const char *name,
+                          Property *prop);
+
+/**
+ * object_class_property_add_field_static: Add a field property to object class
+ * @oc: object class
+ * @name: property name
+ * @prop: property definition
+ *
+ * Add a field property to an object class.  A field property is
+ * a property that will change a field at a specific offset of the
+ * object instance struct.
+ *
+ * *@prop must have static life time.
+ */
+ObjectProperty *
+object_class_property_add_field_static(ObjectClass *oc, const char *name,
+                                       Property *prop);
+
+/**
+ * object_class_add_field_properties: Add field properties from array to a class
+ * @oc: object class
+ * @props: array of property definitions
+ *
+ * Register an array of field properties to a class, using
+ * object_class_property_add_field_static() for each array element.
+ *
+ * The array at @props must end with DEFINE_PROP_END_OF_LIST(), and
+ * must have static life time.
+ */
+void object_class_add_field_properties(ObjectClass *oc, Property *props);
+
 #endif
diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index fcda0c8f4b..8436b60ec4 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -835,20 +835,21 @@ const PropertyInfo qdev_prop_link = {
     .create = create_link_property,
 };
 
-void qdev_property_add_static(DeviceState *dev, Property *prop)
+ObjectProperty *
+object_property_add_field(Object *obj, const char *name,
+                          Property *prop)
 {
-    Object *obj = OBJECT(dev);
     ObjectProperty *op;
 
     assert(!prop->info->create);
 
-    op = object_property_add(obj, prop->name, prop->info->name,
+    op = object_property_add(obj, name, prop->info->name,
                              field_prop_getter(prop->info),
                              field_prop_setter(prop->info),
                              prop->info->release,
                              prop);
 
-    object_property_set_description(obj, prop->name,
+    object_property_set_description(obj, name,
                                     prop->info->description);
 
     if (prop->set_default) {
@@ -857,12 +858,14 @@ void qdev_property_add_static(DeviceState *dev, Property *prop)
             op->init(obj, op);
         }
     }
+
+    return op;
 }
 
-static void qdev_class_add_property(DeviceClass *klass, const char *name,
-                                    Property *prop)
+ObjectProperty *
+object_class_property_add_field_static(ObjectClass *oc, const char *name,
+                                       Property *prop)
 {
-    ObjectClass *oc = OBJECT_CLASS(klass);
     ObjectProperty *op;
 
     if (prop->info->create) {
@@ -882,6 +885,22 @@ static void qdev_class_add_property(DeviceClass *klass, const char *name,
         object_class_property_set_description(oc, name,
                                               prop->info->description);
     }
+    return op;
+}
+
+void object_class_add_field_properties(ObjectClass *oc, Property *props)
+{
+    Property *prop;
+
+    for (prop = props; prop && prop->name; prop++) {
+        object_class_property_add_field_static(oc, prop->name, prop);
+    }
+}
+
+
+void qdev_property_add_static(DeviceState *dev, Property *prop)
+{
+    object_property_add_field(OBJECT(dev), prop->name, prop);
 }
 
 /**
@@ -932,13 +951,15 @@ static void qdev_class_add_legacy_property(DeviceClass *dc, Property *prop)
 
 void device_class_set_props(DeviceClass *dc, Property *props)
 {
+    ObjectClass *oc = OBJECT_CLASS(dc);
     Property *prop;
 
     dc->props_ = props;
     for (prop = props; prop && prop->name; prop++) {
         qdev_class_add_legacy_property(dc, prop);
-        qdev_class_add_property(dc, prop->name, prop);
     }
+
+    object_class_add_field_properties(oc, props);
 }
 
 void qdev_alias_all_properties(DeviceState *target, Object *source)
-- 
2.28.0