diff mbox series

[v3,34/53] qdev: Make qdev_prop_allow_set() a ObjectProperty.allow_set callback

Message ID 20201112214350.872250-35-ehabkost@redhat.com
State New
Headers show
Series Make qdev static property API usable by any QOM type | expand

Commit Message

Eduardo Habkost Nov. 12, 2020, 9:43 p.m. UTC
Add a new allow_set parameter to object*_property_add_field(),
and pass qdev_prop_allow_set as argument in the qdev registration
code.

This removes the last remaining line of code inside the core
field property code that's specific to TYPE_DEVICE, and will
allow us to make field properties a core QOM feature.

The array property also needs to set PropertyInfo.allow_set
manually, but this will be cleaned up later (that code will be
changed to use object_class_property_add_field() too).

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
Changes v2 -> v3:
* Moved earlier in the series, before array property cleanup
  (I did this to allow us to move the code to QOM earlier)
* Redone after object_class_add_field_properties()
  was reintroduced

Changes v1 -> v2:
* Redone patch on top of changes in previous patches in the
  series
* Forbid allow_set==NULL to avoid confusion with link property
  semantics (where NULL makes the property read only)
---
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 | 16 ++++++++---
 hw/core/qdev-properties.c    | 51 ++++++++++++++++++++++--------------
 2 files changed, 45 insertions(+), 22 deletions(-)
diff mbox series

Patch

diff --git a/hw/core/qdev-prop-internal.h b/hw/core/qdev-prop-internal.h
index 740a5e530b..47bab46810 100644
--- a/hw/core/qdev-prop-internal.h
+++ b/hw/core/qdev-prop-internal.h
@@ -30,34 +30,43 @@  void field_prop_get_size32(Object *obj, Visitor *v, const char *name,
  * @obj: object instance
  * @name: property name
  * @prop: property definition
+ * @allow_set: check function called when property is set
  *
  * 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);
+                          Property *prop,
+                          ObjectPropertyAllowSet allow_set);
 
 /**
  * object_class_property_add_field_static: Add a field property to object class
  * @oc: object class
  * @name: property name
  * @prop: property definition
+ * @allow_set: check function called when property is set
  *
  * 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.
+ *
+ * @allow_set should not be NULL.  If the property can always be
+ * set, `prop_allow_set_always` can be used.  If the property can
+ * never be set, `prop_allow_set_never` can be used.
  */
 ObjectProperty *
 object_class_property_add_field_static(ObjectClass *oc, const char *name,
-                                       Property *prop);
+                                       Property *prop,
+                                       ObjectPropertyAllowSet allow_set);
 
 /**
  * object_class_add_field_properties: Add field properties from array to a class
  * @oc: object class
  * @props: array of property definitions
+ * @allow_set: check function called when property is set
  *
  * Register an array of field properties to a class, using
  * object_class_property_add_field_static() for each array element.
@@ -65,6 +74,7 @@  object_class_property_add_field_static(ObjectClass *oc, const char *name,
  * 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);
+void object_class_add_field_properties(ObjectClass *oc, Property *props,
+                                       ObjectPropertyAllowSet allow_set);
 
 #endif
diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index 9d25b49fc1..638daf1ebf 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -25,13 +25,13 @@  void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
 }
 
 /* returns: true if property is allowed to be set, false otherwise */
