diff mbox series

[v2,40/44] qdev: Move core field property code to QOM

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

Commit Message

Eduardo Habkost Nov. 4, 2020, 4 p.m. UTC
Move the core of the static property code to qom/field-property.c.

The actual property type implementations are still in
qdev-properties.c, they will be moved later.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
Changes v1 -> v2:
* Rename static-property.* to field-property.*
---
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Cc: qemu-devel@nongnu.org
---
 include/hw/qdev-properties.h                  |  71 +-----------
 .../qom/field-property-internal.h             |   6 +-
 include/qom/field-property.h                  |  79 +++++++++++++
 hw/core/qdev-properties-system.c              |   2 +-
 hw/core/qdev-properties.c                     | 104 +----------------
 qom/field-property.c                          | 108 ++++++++++++++++++
 qom/meson.build                               |   1 +
 7 files changed, 194 insertions(+), 177 deletions(-)
 rename hw/core/qdev-prop-internal.h => include/qom/field-property-internal.h (91%)
 create mode 100644 include/qom/field-property.h
 create mode 100644 qom/field-property.c
diff mbox series

Patch

diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
index 89c820eeb7..bee26d0319 100644
--- a/include/hw/qdev-properties.h
+++ b/include/hw/qdev-properties.h
@@ -2,52 +2,7 @@ 
 #define QEMU_QDEV_PROPERTIES_H
 
 #include "hw/qdev-core.h"
-
-/**
- * Property:
- * @set_default: true if the default value should be set from @defval,
- *    in which case @info->set_default_value must not be NULL
- *    (if false then no default value is set by the property system
- *     and the field retains whatever value it was given by instance_init).
- * @defval: default value for the property. This is used only if @set_default
- *     is true.
- */
-struct Property {
-    /**
-     * @qdev_prop_name: qdev property name
-     *
-     * qdev_prop_name is used only by TYPE_DEVICE code
-     * (device_class_set_props(), qdev_class_add_property(), and
-     * others).
-     */
-    const char   *qdev_prop_name;
-    const PropertyInfo *info;
-    ptrdiff_t    offset;
-    uint8_t      bitnr;
-    bool         set_default;
-    union {
-        int64_t i;
-        uint64_t u;
-    } defval;
-    int          arrayoffset;
-    const PropertyInfo *arrayinfo;
-    int          arrayfieldsize;
-    const char   *link_type;
-};
-
-struct PropertyInfo {
-    const char *name;
-    const char *description;
-    const QEnumLookup *enum_table;
-    int (*print)(Object *obj, Property *prop, char *dest, size_t len);
-    void (*set_default_value)(ObjectProperty *op, const Property *prop);
-    ObjectProperty *(*create)(ObjectClass *oc, const char *name,
-                              Property *prop);
-    ObjectPropertyAccessor *get;
-    ObjectPropertyAccessor *set;
-    ObjectPropertyRelease *release;
-};
-
+#include "qom/field-property.h"
 
 /*** qdev-properties.c ***/
 
@@ -227,28 +182,6 @@  extern const PropertyInfo prop_info_link;
 #define PROP_END_OF_LIST(...) \
     FIELD_PROP(DEFINE_PROP_END_OF_LIST(NULL, __VA_ARGS__))
 
-/**
- * object_class_property_add_field: 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 exist for the life time of @oc.
- *
- * @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(ObjectClass *oc, const char *name,
-                                Property *prop,
-                                ObjectPropertyAllowSet allow_set);
-
 /*
  * Set properties between creation and realization.
  *
@@ -276,8 +209,6 @@  void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
                            const uint8_t *value);
 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
 
-void *object_field_prop_ptr(Object *obj, Property *prop);
-
 void qdev_prop_register_global(GlobalProperty *prop);
 const GlobalProperty *qdev_find_global_prop(Object *obj,
                                             const char *name);
diff --git a/hw/core/qdev-prop-internal.h b/include/qom/field-property-internal.h
similarity index 91%
rename from hw/core/qdev-prop-internal.h
rename to include/qom/field-property-internal.h
index bdcb37f14f..7aa27ce836 100644
--- a/hw/core/qdev-prop-internal.h
+++ b/include/qom/field-property-internal.h
@@ -1,12 +1,12 @@ 
 /*
- * qdev property parsing
+ * QOM field property internal API (for implementing custom types)
  *
  * This work is licensed under the terms of the GNU GPL, version 2 or later.
  * See the COPYING file in the top-level directory.
  */
 
-#ifndef HW_CORE_QDEV_PROP_INTERNAL_H
-#define HW_CORE_QDEV_PROP_INTERNAL_H
+#ifndef QOM_STATIC_PROPERTY_INTERNAL_H
+#define QOM_STATIC_PROPERTY_INTERNAL_H
 
 void field_prop_get_enum(Object *obj, Visitor *v, const char *name,
                          void *opaque, Error **errp);
