Discussion:
[PATCH] sys/queue.h: add a few more helpers from FreeBSD
Mike Frysinger
2011-09-19 18:53:25 UTC
Permalink
we've found some newer packages expect these defines to be in sys/queue.h when
building on glibc systems. so import them from FreeBSD.

2011-09-19 Mike Frysinger <***@gentoo.org>

* misc/sys/queue.h (LIST_FOREACH_SAFE, SLIST_FOREACH_SAFE,
SLIST_FOREACH_PREVPTR, STAILQ_LAST, STAILQ_FOREACH_SAFE,
TAILQ_FOREACH_SAFE. TAILQ_FOREACH_REVERSE_SAFE): Define.

--- libc/misc/sys/queue.h
+++ libc/misc/sys/queue.h
@@ -136,6 +136,11 @@ struct { \
(var); \
(var) = ((var)->field.le_next))

+#define LIST_FOREACH_SAFE(var, head, field, tvar) \
+ for ((var) = LIST_FIRST((head)); \
+ (var) && ((tvar) = LIST_NEXT((var), field), 1); \
+ (var) = (tvar))
+
/*
* List access methods.
*/
@@ -197,6 +202,16 @@ struct { \
#define SLIST_FOREACH(var, head, field) \
for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)

+#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
+ for ((var) = SLIST_FIRST((head)); \
+ (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
+ (var) = (tvar))
+
+#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
+ for ((varp) = &SLIST_FIRST((head)); \
+ ((var) = *(varp)) != NULL; \
+ (varp) = &SLIST_NEXT((var), field))
+
/*
* Singly-linked List access methods.
*/
@@ -242,6 +257,12 @@ struct { \
(head)->stqh_last = &(elm)->field.stqe_next; \
} while (/*CONSTCOND*/0)

+#define STAILQ_LAST(head, type, field) \
+ (STAILQ_EMPTY((head)) ? \
+ NULL : \
+ ((struct type *)(void *) \
+ ((char *)((head)->stqh_last) - offsetof(struct type, field))))
+
#define STAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
(head)->stqh_last = &(elm)->field.stqe_next; \
@@ -271,6 +292,11 @@ struct { \
(var); \
(var) = ((var)->field.stqe_next))

+#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
+ for ((var) = STAILQ_FIRST((head)); \
+ (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
+ (var) = (tvar))
+

/*
* Singly-linked Tail queue access methods.
@@ -437,10 +463,21 @@ struct { \
(var); \
(var) = ((var)->field.tqe_next))

+#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
+ for ((var) = TAILQ_FIRST((head)); \
+ (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
+ (var) = (tvar))
+
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \
(var); \
(var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
+
+#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
+ for ((var) = TAILQ_LAST((head), headname); \
+ (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
+ (var) = (tvar))
+

/*
* Tail queue access methods.
Roland McGrath
2011-09-19 19:37:19 UTC
Permalink
Is there consistency among the modern *BSD variants about this file?
I'm inclined to update the whole file verbatim from a newer BSD version
if there is a common one (and it doesn't add new copyright owners).


Thanks,
Roland
Mike Frysinger
2011-09-19 20:05:02 UTC
Permalink
Post by Roland McGrath
Is there consistency among the modern *BSD variants about this file?
I'm inclined to update the whole file verbatim from a newer BSD version
if there is a common one (and it doesn't add new copyright owners).
in the little usage ive seen, FreeBSD has been the semi-canonical source in
that other distros often cherry pick from them. last time i tried to do a
full file sync though, things broke. maybe i screwed it up, but i focused on
importing the updated defines that people wanted so i wouldnt have to worry
about it again.

i could take another look to see what's up.
-mike
Ulrich Drepper
2011-10-08 08:29:23 UTC
Permalink
Post by Mike Frysinger
we've found some newer packages expect these defines to be in sys/queue.h when
building on glibc systems.  so import them from FreeBSD.
What packages?

Loading...