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

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