diff --git a/include/qom/field-property.h b/include/qom/field-property.h
new file mode 100644
index 0000000000..bdc89b38a6
--- /dev/null
+++ b/include/qom/field-property.h
@@ -0,0 +1,79 @@ 
+/*
+ * QOM field property API
+ */
+#ifndef QOM_FIELD_PROPERTY_H
+#define QOM_FIELD_PROPERTY_H
+
+#include "qom/object.h"
+#include "qapi/util.h"
+
+/**
+ * Property:
+ * @set_default: true if the default value should be set from @defval,
+ *    in which case @info->set_default_value must not be NULL
+ *    (if false then no default value is set by the property system
+ *     and the field retains whatever value it was given by instance_init).
+ * @defval: default value for the property. This is used only if @set_default
+ *     is true.
+ */
+struct Property {
+    /**
+     * @qdev_prop_name: qdev property name
+     *
+     * qdev_prop_name is used only by TYPE_DEVICE code
+     * (device_class_set_props(), qdev_class_add_property(), and
+     * others).
+     */
+    const char   *qdev_prop_name;
+    const PropertyInfo *info;
+    ptrdiff_t    offset;
+    uint8_t      bitnr;
+    bool         set_default;
+    union {
+        int64_t i;
+        uint64_t u;
+    } defval;
+    int          arrayoffset;
+    const PropertyInfo *arrayinfo;
+    int          arrayfieldsize;
+    const char   *link_type;
+};
+
+struct PropertyInfo {
+    const char *name;
+    const char *description;
+    const QEnumLookup *enum_table;
+    int (*print)(Object *obj, Property *prop, char *dest, size_t len);
+    void (*set_default_value)(ObjectProperty *op, const Property *prop);
+    ObjectProperty *(*create)(ObjectClass *oc, const char *name,
+                              Property *prop);
+    ObjectPropertyAccessor *get;
+    ObjectPropertyAccessor *set;
+    ObjectPropertyRelease *release;
+};
+
+/**
+ * object_class_property_add_field: 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 exist for the life time of @oc.
+ *
+ * @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(ObjectClass *oc, const char *name,
+                                Property *prop,
+                                ObjectPropertyAllowSet allow_set);
+
+void *object_field_prop_ptr(Object *obj, Property *prop);
+
+#endif
diff --git a/hw/core/qdev-properties-system.c b/hw/core/qdev-properties-system.c
index 8781b856d3..8da68f076c 100644
--- a/hw/core/qdev-properties-system.c
+++ b/hw/core/qdev-properties-system.c
@@ -24,7 +24,7 @@ 
 #include "qemu/units.h"
 #include "qemu/uuid.h"
 #include "qemu/error-report.h"
-#include "qdev-prop-internal.h"
+#include "qom/field-property-internal.h"
 
 #include "audio/audio.h"
 #include "chardev/char-fe.h"
diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index 64b803a200..b75730f15c 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -8,7 +8,7 @@ 
 #include "qapi/visitor.h"
 #include "qemu/units.h"
 #include "qemu/cutils.h"
-#include "qdev-prop-internal.h"
+#include "qom/field-property-internal.h"
 
 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
                                   Error **errp)
@@ -50,48 +50,6 @@  void qdev_prop_allow_set_link_before_realize(const Object *obj,
     }
 }
 
-void *object_field_prop_ptr(Object *obj, Property *prop)
-{
-    void *ptr = obj;
-    ptr += prop->offset;
-    return ptr;
-}
-
-static void field_prop_get(Object *obj, Visitor *v, const char *name,
-                           void *opaque, Error **errp)
-{
-    Property *prop = opaque;
-    return prop->info->get(obj, v, name, opaque, errp);
-}
-
-/**
- * field_prop_getter: Return getter function to be used for property
- *
- * Return value can be NULL if @info has no getter function.
- */
-static ObjectPropertyAccessor *field_prop_getter(const PropertyInfo *info)
-{
-    return info->get ? field_prop_get : NULL;
-}
-
-static void field_prop_set(Object *obj, Visitor *v, const char *name,
-                           void *opaque, Error **errp)
-{
-    Property *prop = opaque;
-
-    return prop->info->set(obj, v, name, opaque, errp);
-}
-
-/**
- * field_prop_setter: Return setter function to be used for property
- *
- * Return value can be NULL if @info has not setter function.
- */
-static ObjectPropertyAccessor *field_prop_setter(const PropertyInfo *info)
-{
-    return info->set ? field_prop_set : NULL;
-}
-
 void field_prop_get_enum(Object *obj, Visitor *v, const char *name,
                          void *opaque, Error **errp)
 {
@@ -801,66 +759,6 @@  const PropertyInfo prop_info_link = {
     .create = create_link_property,
 };
 
-ObjectProperty *
-object_property_add_field(Object *obj, const char *name, Property *prop,
-                          ObjectPropertyAllowSet allow_set)
-{
-    ObjectProperty *op;
-
-    assert(allow_set);
-    assert(!prop->info->create);
-
-    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, name,
-                                    prop->info->description);
-
-    if (prop->set_default) {
-        prop->info->set_default_value(op, prop);
-        if (op->init) {
-            op->init(obj, op);
-        }
-    }
-
-    op->allow_set = allow_set;
-    return op;
-}
-
-ObjectProperty *
-object_class_property_add_field(ObjectClass *oc, const char *name,
-                                Property *prop,
-                                ObjectPropertyAllowSet allow_set)
-{
-    ObjectProperty *op;
-
-    assert(allow_set);
-
-    if (prop->info->create) {
-        op = prop->info->create(oc, name, prop);
-    } else {
-        op = object_class_property_add(oc,
-                                       name, prop->info->name,
-                                       field_prop_getter(prop->info),
-                                       field_prop_setter(prop->info),
-                                       prop->info->release,
-                                       prop);
-    }
-    if (prop->set_default) {
-        prop->info->set_default_value(op, prop);
-    }
-    if (prop->info->description) {
-        object_class_property_set_description(oc, name,
-                                              prop->info->description);
-    }
-
-    op->allow_set = allow_set;
-    return op;
-}
-
 void qdev_property_add_static(DeviceState *dev, Property *prop)
 {
     object_property_add_field(OBJECT(dev), prop->qdev_prop_name, prop,
diff --git a/qom/field-property.c b/qom/field-property.c
new file mode 100644
index 0000000000..25a818bb69
--- /dev/null
+++ b/qom/field-property.c
@@ -0,0 +1,108 @@ 
+/*
+ * QOM field property API implementation
+ */
+#include "qemu/osdep.h"
+#include "qom/field-property.h"
+#include "qom/field-property-internal.h"
+
+void *object_field_prop_ptr(Object *obj, Property *prop)
+{
+    void *ptr = obj;
+    ptr += prop->offset;
+    return ptr;
+}
+
+static void field_prop_get(Object *obj, Visitor *v, const char *name,
+                           void *opaque, Error **errp)
+{
+    Property *prop = opaque;
+    return prop->info->get(obj, v, name, opaque, errp);
+}
+
+/**
+ * field_prop_getter: Return getter function to be used for property
+ *
+ * Return value can be NULL if @info has no getter function.
+ */
+static ObjectPropertyAccessor *field_prop_getter(const PropertyInfo *info)
+{
+    return info->get ? field_prop_get : NULL;
+}
+
+static void field_prop_set(Object *obj, Visitor *v, const char *name,
+                           void *opaque, Error **errp)
+{
+    Property *prop = opaque;
+
+    return prop->info->set(obj, v, name, opaque, errp);
+}
+
+/**
+ * field_prop_setter: Return setter function to be used for property
+ *
+ * Return value can be NULL if @info has not setter function.
+ */
+static ObjectPropertyAccessor *field_prop_setter(const PropertyInfo *info)
+{
+    return info->set ? field_prop_set : NULL;
+}
+
+ObjectProperty *
+object_property_add_field(Object *obj, const char *name, Property *prop,
+                          ObjectPropertyAllowSet allow_set)
+{
+    ObjectProperty *op;
+
+    assert(allow_set);
+    assert(!prop->info->create);
+
+    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, name,
+                                    prop->info->description);
+
+    if (prop->set_default) {
+        prop->info->set_default_value(op, prop);
+        if (op->init) {
+            op->init(obj, op);
+        }
+    }
+
+    op->allow_set = allow_set;
+    return op;
+}
+
+ObjectProperty *
+object_class_property_add_field(ObjectClass *oc, const char *name,
+                                Property *prop,
+                                ObjectPropertyAllowSet allow_set)
+{
+    ObjectProperty *op;
+
+    assert(allow_set);
+
+    if (prop->info->create) {
+        op = prop->info->create(oc, name, prop);
+    } else {
+        op = object_class_property_add(oc,
+                                       name, prop->info->name,
+                                       field_prop_getter(prop->info),
+                                       field_prop_setter(prop->info),
+                                       prop->info->release,
+                                       prop);
+    }
+    if (prop->set_default) {
+        prop->info->set_default_value(op, prop);
+    }
+    if (prop->info->description) {
+        object_class_property_set_description(oc, name,
+                                              prop->info->description);
+    }
+
+    op->allow_set = allow_set;
+    return op;
+}
diff --git a/qom/meson.build b/qom/meson.build
index 062a3789d8..e83794454d 100644
--- a/qom/meson.build
+++ b/qom/meson.build
@@ -4,6 +4,7 @@  qom_ss.add(files(
   'object.c',
   'object_interfaces.c',
   'qom-qobject.c',
+  'field-property.c',
 ))
 
 qmp_ss.add(files('qom-qmp-cmds.c'))