[14/26] object: add direct link flag
diff mbox series

Message ID 20191201111531.1136947-15-marcandre.lureau@redhat.com
State New
Headers show
Series
  • Make QDev use class properties
Related show

Commit Message

Marc-André Lureau Dec. 1, 2019, 11:15 a.m. UTC
Allow the link property to hold the pointer to the target, instead of
indirectly through another variable.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 include/qom/object.h |  3 +++
 qom/object.c         | 26 ++++++++++++++++++++------
 2 files changed, 23 insertions(+), 6 deletions(-)

Patch
diff mbox series

diff --git a/include/qom/object.h b/include/qom/object.h
index 0ac1a9acca..9bef5783f1 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -1509,6 +1509,9 @@  void object_property_add_child(Object *obj, const char *name,
 typedef enum {
     /* Unref the link pointer when the property is deleted */
     OBJ_PROP_LINK_STRONG = 0x1,
+
+    /* private */
+    OBJ_PROP_LINK_DIRECT = 0x2,
 } ObjectPropertyLinkFlags;
 
 /**
diff --git a/qom/object.c b/qom/object.c
index 6f3800c693..d9fc71c284 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1696,17 +1696,30 @@  void object_property_allow_set_link(const Object *obj, const char *name,
 }
 
 typedef struct {
-    Object **targetp;
+    union {
+        Object **targetp;
+        Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer  */
+    };
     void (*check)(const Object *, const char *, Object *, Error **);
     ObjectPropertyLinkFlags flags;
 } LinkProperty;
 
+static Object **
+object_link_get_targetp(Object *obj, LinkProperty *lprop)
+{
+    if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
+        return &lprop->target;
+    } else {
+        return lprop->targetp;
+    }
+}
+
 static void object_get_link_property(Object *obj, Visitor *v,
                                      const char *name, void *opaque,
                                      Error **errp)
 {
     LinkProperty *lprop = opaque;
-    Object **targetp = lprop->targetp;
+    Object **targetp = object_link_get_targetp(obj, lprop);
     gchar *path;
 
     if (*targetp) {
@@ -1765,7 +1778,7 @@  static void object_set_link_property(Object *obj, Visitor *v,
 {
     Error *local_err = NULL;
     LinkProperty *prop = opaque;
-    Object **targetp = prop->targetp;
+    Object **targetp = object_link_get_targetp(obj, prop);
     Object *old_target = *targetp;
     Object *new_target = NULL;
     char *path = NULL;
@@ -1799,16 +1812,17 @@  static Object *object_resolve_link_property(Object *parent, void *opaque, const
 {
     LinkProperty *lprop = opaque;
 
-    return *lprop->targetp;
+    return *object_link_get_targetp(parent, lprop);
 }
 
 static void object_release_link_property(Object *obj, const char *name,
                                          void *opaque)
 {
     LinkProperty *prop = opaque;
+    Object **targetp = object_link_get_targetp(obj, prop);
 
-    if ((prop->flags & OBJ_PROP_LINK_STRONG) && *prop->targetp) {
-        object_unref(*prop->targetp);
+    if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
+        object_unref(*targetp);
     }
     g_free(prop);
 }