-static bool qdev_prop_allow_set(Object *obj, const char *name,
+static bool qdev_prop_allow_set(Object *obj, ObjectProperty *op,
                                 Error **errp)
 {
     DeviceState *dev = DEVICE(obj);
 
     if (dev->realized) {
-        qdev_prop_set_after_realize(dev, name, errp);
+        qdev_prop_set_after_realize(dev, op->name, errp);
         return false;
     }
     return true;
@@ -79,10 +79,6 @@  static void field_prop_set(Object *obj, Visitor *v, const char *name,
 {
     Property *prop = opaque;
 
-    if (!qdev_prop_allow_set(obj, name, errp)) {
-        return;
-    }
-
     return prop->info->set(obj, v, name, opaque, errp);
 }
 
@@ -560,6 +556,7 @@  static void set_prop_arraylen(Object *obj, Visitor *v, const char *name,
      * array itself and dynamically add the corresponding properties.
      */
     Property *prop = opaque;
+    ObjectProperty *op = object_property_find_err(obj, name, &error_abort);
     uint32_t *alenptr = object_field_prop_ptr(obj, prop);
     void **arrayptr = (void *)obj + prop->arrayoffset;
     void *eltptr;
@@ -592,6 +589,7 @@  static void set_prop_arraylen(Object *obj, Visitor *v, const char *name,
     for (i = 0; i < *alenptr; i++, eltptr += prop->arrayfieldsize) {
         char *propname = g_strdup_printf("%s[%d]", arrayname, i);
         ArrayElementProperty *arrayprop = g_new0(ArrayElementProperty, 1);
+        ObjectProperty *elmop;
         arrayprop->release = prop->arrayinfo->release;
         arrayprop->propname = propname;
         arrayprop->prop.info = prop->arrayinfo;
@@ -603,12 +601,13 @@  static void set_prop_arraylen(Object *obj, Visitor *v, const char *name,
          */
         arrayprop->prop.offset = eltptr - (void *)obj;
         assert(object_field_prop_ptr(obj, &arrayprop->prop) == eltptr);
-        object_property_add(obj, propname,
-                            arrayprop->prop.info->name,
-                            field_prop_getter(arrayprop->prop.info),
-                            field_prop_setter(arrayprop->prop.info),
-                            array_element_release,
-                            arrayprop);
+        elmop = object_property_add(obj, propname,
+                                    arrayprop->prop.info->name,
+                                    field_prop_getter(arrayprop->prop.info),
+                                    field_prop_setter(arrayprop->prop.info),
+                                    array_element_release,
+                                    arrayprop);
+        elmop->allow_set = op->allow_set;
     }
 }
 
@@ -823,9 +822,13 @@  const PropertyInfo prop_info_size = {
 static ObjectProperty *create_link_property(ObjectClass *oc, const char *name,
                                             Property *prop)
 {
+    /*
+     * NOTE: object_property_allow_set_link is unconditional, but
+     *       ObjectProperty.allow_set may be set for the property too.
+     */
     return object_class_property_add_link(oc, name, prop->link_type,
                                           prop->offset,
-                                          qdev_prop_allow_set_link_before_realize,
+                                          object_property_allow_set_link,
                                           OBJ_PROP_LINK_STRONG);
 }
 
@@ -836,10 +839,12 @@  const PropertyInfo prop_info_link = {
 
 ObjectProperty *
 object_property_add_field(Object *obj, const char *name,
-                          Property *prop)
+                          Property *prop,
+                          ObjectPropertyAllowSet allow_set)
 {
     ObjectProperty *op;
 
+    assert(allow_set);
     assert(!prop->info->create);
 
     op = object_property_add(obj, name, prop->info->name,
@@ -858,15 +863,19 @@  object_property_add_field(Object *obj, const char *name,
         }
     }
 
+    op->allow_set = allow_set;
     return op;
 }
 
 ObjectProperty *
 object_class_property_add_field_static(ObjectClass *oc, const char *name,
-                                       Property *prop)
+                                       Property *prop,
+                                       ObjectPropertyAllowSet allow_set)
 {
     ObjectProperty *op;
 
+    assert(allow_set);
+
     if (prop->info->create) {
         op = prop->info->create(oc, name, prop);
     } else {
@@ -884,22 +893,26 @@  object_class_property_add_field_static(ObjectClass *oc, const char *name,
         object_class_property_set_description(oc, name,
                                               prop->info->description);
     }
+
+    op->allow_set = allow_set;
     return op;
 }
 
-void object_class_add_field_properties(ObjectClass *oc, Property *props)
+void object_class_add_field_properties(ObjectClass *oc, Property *props,
+                                       ObjectPropertyAllowSet allow_set)
 {
     Property *prop;
 
     for (prop = props; prop && prop->name; prop++) {
-        object_class_property_add_field_static(oc, prop->name, prop);
+        object_class_property_add_field_static(oc, prop->name, prop, allow_set);
     }
 }
 
 
 void qdev_property_add_static(DeviceState *dev, Property *prop)
 {
-    object_property_add_field(OBJECT(dev), prop->name, prop);
+    object_property_add_field(OBJECT(dev), prop->name, prop,
+                              qdev_prop_allow_set);
 }
 
 /**
@@ -958,7 +971,7 @@  void device_class_set_props(DeviceClass *dc, Property *props)
         qdev_class_add_legacy_property(dc, prop);
     }
 
-    object_class_add_field_properties(oc, props);
+    object_class_add_field_properties(oc, props, qdev_prop_allow_set);
 }
 
 void qdev_alias_all_properties(DeviceState *target, Object *source)