[Spice-devel] [RFC PATCH 2/2] Introduce some macro to simplify iteration on GList

Uri Lublin uril at redhat.com
Mon Sep 19 10:17:40 UTC 2016


On 09/16/2016 06:06 PM, Frediano Ziglio wrote:
> Noting that coding by hand these loop introduced some regression
> I'm trying to introduce back from macros.
> Before trying something harder to make possible to bind the type of
> the content I'm trying some simple macro as were before.
> I added the type to avoid some blindly void* casts.
> Also the GListIter is introduced to avoid the possibility to exchange
> easily some parameters.
>
> Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
> ---
>  server/red-common.h   | 18 ++++++++++++++
>  server/reds-private.h |  3 +++
>  server/reds.c         | 66 +++++++++++++++++++++++++--------------------------
>  3 files changed, 53 insertions(+), 34 deletions(-)
>
> diff --git a/server/red-common.h b/server/red-common.h
> index 7ab7e15..190fd9c 100644
> --- a/server/red-common.h
> +++ b/server/red-common.h
> @@ -62,4 +62,22 @@ extern const SpiceCoreInterfaceInternal event_loop_core;
>
>  typedef struct RedsState RedsState;
>
> +typedef struct GListIter {
> +    GList *link;
> +    GList *next;
> +} GListIter;
> +
> +#define GLIST_FOREACH_GENERIC(_list, _iter, _type, _data, _dir) \
> +    for (_iter.link = _list; \
> +        (_data = (_type *) (_iter.link ? _iter.link->data : NULL), \
> +         _iter.next = (_iter.link ? _iter.link->_dir : NULL), \
> +         _iter.link) != NULL; \
> +         _iter.link = _iter.next)
> +
> +#define GLIST_FOREACH(_list, _iter, _type, _data) \
> +    GLIST_FOREACH_GENERIC(_list, _iter, _type, _data, next)
> +
> +#define GLIST_FOREACH_REVERSED(_list, _iter, _type, _data) \
> +    GLIST_FOREACH_GENERIC(_list, _iter, _type, _data, prev)
> +
>  #endif


Hi Frediano,

Looks good.

GLIST_FOREACH_REVERSED is not used.

In the future it would be nice to set _type
according to typeof _data, or compare _type with it.

Regards,
     Uri.




More information about the Spice-devel mailing list