Blame SOURCES/kvm-qobject-use-a-QObjectBase_-struct.patch

1bdc94
From a05d912ac29700014b1d6a894240a62f883e4409 Mon Sep 17 00:00:00 2001
1bdc94
From: Markus Armbruster <armbru@redhat.com>
1bdc94
Date: Mon, 18 Jun 2018 08:43:10 +0200
1bdc94
Subject: [PATCH 03/54] qobject: use a QObjectBase_ struct
1bdc94
MIME-Version: 1.0
1bdc94
Content-Type: text/plain; charset=UTF-8
1bdc94
Content-Transfer-Encoding: 8bit
1bdc94
1bdc94
RH-Author: Markus Armbruster <armbru@redhat.com>
1bdc94
Message-id: <20180618084330.30009-4-armbru@redhat.com>
1bdc94
Patchwork-id: 80742
1bdc94
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 03/23] qobject: use a QObjectBase_ struct
1bdc94
Bugzilla: 1557995
1bdc94
RH-Acked-by: Max Reitz <mreitz@redhat.com>
1bdc94
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
1bdc94
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
1bdc94
1bdc94
From: Marc-André Lureau <marcandre.lureau@redhat.com>
1bdc94
1bdc94
By moving the base fields to a QObjectBase_, QObject can be a type
1bdc94
which also has a 'base' field. This allows writing a generic QOBJECT()
1bdc94
macro that will work with any QObject type, including QObject
1bdc94
itself. The container_of() macro ensures that the object to cast has a
1bdc94
QObjectBase_ base field, giving some type safety guarantees. QObject
1bdc94
must have no members but QObjectBase_ base, or else QOBJECT() breaks.
1bdc94
1bdc94
QObjectBase_ is not a typedef and uses a trailing underscore to make
1bdc94
it obvious it is not for normal use and to avoid potential abuse.
1bdc94
1bdc94
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1bdc94
Reviewed-by: Eric Blake <eblake@redhat.com>
1bdc94
Message-Id: <20180419150145.24795-3-marcandre.lureau@redhat.com>
1bdc94
Reviewed-by: Markus Armbruster <armbru@redhat.com>
1bdc94
Signed-off-by: Markus Armbruster <armbru@redhat.com>
1bdc94
(cherry picked from commit 3d3eacaeccaab718ea0e2ddaa578bfae9e311c59)
1bdc94
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
1bdc94
---
1bdc94
 include/qapi/qmp/qbool.h   |  2 +-
1bdc94
 include/qapi/qmp/qdict.h   |  2 +-
1bdc94
 include/qapi/qmp/qlist.h   |  2 +-
1bdc94
 include/qapi/qmp/qnull.h   |  2 +-
1bdc94
 include/qapi/qmp/qnum.h    |  2 +-
1bdc94
 include/qapi/qmp/qobject.h | 31 ++++++++++++++++++++-----------
1bdc94
 include/qapi/qmp/qstring.h |  2 +-
1bdc94
 qobject/qobject.c          | 12 ++++++------
1bdc94
 tests/check-qdict.c        |  6 +++---
1bdc94
 9 files changed, 35 insertions(+), 26 deletions(-)
1bdc94
1bdc94
diff --git a/include/qapi/qmp/qbool.h b/include/qapi/qmp/qbool.h
1bdc94
index b9a44a1..5f61e38 100644
1bdc94
--- a/include/qapi/qmp/qbool.h
1bdc94
+++ b/include/qapi/qmp/qbool.h
1bdc94
@@ -17,7 +17,7 @@
1bdc94
 #include "qapi/qmp/qobject.h"
1bdc94
 
1bdc94
 struct QBool {
1bdc94
-    QObject base;
1bdc94
+    struct QObjectBase_ base;
1bdc94
     bool value;
1bdc94
 };
1bdc94
 
1bdc94
diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h
1bdc94
index 2cc3e90..921a28d 100644
1bdc94
--- a/include/qapi/qmp/qdict.h
1bdc94
+++ b/include/qapi/qmp/qdict.h
1bdc94
@@ -25,7 +25,7 @@ typedef struct QDictEntry {
1bdc94
 } QDictEntry;
1bdc94
 
1bdc94
 struct QDict {
1bdc94
-    QObject base;
1bdc94
+    struct QObjectBase_ base;
1bdc94
     size_t size;
1bdc94
     QLIST_HEAD(,QDictEntry) table[QDICT_BUCKET_MAX];
1bdc94
 };
