qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 2/3] qobject: assume base of a qobject is at off


From: Eric Blake
Subject: Re: [Qemu-devel] [PATCH 2/3] qobject: assume base of a qobject is at offset 0
Date: Wed, 21 Mar 2018 09:01:45 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0

On 03/21/2018 08:40 AM, Marc-André Lureau wrote:
Simplify casting to QObject by not requiring the definition of the
type and assuming base QObject is at offset 0. Add a static check for
this.

Use the QEMU_GENERIC macro for keeping type safety check and error
when given an unknown type.

Signed-off-by: Marc-André Lureau <address@hidden>
---
  include/qapi/qmp/qobject.h | 36 ++++++++++++++++++++++++++++++++++--
  qobject/qobject.c          |  9 +++++++++
  2 files changed, 43 insertions(+), 2 deletions(-)

+++ b/qobject/qobject.c
@@ -16,6 +16,15 @@
  #include "qapi/qmp/qlist.h"
  #include "qapi/qmp/qstring.h"
+QEMU_BUILD_BUG_MSG(
+    offsetof(QNull, base) != 0 ||
+    offsetof(QNum, base) != 0 ||
+    offsetof(QString, base) != 0 ||
+    offsetof(QDict, base) != 0 ||
+    offsetof(QList, base) != 0 ||
+    offsetof(QBool, base) != 0,
+    "base qobject must be at offset 0");

Interesting. If we do this, we should also simplify qobject_to() from its current:

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

to the simpler:

#define qobject_to(type, obj) \
    ((type *)qobject_check_type(obj, glue(QTYPE_CAST_TO_, type)))


--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org



reply via email to

[Prev in Thread] Current Thread [Next in Thread]