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

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