--- misc/sys/queue.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) --- a/misc/sys/queue.h +++ b/misc/sys/queue.h @@ -32,6 +32,8 @@ #ifndef _SYS_QUEUE_H_ #define _SYS_QUEUE_H_ +#include + /* * This file defines five types of data structures: singly-linked lists, * lists, simple queues, tail queues, and circular queues. @@ -136,6 +138,11 @@ (var); \ (var) = ((var)->field.le_next)) +#define LIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = ((head)->lh_first); \ + (var) && ((tvar) = ((var)->field.le_next), 1); \ + (var) = (tvar)) + /* * List access methods. */ @@ -197,6 +204,16 @@ #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. */ @@ -253,6 +270,11 @@ (head)->stqh_last = &(head)->stqh_first; \ } while (/*CONSTCOND*/0) +#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \ + if (((head)->stqh_first = ((elm)->field.stqe_next)) == NULL) \ + (head)->stqh_last = &((head)->stqh_first); \ +} while (/*CONSTCOND*/0) + #define STAILQ_REMOVE(head, elm, type, field) do { \ if ((head)->stqh_first == (elm)) { \ STAILQ_REMOVE_HEAD((head), field); \ @@ -271,6 +293,11 @@ (var); \ (var) = ((var)->field.stqe_next)) +#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = ((elm)->field.stqe_next); \ + (var) && ((tvar) = ((var)->field.stqe_next), 1); \ + (var) = (tvar)) + #define STAILQ_CONCAT(head1, head2) do { \ if (!STAILQ_EMPTY((head2))) { \ *(head1)->stqh_last = (head2)->stqh_first; \ @@ -437,11 +464,21 @@ (var); \ (var) = ((var)->field.tqe_next)) +#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = ((head)->tqh_first); \ + (var) && ((tvar) = ((var)->field.tqe_next), 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) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \ + (var) && ((tvar) = (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)), 1); \ + (var) = (tvar)) + #define TAILQ_CONCAT(head1, head2, field) do { \ if (!TAILQ_EMPTY(head2)) { \ *(head1)->tqh_last = (head2)->tqh_first; \