Skip to content

Commit 7ee9edf

Browse files
elmarcoMarkus Armbruster
authored and
Markus Armbruster
committed
qobject: Ensure base is at offset 0
All QObject types have the base QObject as their first field. This allows the simplification of qobject_to(). Signed-off-by: Marc-André Lureau <[email protected]> Reviewed-by: Eric Blake <[email protected]> Message-Id: <[email protected]> Reviewed-by: Markus Armbruster <[email protected]> [Commit message paragraph on type casts dropped, to avoid giving the impression type casting would be okay] Signed-off-by: Markus Armbruster <[email protected]>
1 parent 46cfbf1 commit 7ee9edf

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

include/qapi/qmp/qobject.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ struct QObject {
6161
QEMU_BUILD_BUG_MSG(QTYPE__MAX != 7,
6262
"The QTYPE_CAST_TO_* list needs to be extended");
6363

64-
#define qobject_to(type, obj) ({ \
65-
QObject *_tmp = qobject_check_type(obj, glue(QTYPE_CAST_TO_, type)); \
66-
_tmp ? container_of(_tmp, type, base) : (type *)NULL; })
64+
#define qobject_to(type, obj) \
65+
((type *)qobject_check_type(obj, glue(QTYPE_CAST_TO_, type)))
6766

6867
/* Initialize an object to default values */
6968
static inline void qobject_init(QObject *obj, QType type)

qobject/qobject.c

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
#include "qapi/qmp/qlist.h"
1717
#include "qapi/qmp/qstring.h"
1818

19+
QEMU_BUILD_BUG_MSG(
20+
offsetof(QNull, base) != 0 ||
21+
offsetof(QNum, base) != 0 ||
22+
offsetof(QString, base) != 0 ||
23+
offsetof(QDict, base) != 0 ||
24+
offsetof(QList, base) != 0 ||
25+
offsetof(QBool, base) != 0,
26+
"base qobject must be at offset 0");
27+
1928
static void (*qdestroy[QTYPE__MAX])(QObject *) = {
2029
[QTYPE_NONE] = NULL, /* No such object exists */
2130
[QTYPE_QNULL] = NULL, /* qnull_ is indestructible */

0 commit comments

Comments
 (0)