[PATCH v2 02/21] qom/object: cache ObjectPropertyFlags in ObjectProperty

Zhao Liu posted 21 patches 14 hours ago
Maintainers: Pierrick Bouvier <pierrick.bouvier@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, "Michael S. Tsirkin" <mst@redhat.com>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Richard Henderson <richard.henderson@linaro.org>, Zhao Liu <zhao1.liu@intel.com>
[PATCH v2 02/21] qom/object: cache ObjectPropertyFlags in ObjectProperty
Posted by Zhao Liu 14 hours ago
Introduce a 'flags' field in the ObjectProperty structure to store
property-specific behavior modifiers.

Currently, ObjectPropertyFlags (READ/WRITE) are only used for property
creation (e.g., initialize accessors for pointer properties).

By caching these flags directly in ObjectProperty, we can:
1. Preserve the initial access intent (read/write) of the property.
   * the READ/WRITE flags cached in ObjectProperty have no effect for
     now, but they may be useful for cases need write-once, if needed.

2. Reuse existing ObjectPropertyFlags and provide a foundation for
   future flags (e.g., to track if a property was explicitly set by
   external user).

To avoid "incomplete type" error, move ObjectPropertyFlags before
ObjectProperty definition, and polish the comment of ObjectPropertyFlags
to clarify how READ/WRITE flags work for general properties.

Suggested-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
 include/qom/object.h | 34 +++++++++++++++++++++++++---------
 qom/object.c         | 16 +++++++++++++++-
 2 files changed, 40 insertions(+), 10 deletions(-)

diff --git a/include/qom/object.h b/include/qom/object.h
index 6226b88c1eda..05706d4d7e3a 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -87,6 +87,30 @@ typedef void (ObjectPropertyRelease)(Object *obj,
  */
 typedef void (ObjectPropertyInit)(Object *obj, ObjectProperty *prop);
 
+typedef enum {
+    /*
+     * The property is readable and has a getter.
+     *
+     * For pointer property, this flag (set in object_{class_}property_add_*_ptr())
+     * will automatically add a getter to this property.
+     */
+    OBJ_PROP_FLAG_READ = BIT(0),
+    /*
+     * The property is writable and has a setter.
+     *
+     * For pointer property, this flag (set in object_{class_}property_add_*_ptr())
+     * will automatically add a setter to this property.
+     */
+    OBJ_PROP_FLAG_WRITE = BIT(1),
+    /*
+     * The property is readable and writable, as well as has a getter and a setter.
+     *
+     * For pointer property, this flag (set in object_{class_}property_add_*_ptr())
+     * will automatically add a getter and a setter to this property.
+     */
+    OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE),
+} ObjectPropertyFlags;
+
 struct ObjectProperty
 {
     char *name;
@@ -99,6 +123,7 @@ struct ObjectProperty
     ObjectPropertyInit *init;
     void *opaque;
     QObject *defval;
+    ObjectPropertyFlags flags;
 };
 
 /**
@@ -1840,15 +1865,6 @@ ObjectProperty *object_class_property_add_tm(ObjectClass *klass,
                             const char *name,
                             void (*get)(Object *, struct tm *, Error **));
 
-typedef enum {
-    /* Automatically add a getter to the property */
-    OBJ_PROP_FLAG_READ = BIT(0),
-    /* Automatically add a setter to the property */
-    OBJ_PROP_FLAG_WRITE = BIT(1),
-    /* Automatically add a getter and a setter to the property */
-    OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE),
-} ObjectPropertyFlags;
-
 /**
  * object_property_add_uint8_ptr:
  * @obj: the object to add a property to
diff --git a/qom/object.c b/qom/object.c
index ff8ede8a328e..f5801f4624c8 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1233,6 +1233,19 @@ void object_unref(void *objptr)
     }
 }
 
+static inline void object_property_flags_init(ObjectProperty *prop)
+{
+    uint8_t flags = 0;
+
+    if (prop->set) {
+        flags |= OBJ_PROP_FLAG_WRITE;
+    }
+    if (prop->get) {
+        flags |= OBJ_PROP_FLAG_READ;
+    }
+    prop->flags |= flags;
+}
+
 ObjectProperty *
 object_property_try_add(Object *obj, const char *name, const char *type,
                         ObjectPropertyAccessor *get,
@@ -1279,6 +1292,7 @@ object_property_try_add(Object *obj, const char *name, const char *type,
     prop->set = set;
     prop->release = release;
     prop->opaque = opaque;
+    object_property_flags_init(prop);
 
     g_hash_table_insert(obj->properties, prop->name, prop);
     return prop;
@@ -1317,9 +1331,9 @@ object_class_property_add(ObjectClass *klass,
     prop->set = set;
     prop->release = release;
     prop->opaque = opaque;
+    object_property_flags_init(prop);
 
     g_hash_table_insert(klass->properties, prop->name, prop);
-
     return prop;
 }
 
-- 
2.34.1