1bdc94
diff --git a/include/qapi/qmp/qlist.h b/include/qapi/qmp/qlist.h
1bdc94
index 5c673ac..8d2c32c 100644
1bdc94
--- a/include/qapi/qmp/qlist.h
1bdc94
+++ b/include/qapi/qmp/qlist.h
1bdc94
@@ -22,7 +22,7 @@ typedef struct QListEntry {
1bdc94
 } QListEntry;
1bdc94
 
1bdc94
 struct QList {
1bdc94
-    QObject base;
1bdc94
+    struct QObjectBase_ base;
1bdc94
     QTAILQ_HEAD(,QListEntry) head;
1bdc94
 };
1bdc94
 
1bdc94
diff --git a/include/qapi/qmp/qnull.h b/include/qapi/qmp/qnull.h
1bdc94
index c992ee2..e8ea2c3 100644
1bdc94
--- a/include/qapi/qmp/qnull.h
1bdc94
+++ b/include/qapi/qmp/qnull.h
1bdc94
@@ -16,7 +16,7 @@
1bdc94
 #include "qapi/qmp/qobject.h"
1bdc94
 
1bdc94
 struct QNull {
1bdc94
-    QObject base;
1bdc94
+    struct QObjectBase_ base;
1bdc94
 };
1bdc94
 
1bdc94
 extern QNull qnull_;
