qemu-devel
[Top][All Lists]
Advanced

[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: Markus Armbruster
Subject: Re: [Qemu-devel] [PATCH v5 2/5] qobject: use a QObjectBase_ struct
Date: Thu, 19 Apr 2018 08:11:08 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)

In addition to Eric's comments...

Marc-André Lureau <address@hidden> writes:

> 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
> 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
> obvious it is not for normal use and to avoid potential abuse.
>
> Signed-off-by: Marc-André Lureau <address@hidden>
[...]
> diff --git a/include/qapi/qmp/qobject.h b/include/qapi/qmp/qobject.h
> index 5206ff9ee1..0a7f800d58 100644
> --- a/include/qapi/qmp/qobject.h
> +++ 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;
> +};
> +
> +#define QOBJECT(x) ({                                       \
> +    typeof(x) __x = (x);                                    \
> +    __x ? container_of(&(__x)->base, QObject, base) : NULL; \
> +})

Identifiers that begin with double underscores are reserved for any
use.  Drop one of the two, please.

>  
>  /* High-level interface for qobject_incref() */
>  #define QINCREF(obj)      \
> @@ -68,8 +75,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;
>  }
>  
>  /**
[...]



reply via email to

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