yeahuh / rpms / qemu-kvm

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