Missing LIST_PREV() ?

John Baldwin jhb at freebsd.org
Tue May 8 17:55:23 UTC 2007


On Monday 07 May 2007 04:25:18 pm Giorgos Keramidas wrote:
> On 2007-05-07 23:20, Giorgos Keramidas <keramida at freebsd.org> wrote:
> >On 2007-05-05 16:17, Hans Petter Selasky <hselasky at c2i.net> wrote:
> >> Hi,
> >>
> >> Why should LISTs only be forward traversable? The following piece of
> >> code make lists backward traversable:
> >>
> >> /sys/sys/queue.h:
> >>
> >> +#define LIST_PREV(head,elm,field) \
> >> +  (((elm) == LIST_FIRST(head)) ? ((__typeof(elm))0) : \
> >> +   ((__typeof(elm))(((uint8_t *)((elm)->field.le_prev)) - \
> >> +                   ((uint8_t *)&LIST_NEXT((__typeof(elm))0,field)))))
> >>
> >> Any comments?
> >
> > 1. The use of (uint8_t *) casts is relatively ugly.

Looks like an ugly version of offsetof()

> > 2. What does LIST_PREV give us that cannot be done with TAILQ_PREV()
> >    already?
> 
> Even more importantly, which I missed in my original look
> 
> (3) The use of the gcc-specific __typeof() extension makes this unusable
> with other compilers.

This can be fixed by passing the type as an argument which is what 
TAILQ_PREV() does:

#define TAILQ_PREV(elm, headname, field)                                \
        (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))

I'm not sure how portable offsetof() would be though.  In general if you want 
this feature, you should just use a TAILQ though.  TAILQ_ENTRY() is the same 
size as a LIST_ENTRY(), it just adds one more pointer to the HEAD structure.  
It is also specifically designed to make TAILQ_PREV() work w/o needing the 
offsetof() hack.

-- 
John Baldwin


More information about the freebsd-arch mailing list