diff mbox series

[v3,48/53] qom: object_class_property_add_field() function

Message ID 20201112214350.872250-49-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
It is similar to object_class_property_add_field_static(), but
gets a copy of a Property struct so we don't need
static variables.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
This is a new patch added in v3 of this series, but v2 had a
object_class_property_add_field() function too.

This version of object_class_property_add_field() is slightly
different from the one in v2, as it gets a copy of the Property
struct (so it won't require static variables anymore).
---
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/qom/field-property.h | 23 +++++++++++++++++++++++
 qom/field-property.c         | 14 ++++++++++++++
 2 files changed, 37 insertions(+)
diff mbox series

Patch

diff --git a/include/qom/field-property.h b/include/qom/field-property.h
index 419e5eef75..a904f98609 100644
--- a/include/qom/field-property.h
+++ b/include/qom/field-property.h
@@ -83,6 +83,29 @@  struct PropertyInfo {
     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.
+ *
+ * Note that data pointed by @prop (like strings or pointers to
+ * other structs) are not copied and 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(ObjectClass *oc, const char *name,
+                                Property prop,
+                                ObjectPropertyAllowSet allow_set);
+
 void *object_field_prop_ptr(Object *obj, Property *prop);
 
 #endif
diff --git a/qom/field-property.c b/qom/field-property.c
index 1fd11f2ad3..cb729626ce 100644
--- a/qom/field-property.c
+++ b/qom/field-property.c
@@ -125,6 +125,20 @@  object_class_property_add_field_static(ObjectClass *oc, const char *name,
     return op;
 }
 
+ObjectProperty *
+object_class_property_add_field(ObjectClass *oc, const char *name,
+                                Property prop,
+                                ObjectPropertyAllowSet allow_set)
+{
+    /*
+     * QOM classes and class properties are never deallocated, so we don't
+     * have a corresponding release function that will free newprop.
+     */
+    Property *newprop = g_new0(Property, 1);
+    *newprop = prop;
+    return object_class_property_add_field_static(oc, name, newprop, allow_set);
+}
+
 void object_class_add_field_properties(ObjectClass *oc, Property *props,
                                        ObjectPropertyAllowSet allow_set)
 {