aboutsummaryrefslogtreecommitdiff
path: root/include/qapi/qmp/qobject.h
diff options
context:
space:
mode:
authorMarc-André Lureau <marcandre.lureau@redhat.com>2018-04-19 17:01:42 +0200
committerMarkus Armbruster <armbru@redhat.com>2018-05-04 08:27:53 +0200
commit3d3eacaeccaab718ea0e2ddaa578bfae9e311c59 (patch)
treee0f8f345f94f9de6b203da0e4a8ff06b23e4c5ae /include/qapi/qmp/qobject.h
parent7ee9edfdb117da47c86c9764d90f0be11a648666 (diff)
qobject: use a QObjectBase_ struct
By moving the base fields to a QObjectBase_, QObject can be a type which also has a 'base' field. This allows writing a generic QOBJECT() macro that will work with any QObject type, including QObject itself. The container_of() macro ensures that the object to cast has a QObjectBase_ base field, giving some type safety guarantees. QObject must have no members but QObjectBase_ base, or else QOBJECT() breaks. QObjectBase_ is not a typedef and uses a trailing underscore to make it obvious it is not for normal use and to avoid potential abuse. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Message-Id: <20180419150145.24795-3-marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
Diffstat (limited to 'include/qapi/qmp/qobject.h')
-rw-r--r--include/qapi/qmp/qobject.h31
1 files changed, 20 insertions, 11 deletions
diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h
index 5206ff9ee1..a713c0165b 100644
--- a/include/qapi/qmp/qobject.h
+++ b/include/qapi/qmp/qobject.h
@@ -34,13 +34,21 @@
#include "qapi/qapi-builtin-types.h"
-struct QObject {
+/* Not for use outside include/qapi/qmp/ */
+struct QObjectBase_ {
QType type;
size_t refcnt;
};
-/* Get the 'base' part of an object */
-#define QOBJECT(obj) (&(obj)->base)
+/* this struct must have no other members than base */
+struct QObject {
+ struct QObjectBase_ base;
+};
+
+#define QOBJECT(obj) ({ \
+ typeof(obj) _obj = (obj); \
+ _obj ? container_of(&(_obj)->base, QObject, base) : NULL; \
+})
/* High-level interface for qobject_incref() */
#define QINCREF(obj) \
@@ -68,8 +76,8 @@ QEMU_BUILD_BUG_MSG(QTYPE__MAX != 7,
static inline void qobject_init(QObject *obj, QType type)
{
assert(QTYPE_NONE < type && type < QTYPE__MAX);
- obj->refcnt = 1;
- obj->type = type;
+ obj->base.refcnt = 1;
+ obj->base.type = type;
}
/**
@@ -77,8 +85,9 @@ static inline void qobject_init(QObject *obj, QType type)
*/
static inline void qobject_incref(QObject *obj)
{
- if (obj)
- obj->refcnt++;
+ if (obj) {
+ obj->base.refcnt++;
+ }
}
/**
@@ -101,8 +110,8 @@ void qobject_destroy(QObject *obj);
*/
static inline void qobject_decref(QObject *obj)
{
- assert(!obj || obj->refcnt);
- if (obj && --obj->refcnt == 0) {
+ assert(!obj || obj->base.refcnt);
+ if (obj && --obj->base.refcnt == 0) {
qobject_destroy(obj);
}
}
@@ -112,8 +121,8 @@ static inline void qobject_decref(QObject *obj)
*/
static inline QType qobject_type(const QObject *obj)
{
- assert(QTYPE_NONE < obj->type && obj->type < QTYPE__MAX);
- return obj->type;
+ assert(QTYPE_NONE < obj->base.type && obj->base.type < QTYPE__MAX);
+ return obj->base.type;
}
/**