Discussion:
Missing LIST_PREV() ?
(too old to reply)
Hans Petter Selasky
2007-05-06 11:04:04 UTC
Permalink
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?

--HPS
Hans Petter Selasky
2007-05-06 11:56:50 UTC
Permalink
Post by Hans Petter Selasky
Hi,
Why should LISTs only be forward traversable? The following piece of
No objection to the concept.
But...
Post by Hans Petter Selasky
+#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)))))
Please don't use typeof; it is a GCCism. Do you really mean NULL?
Thanks for pointing that out.

Then you will have to pass an additional argument, namely the "type":

#define LIST_PREV(head,elm,field,type) \
(((elm) == LIST_FIRST(head)) ? ((struct type *)0) : \
((struct type *)(((uint8_t *)((elm)->field.le_prev)) - \
((uint8_t *)&LIST_NEXT((struct type *)0,field)))))

How about the order of the arguments?

Is this better?

If this is accepted I will commit it to my FreeBSD P4 USB project first. Then
someone else can commit it to HEAD.

--HPS

Loading...