From 7ee9edfdb117da47c86c9764d90f0be11a648666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Thu, 19 Apr 2018 17:01:41 +0200 Subject: qobject: Ensure base is at offset 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All QObject types have the base QObject as their first field. This allows the simplification of qobject_to(). Signed-off-by: Marc-André Lureau Reviewed-by: Eric Blake Message-Id: <20180419150145.24795-2-marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster [Commit message paragraph on type casts dropped, to avoid giving the impression type casting would be okay] Signed-off-by: Markus Armbruster --- include/qapi/qmp/qobject.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h index e022707..5206ff9 100644 --- a/include/qapi/qmp/qobject.h +++ b/include/qapi/qmp/qobject.h @@ -61,9 +61,8 @@ struct QObject { QEMU_BUILD_BUG_MSG(QTYPE__MAX != 7, "The QTYPE_CAST_TO_* list needs to be extended"); -#define qobject_to(type, obj) ({ \ - QObject *_tmp = qobject_check_type(obj, glue(QTYPE_CAST_TO_, type)); \ - _tmp ? container_of(_tmp, type, base) : (type *)NULL; }) +#define qobject_to(type, obj) \ + ((type *)qobject_check_type(obj, glue(QTYPE_CAST_TO_, type))) /* Initialize an object to default values */ static inline void qobject_init(QObject *obj, QType type) -- cgit v1.1 From 3d3eacaeccaab718ea0e2ddaa578bfae9e311c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Thu, 19 Apr 2018 17:01:42 +0200 Subject: qobject: use a QObjectBase_ struct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Eric Blake Message-Id: <20180419150145.24795-3-marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster Signed-off-by: Markus Armbruster --- include/qapi/qmp/qbool.h | 2 +- include/qapi/qmp/qdict.h | 2 +- include/qapi/qmp/qlist.h | 2 +- include/qapi/qmp/qnull.h | 2 +- include/qapi/qmp/qnum.h | 2 +- include/qapi/qmp/qobject.h | 31 ++++++++++++++++++++----------- include/qapi/qmp/qstring.h | 2 +- 7 files changed, 26 insertions(+), 17 deletions(-) (limited to 'include') diff --git a/include/qapi/qmp/qbool.h b/include/qapi/qmp/qbool.h index b9a44a1..5f61e38 100644 --- a/include/qapi/qmp/qbool.h +++ b/include/qapi/qmp/qbool.h @@ -17,7 +17,7 @@ #include "qapi/qmp/qobject.h" struct QBool { - QObject base; + struct QObjectBase_ base; bool value; }; diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h index 2cc3e90..921a28d 100644 --- a/include/qapi/qmp/qdict.h +++ b/include/qapi/qmp/qdict.h @@ -25,7 +25,7 @@ typedef struct QDictEntry { } QDictEntry; struct QDict { - QObject base; + struct QObjectBase_ base; size_t size; QLIST_HEAD(,QDictEntry) table[QDICT_BUCKET_MAX]; }; diff --git a/include/qapi/qmp/qlist.h b/include/qapi/qmp/qlist.h index 5c673ac..8d2c32c 100644 --- a/include/qapi/qmp/qlist.h +++ b/include/qapi/qmp/qlist.h @@ -22,7 +22,7 @@ typedef struct QListEntry { } QListEntry; struct QList { - QObject base; + struct QObjectBase_ base; QTAILQ_HEAD(,QListEntry) head; }; diff --git a/include/qapi/qmp/qnull.h b/include/qapi/qmp/qnull.h index c992ee2..e8ea2c3 100644 --- a/include/qapi/qmp/qnull.h +++ b/include/qapi/qmp/qnull.h @@ -16,7 +16,7 @@ #include "qapi/qmp/qobject.h" struct QNull { - QObject base; + struct QObjectBase_ base; }; extern QNull qnull_; diff --git a/include/qapi/qmp/qnum.h b/include/qapi/qmp/qnum.h index 3e47475..45bf02a 100644 --- a/include/qapi/qmp/qnum.h +++ b/include/qapi/qmp/qnum.h @@ -45,7 +45,7 @@ typedef enum { * convert under the hood. */ struct QNum { - QObject base; + struct QObjectBase_ base; QNumKind kind; union { int64_t i64; diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h index 5206ff9..a713c01 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; } /** diff --git a/include/qapi/qmp/qstring.h b/include/qapi/qmp/qstring.h index 30ae260..b3b3d44 100644 --- a/include/qapi/qmp/qstring.h +++ b/include/qapi/qmp/qstring.h @@ -16,7 +16,7 @@ #include "qapi/qmp/qobject.h" struct QString { - QObject base; + struct QObjectBase_ base; char *string; size_t length; size_t capacity; -- cgit v1.1 From cb3e7f08aeaab0ab13e629ce8496dca150a449ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Thu, 19 Apr 2018 17:01:43 +0200 Subject: qobject: Replace qobject_incref/QINCREF qobject_decref/QDECREF MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now that we can safely call QOBJECT() on QObject * as well as its subtypes, we can have macros qobject_ref() / qobject_unref() that work everywhere instead of having to use QINCREF() / QDECREF() for QObject and qobject_incref() / qobject_decref() for its subtypes. The replacement is mechanical, except I broke a long line, and added a cast in monitor_qmp_cleanup_req_queue_locked(). Unlike qobject_decref(), qobject_unref() doesn't accept void *. Note that the new macros evaluate their argument exactly once, thus no need to shout them. Signed-off-by: Marc-André Lureau Reviewed-by: Eric Blake Message-Id: <20180419150145.24795-4-marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster [Rebased, semantic conflict resolved, commit message improved] Signed-off-by: Markus Armbruster --- include/qapi/qmp/qnull.h | 2 +- include/qapi/qmp/qobject.h | 40 ++++++++++++++++++---------------------- 2 files changed, 19 insertions(+), 23 deletions(-) (limited to 'include') diff --git a/include/qapi/qmp/qnull.h b/include/qapi/qmp/qnull.h index e8ea2c3..75b29c6 100644 --- a/include/qapi/qmp/qnull.h +++ b/include/qapi/qmp/qnull.h @@ -23,7 +23,7 @@ extern QNull qnull_; static inline QNull *qnull(void) { - QINCREF(&qnull_); + qobject_ref(&qnull_); return &qnull_; } diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h index a713c01..e20006f 100644 --- a/include/qapi/qmp/qobject.h +++ b/include/qapi/qmp/qobject.h @@ -15,17 +15,17 @@ * ------------------------------------ * * - Returning references: A function that returns an object may - * return it as either a weak or a strong reference. If the reference - * is strong, you are responsible for calling QDECREF() on the reference - * when you are done. + * return it as either a weak or a strong reference. If the + * reference is strong, you are responsible for calling + * qobject_unref() on the reference when you are done. * * If the reference is weak, the owner of the reference may free it at * any time in the future. Before storing the reference anywhere, you - * should call QINCREF() to make the reference strong. + * should call qobject_ref() to make the reference strong. * * - Transferring ownership: when you transfer ownership of a reference * by calling a function, you are no longer responsible for calling - * QDECREF() when the reference is no longer needed. In other words, + * qobject_unref() when the reference is no longer needed. In other words, * when the function returns you must behave as if the reference to the * passed object was weak. */ @@ -50,14 +50,6 @@ struct QObject { _obj ? container_of(&(_obj)->base, QObject, base) : NULL; \ }) -/* High-level interface for qobject_incref() */ -#define QINCREF(obj) \ - qobject_incref(QOBJECT(obj)) - -/* High-level interface for qobject_decref() */ -#define QDECREF(obj) \ - qobject_decref(obj ? QOBJECT(obj) : NULL) - /* Required for qobject_to() */ #define QTYPE_CAST_TO_QNull QTYPE_QNULL #define QTYPE_CAST_TO_QNum QTYPE_QNUM @@ -80,10 +72,7 @@ static inline void qobject_init(QObject *obj, QType type) obj->base.type = type; } -/** - * qobject_incref(): Increment QObject's reference count - */ -static inline void qobject_incref(QObject *obj) +static inline void qobject_ref_impl(QObject *obj) { if (obj) { obj->base.refcnt++; @@ -104,11 +93,7 @@ bool qobject_is_equal(const QObject *x, const QObject *y); */ void qobject_destroy(QObject *obj); -/** - * qobject_decref(): Decrement QObject's reference count, deallocate - * when it reaches zero - */ -static inline void qobject_decref(QObject *obj) +static inline void qobject_unref_impl(QObject *obj) { assert(!obj || obj->base.refcnt); if (obj && --obj->base.refcnt == 0) { @@ -117,6 +102,17 @@ static inline void qobject_decref(QObject *obj) } /** + * qobject_ref(): Increment QObject's reference count + */ +#define qobject_ref(obj) qobject_ref_impl(QOBJECT(obj)) + +/** + * qobject_unref(): Decrement QObject's reference count, deallocate + * when it reaches zero + */ +#define qobject_unref(obj) qobject_unref_impl(QOBJECT(obj)) + +/** * qobject_type(): Return the QObject's type */ static inline QType qobject_type(const QObject *obj) -- cgit v1.1 From f5a74a5a50387c6f980b2e2f94f062487a1826da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Thu, 19 Apr 2018 17:01:44 +0200 Subject: qobject: Modify qobject_ref() to return obj MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For convenience and clarity, make it possible to call qobject_ref() at the time when the reference is associated with a variable, or argument, by making qobject_ref() return the same pointer as given. Use that to simplify the callers. Signed-off-by: Marc-André Lureau Reviewed-by: Eric Blake Message-Id: <20180419150145.24795-5-marcandre.lureau@redhat.com> Reviewed-by: Markus Armbruster [Useless change to qobject_ref_impl() dropped, commit message improved slightly] Signed-off-by: Markus Armbruster --- include/qapi/qmp/qnull.h | 3 +-- include/qapi/qmp/qobject.h | 9 ++++++++- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/qapi/qmp/qnull.h b/include/qapi/qmp/qnull.h index 75b29c6..c142688 100644 --- a/include/qapi/qmp/qnull.h +++ b/include/qapi/qmp/qnull.h @@ -23,8 +23,7 @@ extern QNull qnull_; static inline QNull *qnull(void) { - qobject_ref(&qnull_); - return &qnull_; + return qobject_ref(&qnull_); } bool qnull_is_equal(const QObject *x, const QObject *y); diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h index e20006f..fcfd549 100644 --- a/include/qapi/qmp/qobject.h +++ b/include/qapi/qmp/qobject.h @@ -103,8 +103,15 @@ static inline void qobject_unref_impl(QObject *obj) /** * qobject_ref(): Increment QObject's reference count + * + * Returns: the same @obj. The type of @obj will be propagated to the + * return type. */ -#define qobject_ref(obj) qobject_ref_impl(QOBJECT(obj)) +#define qobject_ref(obj) ({ \ + typeof(obj) _o = (obj); \ + qobject_ref_impl(QOBJECT(_o)); \ + _o; \ +}) /** * qobject_unref(): Decrement QObject's reference count, deallocate -- cgit v1.1