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

Frediano Ziglio fziglio at redhat.com
Tue Sep 20 07:12:38 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.
> 

It will.

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

It's the reason of the cast. The compiler will give warning that with -Werror
become an error. Would be worth attaching type inside GList to GList field
but would require some hacks.

> Regards,
>      Uri.
> 

Frediano


More information about the Spice-devel mailing list