1bdc94
diff --git a/include/qapi/qmp/qnum.h b/include/qapi/qmp/qnum.h
1bdc94
index 3e47475..45bf02a 100644
1bdc94
--- a/include/qapi/qmp/qnum.h
1bdc94
+++ b/include/qapi/qmp/qnum.h
1bdc94
@@ -45,7 +45,7 @@ typedef enum {
1bdc94
  * convert under the hood.
1bdc94
  */
1bdc94
 struct QNum {
1bdc94
-    QObject base;
1bdc94
+    struct QObjectBase_ base;
1bdc94
     QNumKind kind;
1bdc94
     union {
1bdc94
         int64_t i64;
1bdc94
diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h
1bdc94
index 5206ff9..a713c01 100644
1bdc94
--- a/include/qapi/qmp/qobject.h
1bdc94
+++ b/include/qapi/qmp/qobject.h
1bdc94
@@ -34,13 +34,21 @@
1bdc94
 
1bdc94
 #include "qapi/qapi-builtin-types.h"
1bdc94
 
1bdc94
-struct QObject {
1bdc94
+/* Not for use outside include/qapi/qmp/ */
1bdc94
+struct QObjectBase_ {
1bdc94
     QType type;
1bdc94
     size_t refcnt;
1bdc94
 };
1bdc94
 
1bdc94
-/* Get the 'base' part of an object */
1bdc94
-#define QOBJECT(obj) (&(obj)->base)
1bdc94
+/* this struct must have no other members than base */
1bdc94
+struct QObject {
1bdc94
+    struct QObjectBase_ base;
1bdc94
+};
1bdc94
+
1bdc94
+#define QOBJECT(obj) ({                                         \
1bdc94
+    typeof(obj) _obj = (obj);                                   \
1bdc94
+    _obj ? container_of(&(_obj)->base, QObject, base) : NULL;   \
1bdc94
+})
1bdc94
 
1bdc94
 /* High-level interface for qobject_incref() */
1bdc94
 #define QINCREF(obj)      \
1bdc94
@@ -68,8 +76,8 @@ QEMU_BUILD_BUG_MSG(QTYPE__MAX != 7,
1bdc94
 static inline void qobject_init(QObject *obj, QType type)
1bdc94
 {
1bdc94
     assert(QTYPE_NONE < type && type < QTYPE__MAX);
1bdc94
-    obj->refcnt = 1;
1bdc94
-    obj->type = type;
1bdc94
+    obj->base.refcnt = 1;
1bdc94
+    obj->base.type = type;
1bdc94
 }
1bdc94
 
1bdc94
 /**
1bdc94
@@ -77,8 +85,9 @@ static inline void qobject_init(QObject *obj, QType type)
1bdc94
  */
1bdc94
 static inline void qobject_incref(QObject *obj)
1bdc94
 {
1bdc94
-    if (obj)
1bdc94
-        obj->refcnt++;
1bdc94
+    if (obj) {
1bdc94
+        obj->base.refcnt++;
1bdc94
+    }
1bdc94
 }
1bdc94
 
1bdc94
 /**
1bdc94
@@ -101,8 +110,8 @@ void qobject_destroy(QObject *obj);
1bdc94
  */
1bdc94
 static inline void qobject_decref(QObject *obj)
1bdc94
 {
1bdc94
-    assert(!obj || obj->refcnt);
1bdc94
-    if (obj && --obj->refcnt == 0) {
1bdc94
+    assert(!obj || obj->base.refcnt);
1bdc94
+    if (obj && --obj->base.refcnt == 0) {
1bdc94
         qobject_destroy(obj);
1bdc94
     }
1bdc94
 }
1bdc94
@@ -112,8 +121,8 @@ static inline void qobject_decref(QObject *obj)
1bdc94
  */
1bdc94
 static inline QType qobject_type(const QObject *obj)
1bdc94
 {
1bdc94
-    assert(QTYPE_NONE < obj->type && obj->type < QTYPE__MAX);
1bdc94
-    return obj->type;
1bdc94
+    assert(QTYPE_NONE < obj->base.type && obj->base.type < QTYPE__MAX);
1bdc94
+    return obj->base.type;
1bdc94
 }
1bdc94
 
1bdc94
 /**
1bdc94
diff --git a/include/qapi/qmp/qstring.h b/include/qapi/qmp/qstring.h
1bdc94
index 30ae260..b3b3d44 100644
1bdc94
--- a/include/qapi/qmp/qstring.h
1bdc94
+++ b/include/qapi/qmp/qstring.h
1bdc94
@@ -16,7 +16,7 @@
1bdc94
 #include "qapi/qmp/qobject.h"
1bdc94
 
1bdc94
 struct QString {
1bdc94
-    QObject base;
1bdc94
+    struct QObjectBase_ base;
1bdc94
     char *string;
1bdc94
     size_t length;
1bdc94
     size_t capacity;
1bdc94
diff --git a/qobject/qobject.c b/qobject/qobject.c
1bdc94
index 87649c5..cf4b7e2 100644
1bdc94
--- a/qobject/qobject.c
1bdc94
+++ b/qobject/qobject.c
1bdc94
@@ -37,9 +37,9 @@ static void (*qdestroy[QTYPE__MAX])(QObject *) = {
1bdc94
 
1bdc94
 void qobject_destroy(QObject *obj)
1bdc94
 {
1bdc94
-    assert(!obj->refcnt);
1bdc94
-    assert(QTYPE_QNULL < obj->type && obj->type < QTYPE__MAX);
1bdc94
-    qdestroy[obj->type](obj);
1bdc94
+    assert(!obj->base.refcnt);
1bdc94
+    assert(QTYPE_QNULL < obj->base.type && obj->base.type < QTYPE__MAX);
1bdc94
+    qdestroy[obj->base.type](obj);
1bdc94
 }
1bdc94
 
1bdc94
 
1bdc94
@@ -62,11 +62,11 @@ bool qobject_is_equal(const QObject *x, const QObject *y)
1bdc94
         return true;
1bdc94
     }
1bdc94
 
1bdc94
-    if (!x || !y || x->type != y->type) {
1bdc94
+    if (!x || !y || x->base.type != y->base.type) {
1bdc94
         return false;
1bdc94
     }
1bdc94
 
1bdc94
-    assert(QTYPE_NONE < x->type && x->type < QTYPE__MAX);
1bdc94
+    assert(QTYPE_NONE < x->base.type && x->base.type < QTYPE__MAX);
1bdc94
 
1bdc94
-    return qis_equal[x->type](x, y);
1bdc94
+    return qis_equal[x->base.type](x, y);
1bdc94
 }
1bdc94
diff --git a/tests/check-qdict.c b/tests/check-qdict.c
1bdc94
index 08d4303..07bb8f4 100644
1bdc94
--- a/tests/check-qdict.c
1bdc94
+++ b/tests/check-qdict.c
1bdc94
@@ -570,11 +570,11 @@ static void qdict_join_test(void)
1bdc94
         }
1bdc94
 
1bdc94
         /* Check the references */
1bdc94
-        g_assert(qdict_get(dict1, "foo")->refcnt == 1);
1bdc94
-        g_assert(qdict_get(dict1, "bar")->refcnt == 1);
1bdc94
+        g_assert(qdict_get(dict1, "foo")->base.refcnt == 1);
1bdc94
+        g_assert(qdict_get(dict1, "bar")->base.refcnt == 1);
1bdc94
 
1bdc94
         if (!overwrite) {
1bdc94
-            g_assert(qdict_get(dict2, "foo")->refcnt == 1);
1bdc94
+            g_assert(qdict_get(dict2, "foo")->base.refcnt == 1);
1bdc94
         }
1bdc94
 
1bdc94
         /* Clean up */
1bdc94
-- 
1bdc94
1.8.3.1
1bdc94