The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/sys/queue.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)queue.h     8.5 (Berkeley) 8/20/94
   30  * $FreeBSD: src/sys/sys/queue.h,v 1.74 2010/12/03 16:07:50 kib Exp $
   31  */
   32 
   33 #ifndef _SYS_QUEUE_H_
   34 #define _SYS_QUEUE_H_
   35 
   36 #include <sys/cdefs.h>
   37 
   38 /*
   39  * This file defines four types of data structures: singly-linked lists,
   40  * singly-linked tail queues, lists and tail queues.
   41  *
   42  * A singly-linked list is headed by a single forward pointer. The elements
   43  * are singly linked for minimum space and pointer manipulation overhead at
   44  * the expense of O(n) removal for arbitrary elements. New elements can be
   45  * added to the list after an existing element or at the head of the list.
   46  * Elements being removed from the head of the list should use the explicit
   47  * macro for this purpose for optimum efficiency. A singly-linked list may
   48  * only be traversed in the forward direction.  Singly-linked lists are ideal
   49  * for applications with large datasets and few or no removals or for
   50  * implementing a LIFO queue.
   51  *
   52  * A singly-linked tail queue is headed by a pair of pointers, one to the
   53  * head of the list and the other to the tail of the list. The elements are
   54  * singly linked for minimum space and pointer manipulation overhead at the
   55  * expense of O(n) removal for arbitrary elements. New elements can be added
   56  * to the list after an existing element, at the head of the list, or at the
   57  * end of the list. Elements being removed from the head of the tail queue
   58  * should use the explicit macro for this purpose for optimum efficiency.
   59  * A singly-linked tail queue may only be traversed in the forward direction.
   60  * Singly-linked tail queues are ideal for applications with large datasets
   61  * and few or no removals or for implementing a FIFO queue.
   62  *
   63  * A list is headed by a single forward pointer (or an array of forward
   64  * pointers for a hash table header). The elements are doubly linked
   65  * so that an arbitrary element can be removed without a need to
   66  * traverse the list. New elements can be added to the list before
   67  * or after an existing element or at the head of the list. A list
   68  * may only be traversed in the forward direction.
   69  *
   70  * A tail queue is headed by a pair of pointers, one to the head of the
   71  * list and the other to the tail of the list. The elements are doubly
   72  * linked so that an arbitrary element can be removed without a need to
   73  * traverse the list. New elements can be added to the list before or
   74  * after an existing element, at the head of the list, or at the end of
   75  * the list. A tail queue may be traversed in either direction.
   76  *
   77  * For details on the use of these macros, see the queue(3) manual page.
   78  *
   79  *
   80  *                              SLIST   LIST    STAILQ  TAILQ
   81  * _HEAD                        +       +       +       +
   82  * _HEAD_INITIALIZER            +       +       +       +
   83  * _ENTRY                       +       +       +       +
   84  * _INIT                        +       +       +       +
   85  * _EMPTY                       +       +       +       +
   86  * _FIRST                       +       +       +       +
   87  * _NEXT                        +       +       +       +
   88  * _PREV                        -       -       -       +
   89  * _LAST                        -       -       +       +
   90  * _FOREACH                     +       +       +       +
   91  * _FOREACH_MUTABLE             +       +       +       +
   92  * _FOREACH_REVERSE             -       -       -       +
   93  * _FOREACH_REVERSE_MUTABLE     -       -       -       +
   94  * _INSERT_HEAD                 +       +       +       +
   95  * _INSERT_BEFORE               -       +       -       +
   96  * _INSERT_AFTER                +       +       +       +
   97  * _INSERT_TAIL                 -       -       +       +
   98  * _CONCAT                      -       -       +       +
   99  * _REMOVE_AFTER                +       -       +       -
  100  * _REMOVE_HEAD                 +       -       +       -
  101  * _REMOVE                      +       +       +       +
  102  * _SWAP                        +       +       +       +
  103  *
  104  */
  105 #ifdef QUEUE_MACRO_DEBUG
  106 /* Store the last 2 places the queue element or head was altered */
  107 struct qm_trace {
  108         char    *lastfile;
  109         int      lastline;
  110         char    *prevfile;
  111         int      prevline;
  112 };
  113 
  114 #define TRACEBUF        struct qm_trace trace;
  115 #define TRASHIT(x)      do {(x) = (void *)-1;} while (0)
  116 #define QMD_SAVELINK(name, link)        void **name = (void *)&(link)
  117 
  118 #define QMD_TRACE_HEAD(head) do {                                       \
  119         (head)->trace.prevline = (head)->trace.lastline;                \
  120         (head)->trace.prevfile = (head)->trace.lastfile;                \
  121         (head)->trace.lastline = __LINE__;                              \
  122         (head)->trace.lastfile = __FILE__;                              \
  123 } while (0)
  124 
  125 #define QMD_TRACE_ELEM(elem) do {                                       \
  126         (elem)->trace.prevline = (elem)->trace.lastline;                \
  127         (elem)->trace.prevfile = (elem)->trace.lastfile;                \
  128         (elem)->trace.lastline = __LINE__;                              \
  129         (elem)->trace.lastfile = __FILE__;                              \
  130 } while (0)
  131 
  132 #else
  133 #define QMD_TRACE_ELEM(elem)
  134 #define QMD_TRACE_HEAD(head)
  135 #define QMD_SAVELINK(name, link)
  136 #define TRACEBUF
  137 #define TRASHIT(x)
  138 #endif  /* QUEUE_MACRO_DEBUG */
  139 
  140 /*
  141  * Singly-linked List declarations.
  142  */
  143 #define SLIST_HEAD(name, type)                                          \
  144 struct name {                                                           \
  145         struct type *slh_first; /* first element */                     \
  146 }
  147 
  148 #define SLIST_HEAD_INITIALIZER(head)                                    \
  149         { NULL }
  150 
  151 #define SLIST_ENTRY(type)                                               \
  152 struct {                                                                \
  153         struct type *sle_next;  /* next element */                      \
  154 }
  155 
  156 #define SLIST_ENTRY_INITIALIZER { NULL }
  157 
  158 /*
  159  * Singly-linked List functions.
  160  */
  161 #define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
  162 
  163 #define SLIST_FIRST(head)       ((head)->slh_first)
  164 
  165 #define SLIST_FOREACH(var, head, field)                                 \
  166         for ((var) = SLIST_FIRST((head));                               \
  167             (var);                                                      \
  168             (var) = SLIST_NEXT((var), field))
  169 
  170 #define SLIST_FOREACH_MUTABLE(var, head, field, tvar)                   \
  171         for ((var) = SLIST_FIRST((head));                               \
  172             (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
  173             (var) = (tvar))
  174 
  175 #define SLIST_FOREACH_PREVPTR(var, varp, head, field)                   \
  176         for ((varp) = &SLIST_FIRST((head));                             \
  177             ((var) = *(varp)) != NULL;                                  \
  178             (varp) = &SLIST_NEXT((var), field))
  179 
  180 #define SLIST_INIT(head) do {                                           \
  181         SLIST_FIRST((head)) = NULL;                                     \
  182 } while (0)
  183 
  184 #define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
  185         SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);       \
  186         SLIST_NEXT((slistelm), field) = (elm);                          \
  187 } while (0)
  188 
  189 #define SLIST_INSERT_HEAD(head, elm, field) do {                        \
  190         SLIST_NEXT((elm), field) = SLIST_FIRST((head));                 \
  191         SLIST_FIRST((head)) = (elm);                                    \
  192 } while (0)
  193 
  194 #define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
  195 
  196 #define SLIST_REMOVE(head, elm, type, field) do {                       \
  197         QMD_SAVELINK(oldnext, (elm)->field.sle_next);                   \
  198         if (SLIST_FIRST((head)) == (elm)) {                             \
  199                 SLIST_REMOVE_HEAD((head), field);                       \
  200         }                                                               \
  201         else {                                                          \
  202                 struct type *curelm = SLIST_FIRST((head));              \
  203                 while (SLIST_NEXT(curelm, field) != (elm))              \
  204                         curelm = SLIST_NEXT(curelm, field);             \
  205                 SLIST_REMOVE_AFTER(curelm, field);                      \
  206         }                                                               \
  207         TRASHIT(*oldnext);                                              \
  208 } while (0)
  209 
  210 #define SLIST_REMOVE_AFTER(elm, field) do {                             \
  211         SLIST_NEXT(elm, field) =                                        \
  212             SLIST_NEXT(SLIST_NEXT(elm, field), field);                  \
  213 } while (0)
  214 
  215 #define SLIST_REMOVE_HEAD(head, field) do {                             \
  216         SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);   \
  217 } while (0)
  218 
  219 #define SLIST_SWAP(head1, head2, type) do {                             \
  220         struct type *swap_first = SLIST_FIRST(head1);                   \
  221         SLIST_FIRST(head1) = SLIST_FIRST(head2);                        \
  222         SLIST_FIRST(head2) = swap_first;                                \
  223 } while (0)
  224 
  225 /*
  226  * Singly-linked Tail queue declarations.
  227  */
  228 #define STAILQ_HEAD(name, type)                                         \
  229 struct name {                                                           \
  230         struct type *stqh_first;/* first element */                     \
  231         struct type **stqh_last;/* addr of last next element */         \
  232 }
  233 
  234 #define STAILQ_HEAD_INITIALIZER(head)                                   \
  235         { NULL, &(head).stqh_first }
  236 
  237 #define STAILQ_ENTRY(type)                                              \
  238 struct {                                                                \
  239         struct type *stqe_next; /* next element */                      \
  240 }
  241 
  242 /*
  243  * Singly-linked Tail queue functions.
  244  */
  245 #define STAILQ_CONCAT(head1, head2) do {                                \
  246         if (!STAILQ_EMPTY((head2))) {                                   \
  247                 *(head1)->stqh_last = (head2)->stqh_first;              \
  248                 (head1)->stqh_last = (head2)->stqh_last;                \
  249                 STAILQ_INIT((head2));                                   \
  250         }                                                               \
  251 } while (0)
  252 
  253 #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
  254 
  255 #define STAILQ_FIRST(head)      ((head)->stqh_first)
  256 
  257 #define STAILQ_FOREACH(var, head, field)                                \
  258         for((var) = STAILQ_FIRST((head));                               \
  259            (var);                                                       \
  260            (var) = STAILQ_NEXT((var), field))
  261 
  262 
  263 #define STAILQ_FOREACH_MUTABLE(var, head, field, tvar)                  \
  264         for ((var) = STAILQ_FIRST((head));                              \
  265             (var) && ((tvar) = STAILQ_NEXT((var), field), 1);           \
  266             (var) = (tvar))
  267 
  268 #define STAILQ_INIT(head) do {                                          \
  269         STAILQ_FIRST((head)) = NULL;                                    \
  270         (head)->stqh_last = &STAILQ_FIRST((head));                      \
  271 } while (0)
  272 
  273 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
  274         if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  275                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
  276         STAILQ_NEXT((tqelm), field) = (elm);                            \
  277 } while (0)
  278 
  279 #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
  280         if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  281                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
  282         STAILQ_FIRST((head)) = (elm);                                   \
  283 } while (0)
  284 
  285 #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
  286         STAILQ_NEXT((elm), field) = NULL;                               \
  287         *(head)->stqh_last = (elm);                                     \
  288         (head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
  289 } while (0)
  290 
  291 #define STAILQ_LAST(head, type, field)                                  \
  292         (STAILQ_EMPTY((head)) ?                                         \
  293                 NULL :                                                  \
  294                 ((struct type *)(void *)                                \
  295                 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
  296 
  297 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  298 
  299 #define STAILQ_REMOVE(head, elm, type, field) do {                      \
  300         QMD_SAVELINK(oldnext, (elm)->field.stqe_next);                  \
  301         if (STAILQ_FIRST((head)) == (elm)) {                            \
  302                 STAILQ_REMOVE_HEAD((head), field);                      \
  303         }                                                               \
  304         else {                                                          \
  305                 struct type *curelm = STAILQ_FIRST((head));             \
  306                 while (STAILQ_NEXT(curelm, field) != (elm))             \
  307                         curelm = STAILQ_NEXT(curelm, field);            \
  308                 STAILQ_REMOVE_AFTER(head, curelm, field);               \
  309         }                                                               \
  310         TRASHIT(*oldnext);                                              \
  311 } while (0)
  312 
  313 #define STAILQ_REMOVE_HEAD(head, field) do {                            \
  314         if ((STAILQ_FIRST((head)) =                                     \
  315              STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)         \
  316                 (head)->stqh_last = &STAILQ_FIRST((head));              \
  317 } while (0)
  318 
  319 #define STAILQ_REMOVE_AFTER(head, elm, field) do {                      \
  320         if ((STAILQ_NEXT(elm, field) =                                  \
  321              STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL)      \
  322                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
  323 } while (0)
  324 
  325 #define STAILQ_SWAP(head1, head2, type) do {                            \
  326         struct type *swap_first = STAILQ_FIRST(head1);                  \
  327         struct type **swap_last = (head1)->stqh_last;                   \
  328         STAILQ_FIRST(head1) = STAILQ_FIRST(head2);                      \
  329         (head1)->stqh_last = (head2)->stqh_last;                        \
  330         STAILQ_FIRST(head2) = swap_first;                               \
  331         (head2)->stqh_last = swap_last;                                 \
  332         if (STAILQ_EMPTY(head1))                                        \
  333                 (head1)->stqh_last = &STAILQ_FIRST(head1);              \
  334         if (STAILQ_EMPTY(head2))                                        \
  335                 (head2)->stqh_last = &STAILQ_FIRST(head2);              \
  336 } while (0)
  337 
  338 
  339 /*
  340  * List declarations.
  341  */
  342 #define LIST_HEAD(name, type)                                           \
  343 struct name {                                                           \
  344         struct type *lh_first;  /* first element */                     \
  345 }
  346 
  347 #define LIST_HEAD_INITIALIZER(head)                                     \
  348         { NULL }
  349 
  350 #define LIST_ENTRY(type)                                                \
  351 struct {                                                                \
  352         struct type *le_next;   /* next element */                      \
  353         struct type **le_prev;  /* address of previous next element */  \
  354 }
  355 
  356 /*
  357  * List functions.
  358  */
  359 
  360 #if (defined(_KERNEL) && defined(INVARIANTS))
  361 #define QMD_LIST_CHECK_HEAD(head, field) do {                           \
  362         if (LIST_FIRST((head)) != NULL &&                               \
  363             LIST_FIRST((head))->field.le_prev !=                        \
  364              &LIST_FIRST((head)))                                       \
  365                 panic("Bad list head %p first->prev != head", (head));  \
  366 } while (0)
  367 
  368 #define QMD_LIST_CHECK_NEXT(elm, field) do {                            \
  369         if (LIST_NEXT((elm), field) != NULL &&                          \
  370             LIST_NEXT((elm), field)->field.le_prev !=                   \
  371              &((elm)->field.le_next))                                   \
  372                 panic("Bad link elm %p next->prev != elm", (elm));      \
  373 } while (0)
  374 
  375 #define QMD_LIST_CHECK_PREV(elm, field) do {                            \
  376         if (*(elm)->field.le_prev != (elm))                             \
  377                 panic("Bad link elm %p prev->next != elm", (elm));      \
  378 } while (0)
  379 #else
  380 #define QMD_LIST_CHECK_HEAD(head, field)
  381 #define QMD_LIST_CHECK_NEXT(elm, field)
  382 #define QMD_LIST_CHECK_PREV(elm, field)
  383 #endif /* (_KERNEL && INVARIANTS) */
  384 
  385 #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
  386 
  387 #define LIST_FIRST(head)        ((head)->lh_first)
  388 
  389 #define LIST_FOREACH(var, head, field)                                  \
  390         for ((var) = LIST_FIRST((head));                                \
  391             (var);                                                      \
  392             (var) = LIST_NEXT((var), field))
  393 
  394 #define LIST_FOREACH_MUTABLE(var, head, field, tvar)                    \
  395         for ((var) = LIST_FIRST((head));                                \
  396             (var) && ((tvar) = LIST_NEXT((var), field), 1);             \
  397             (var) = (tvar))
  398 
  399 #define LIST_INIT(head) do {                                            \
  400         LIST_FIRST((head)) = NULL;                                      \
  401 } while (0)
  402 
  403 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
  404         QMD_LIST_CHECK_NEXT(listelm, field);                            \
  405         if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  406                 LIST_NEXT((listelm), field)->field.le_prev =            \
  407                     &LIST_NEXT((elm), field);                           \
  408         LIST_NEXT((listelm), field) = (elm);                            \
  409         (elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
  410 } while (0)
  411 
  412 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
  413         QMD_LIST_CHECK_PREV(listelm, field);                            \
  414         (elm)->field.le_prev = (listelm)->field.le_prev;                \
  415         LIST_NEXT((elm), field) = (listelm);                            \
  416         *(listelm)->field.le_prev = (elm);                              \
  417         (listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
  418 } while (0)
  419 
  420 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
  421         QMD_LIST_CHECK_HEAD((head), field);                             \
  422         if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
  423                 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  424         LIST_FIRST((head)) = (elm);                                     \
  425         (elm)->field.le_prev = &LIST_FIRST((head));                     \
  426 } while (0)
  427 
  428 #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
  429 
  430 #define LIST_REMOVE(elm, field) do {                                    \
  431         QMD_SAVELINK(oldnext, (elm)->field.le_next);                    \
  432         QMD_SAVELINK(oldprev, (elm)->field.le_prev);                    \
  433         QMD_LIST_CHECK_NEXT(elm, field);                                \
  434         QMD_LIST_CHECK_PREV(elm, field);                                \
  435         if (LIST_NEXT((elm), field) != NULL)                            \
  436                 LIST_NEXT((elm), field)->field.le_prev =                \
  437                     (elm)->field.le_prev;                               \
  438         *(elm)->field.le_prev = LIST_NEXT((elm), field);                \
  439         TRASHIT(*oldnext);                                              \
  440         TRASHIT(*oldprev);                                              \
  441 } while (0)
  442 
  443 #define LIST_SWAP(head1, head2, type, field) do {                       \
  444         struct type *swap_tmp = LIST_FIRST((head1));                    \
  445         LIST_FIRST((head1)) = LIST_FIRST((head2));                      \
  446         LIST_FIRST((head2)) = swap_tmp;                                 \
  447         if ((swap_tmp = LIST_FIRST((head1))) != NULL)                   \
  448                 swap_tmp->field.le_prev = &LIST_FIRST((head1));         \
  449         if ((swap_tmp = LIST_FIRST((head2))) != NULL)                   \
  450                 swap_tmp->field.le_prev = &LIST_FIRST((head2));         \
  451 } while (0)
  452 
  453 /*
  454  * Tail queue declarations.
  455  */
  456 #define TAILQ_HEAD(name, type)                                          \
  457 struct name {                                                           \
  458         struct type *tqh_first; /* first element */                     \
  459         struct type **tqh_last; /* addr of last next element */         \
  460         TRACEBUF                                                        \
  461 }
  462 
  463 #define TAILQ_HEAD_INITIALIZER(head)                                    \
  464         { NULL, &(head).tqh_first }
  465 
  466 #define TAILQ_ENTRY(type)                                               \
  467 struct {                                                                \
  468         struct type *tqe_next;  /* next element */                      \
  469         struct type **tqe_prev; /* address of previous next element */  \
  470         TRACEBUF                                                        \
  471 }
  472 
  473 /*
  474  * Tail queue functions.
  475  */
  476 #if (defined(_KERNEL) && defined(INVARIANTS))
  477 #define QMD_TAILQ_CHECK_HEAD(head, field) do {                          \
  478         if (!TAILQ_EMPTY(head) &&                                       \
  479             TAILQ_FIRST((head))->field.tqe_prev !=                      \
  480              &TAILQ_FIRST((head)))                                      \
  481                 panic("Bad tailq head %p first->prev != head", (head)); \
  482 } while (0)
  483 
  484 #define QMD_TAILQ_CHECK_TAIL(head, field) do {                          \
  485         if (*(head)->tqh_last != NULL)                                  \
  486                 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head));  \
  487 } while (0)
  488 
  489 #define QMD_TAILQ_CHECK_NEXT(elm, field) do {                           \
  490         if (TAILQ_NEXT((elm), field) != NULL &&                         \
  491             TAILQ_NEXT((elm), field)->field.tqe_prev !=                 \
  492              &((elm)->field.tqe_next))                                  \
  493                 panic("Bad link elm %p next->prev != elm", (elm));      \
  494 } while (0)
  495 
  496 #define QMD_TAILQ_CHECK_PREV(elm, field) do {                           \
  497         if (*(elm)->field.tqe_prev != (elm))                            \
  498                 panic("Bad link elm %p prev->next != elm", (elm));      \
  499 } while (0)
  500 #else
  501 #define QMD_TAILQ_CHECK_HEAD(head, field)
  502 #define QMD_TAILQ_CHECK_TAIL(head, headname)
  503 #define QMD_TAILQ_CHECK_NEXT(elm, field)
  504 #define QMD_TAILQ_CHECK_PREV(elm, field)
  505 #endif /* (_KERNEL && INVARIANTS) */
  506 
  507 #define TAILQ_CONCAT(head1, head2, field) do {                          \
  508         if (!TAILQ_EMPTY(head2)) {                                      \
  509                 *(head1)->tqh_last = (head2)->tqh_first;                \
  510                 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  511                 (head1)->tqh_last = (head2)->tqh_last;                  \
  512                 TAILQ_INIT((head2));                                    \
  513                 QMD_TRACE_HEAD(head1);                                  \
  514                 QMD_TRACE_HEAD(head2);                                  \
  515         }                                                               \
  516 } while (0)
  517 
  518 #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
  519 
  520 #define TAILQ_FIRST(head)       ((head)->tqh_first)
  521 
  522 #define TAILQ_FOREACH(var, head, field)                                 \
  523         for ((var) = TAILQ_FIRST((head));                               \
  524             (var);                                                      \
  525             (var) = TAILQ_NEXT((var), field))
  526 
  527 #define TAILQ_FOREACH_MUTABLE(var, head, field, tvar)                   \
  528         for ((var) = TAILQ_FIRST((head));                               \
  529             (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
  530             (var) = (tvar))
  531 
  532 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
  533         for ((var) = TAILQ_LAST((head), headname);                      \
  534             (var);                                                      \
  535             (var) = TAILQ_PREV((var), headname, field))
  536 
  537 #define TAILQ_FOREACH_REVERSE_MUTABLE(var, head, headname, field, tvar) \
  538         for ((var) = TAILQ_LAST((head), headname);                      \
  539             (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);  \
  540             (var) = (tvar))
  541 
  542 #define TAILQ_INIT(head) do {                                           \
  543         TAILQ_FIRST((head)) = NULL;                                     \
  544         (head)->tqh_last = &TAILQ_FIRST((head));                        \
  545         QMD_TRACE_HEAD(head);                                           \
  546 } while (0)
  547 
  548 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
  549         QMD_TAILQ_CHECK_NEXT(listelm, field);                           \
  550         if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  551                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
  552                     &TAILQ_NEXT((elm), field);                          \
  553         else {                                                          \
  554                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
  555                 QMD_TRACE_HEAD(head);                                   \
  556         }                                                               \
  557         TAILQ_NEXT((listelm), field) = (elm);                           \
  558         (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
  559         QMD_TRACE_ELEM(&(elm)->field);                                  \
  560         QMD_TRACE_ELEM(&listelm->field);                                \
  561 } while (0)
  562 
  563 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
  564         QMD_TAILQ_CHECK_PREV(listelm, field);                           \
  565         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
  566         TAILQ_NEXT((elm), field) = (listelm);                           \
  567         *(listelm)->field.tqe_prev = (elm);                             \
  568         (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
  569         QMD_TRACE_ELEM(&(elm)->field);                                  \
  570         QMD_TRACE_ELEM(&listelm->field);                                \
  571 } while (0)
  572 
  573 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
  574         QMD_TAILQ_CHECK_HEAD(head, field);                              \
  575         if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
  576                 TAILQ_FIRST((head))->field.tqe_prev =                   \
  577                     &TAILQ_NEXT((elm), field);                          \
  578         else                                                            \
  579                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
  580         TAILQ_FIRST((head)) = (elm);                                    \
  581         (elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
  582         QMD_TRACE_HEAD(head);                                           \
  583         QMD_TRACE_ELEM(&(elm)->field);                                  \
  584 } while (0)
  585 
  586 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
  587         QMD_TAILQ_CHECK_TAIL(head, field);                              \
  588         TAILQ_NEXT((elm), field) = NULL;                                \
  589         (elm)->field.tqe_prev = (head)->tqh_last;                       \
  590         *(head)->tqh_last = (elm);                                      \
  591         (head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
  592         QMD_TRACE_HEAD(head);                                           \
  593         QMD_TRACE_ELEM(&(elm)->field);                                  \
  594 } while (0)
  595 
  596 #define TAILQ_LAST(head, headname)                                      \
  597         (*(((struct headname *)((head)->tqh_last))->tqh_last))
  598 
  599 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  600 
  601 #define TAILQ_PREV(elm, headname, field)                                \
  602         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  603 
  604 #define TAILQ_REMOVE(head, elm, field) do {                             \
  605         QMD_SAVELINK(oldnext, (elm)->field.tqe_next);                   \
  606         QMD_SAVELINK(oldprev, (elm)->field.tqe_prev);                   \
  607         QMD_TAILQ_CHECK_NEXT(elm, field);                               \
  608         QMD_TAILQ_CHECK_PREV(elm, field);                               \
  609         if ((TAILQ_NEXT((elm), field)) != NULL)                         \
  610                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
  611                     (elm)->field.tqe_prev;                              \
  612         else {                                                          \
  613                 (head)->tqh_last = (elm)->field.tqe_prev;               \
  614                 QMD_TRACE_HEAD(head);                                   \
  615         }                                                               \
  616         *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
  617         TRASHIT(*oldnext);                                              \
  618         TRASHIT(*oldprev);                                              \
  619         QMD_TRACE_ELEM(&(elm)->field);                                  \
  620 } while (0)
  621 
  622 #define TAILQ_SWAP(head1, head2, type, field) do {                      \
  623         struct type *swap_first = (head1)->tqh_first;                   \
  624         struct type **swap_last = (head1)->tqh_last;                    \
  625         (head1)->tqh_first = (head2)->tqh_first;                        \
  626         (head1)->tqh_last = (head2)->tqh_last;                          \
  627         (head2)->tqh_first = swap_first;                                \
  628         (head2)->tqh_last = swap_last;                                  \
  629         if ((swap_first = (head1)->tqh_first) != NULL)                  \
  630                 swap_first->field.tqe_prev = &(head1)->tqh_first;       \
  631         else                                                            \
  632                 (head1)->tqh_last = &(head1)->tqh_first;                \
  633         if ((swap_first = (head2)->tqh_first) != NULL)                  \
  634                 swap_first->field.tqe_prev = &(head2)->tqh_first;       \
  635         else                                                            \
  636                 (head2)->tqh_last = &(head2)->tqh_first;                \
  637 } while (0)
  638 
  639 #endif /* !_SYS_QUEUE_H_ */

Cache object: 5921335e143e82a210b587f959645484


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.