[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v5 2/5] qobject: use a QObjectBase_ struct
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH v5 2/5] qobject: use a QObjectBase_ struct |
Date: |
Tue, 17 Apr 2018 13:33:37 -0500 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 |
On 04/17/2018 08:35 AM, Marc-André Lureau wrote:
> By moving the base fields to a QObjectBase_, QObject can be a type
> which also has a 'base' field. This allows to write a generic
either "allows us to write" or "allows writing"
> 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 typedef and use a trailing underscore to make it
s/not typedef and use/not a typedef and uses/
> obvious it is not for normal use and to avoid potential abuse.
>
> Signed-off-by: Marc-André Lureau <address@hidden>
> ---
> +++ b/include/qapi/qmp/qobject.h
> @@ -34,13 +34,20 @@
>
> #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)
> +struct QObject {
> + struct QObjectBase_ base;
I'd add a comment here that this struct must have no other members than
base.
> +};
> +
> +#define QOBJECT(x) ({ \
> + typeof(x) __x = (x); \
> + __x ? container_of(&(__x)->base, QObject, base) : NULL; \
> +})
>
With those changes,
Reviewed-by: Eric Blake <address@hidden>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
signature.asc
Description: OpenPGP digital signature
[Qemu-devel] [PATCH v5 2/5] qobject: use a QObjectBase_ struct, Marc-André Lureau, 2018/04/17
[Qemu-devel] [PATCH v5 4/5] qobject: modify qobject_ref() to return obj, Marc-André Lureau, 2018/04/17
[Qemu-devel] [PATCH v5 5/5] qobject: modify qobject_ref() to assert on NULL, Marc-André Lureau, 2018/04/17
[Qemu-devel] [PATCH v5 3/5] qobject: replace qobject_incref/QINCREF qobject_decref/QDECREF, Marc-André Lureau, 2018/04/17