yeahuh / rpms / qemu-kvm

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