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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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: stable/9/sys/sys/queue.h 289019 2015-10-08 08:06:23Z hselasky $
   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 be traversed in either 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  * _CLASS_HEAD                  +       +       +       +
   83  * _HEAD_INITIALIZER            +       +       +       +
   84  * _ENTRY                       +       +       +       +
   85  * _CLASS_ENTRY                 +       +       +       +
   86  * _INIT                        +       +       +       +
   87  * _EMPTY                       +       +       +       +
   88  * _FIRST                       +       +       +       +
   89  * _NEXT                        +       +       +       +
   90  * _PREV                        -       +       -       +
   91  * _LAST                        -       -       +       +
   92  * _FOREACH                     +       +       +       +
   93  * _FOREACH_FROM                +       +       +       +
   94  * _FOREACH_SAFE                +       +       +       +
   95  * _FOREACH_FROM_SAFE           +       +       +       +
   96  * _FOREACH_REVERSE             -       -       -       +
   97  * _FOREACH_REVERSE_FROM        -       -       -       +
   98  * _FOREACH_REVERSE_SAFE        -       -       -       +
   99  * _FOREACH_REVERSE_FROM_SAFE   -       -       -       +
  100  * _INSERT_HEAD                 +       +       +       +
  101  * _INSERT_BEFORE               -       +       -       +
  102  * _INSERT_AFTER                +       +       +       +
  103  * _INSERT_TAIL                 -       -       +       +
  104  * _CONCAT                      -       -       +       +
  105  * _REMOVE_AFTER                +       -       +       -
  106  * _REMOVE_HEAD                 +       -       +       -
  107  * _REMOVE                      +       +       +       +
  108  * _SWAP                        +       +       +       +
  109  *
  110  */
  111 #ifdef QUEUE_MACRO_DEBUG
  112 /* Store the last 2 places the queue element or head was altered */
  113 struct qm_trace {
  114         char * lastfile;
  115         int lastline;
  116         char * prevfile;
  117         int prevline;
  118 };
  119 
  120 #define TRACEBUF        struct qm_trace trace;
  121 #define TRASHIT(x)      do {(x) = (void *)-1;} while (0)
  122 #define QMD_SAVELINK(name, link)        void **name = (void *)&(link)
  123 
  124 #define QMD_TRACE_HEAD(head) do {                                       \
  125         (head)->trace.prevline = (head)->trace.lastline;                \
  126         (head)->trace.prevfile = (head)->trace.lastfile;                \
  127         (head)->trace.lastline = __LINE__;                              \
  128         (head)->trace.lastfile = __FILE__;                              \
  129 } while (0)
  130 
  131 #define QMD_TRACE_ELEM(elem) do {                                       \
  132         (elem)->trace.prevline = (elem)->trace.lastline;                \
  133         (elem)->trace.prevfile = (elem)->trace.lastfile;                \
  134         (elem)->trace.lastline = __LINE__;                              \
  135         (elem)->trace.lastfile = __FILE__;                              \
  136 } while (0)
  137 
  138 #else
  139 #define QMD_TRACE_ELEM(elem)
  140 #define QMD_TRACE_HEAD(head)
  141 #define QMD_SAVELINK(name, link)
  142 #define TRACEBUF
  143 #define TRASHIT(x)
  144 #endif  /* QUEUE_MACRO_DEBUG */
  145 
  146 #ifdef __cplusplus
  147 /*
  148  * In C++ there can be structure lists and class lists:
  149  */
  150 #define QUEUE_TYPEOF(type) type
  151 #else
  152 #define QUEUE_TYPEOF(type) struct type
  153 #endif
  154 
  155 /*
  156  * Singly-linked List declarations.
  157  */
  158 #define SLIST_HEAD(name, type)                                          \
  159 struct name {                                                           \
  160         struct type *slh_first; /* first element */                     \
  161 }
  162 
  163 #define SLIST_CLASS_HEAD(name, type)                                    \
  164 struct name {                                                           \
  165         class type *slh_first;  /* first element */                     \
  166 }
  167 
  168 #define SLIST_HEAD_INITIALIZER(head)                                    \
  169         { NULL }
  170 
  171 #define SLIST_ENTRY(type)                                               \
  172 struct {                                                                \
  173         struct type *sle_next;  /* next element */                      \
  174 }
  175 
  176 #define SLIST_CLASS_ENTRY(type)                                         \
  177 struct {                                                                \
  178         class type *sle_next;           /* next element */              \
  179 }
  180 
  181 /*
  182  * Singly-linked List functions.
  183  */
  184 #define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
  185 
  186 #define SLIST_FIRST(head)       ((head)->slh_first)
  187 
  188 #define SLIST_FOREACH(var, head, field)                                 \
  189         for ((var) = SLIST_FIRST((head));                               \
  190             (var);                                                      \
  191             (var) = SLIST_NEXT((var), field))
  192 
  193 #define SLIST_FOREACH_FROM(var, head, field)                            \
  194         for ((var) = ((var) ? (var) : SLIST_FIRST((head)));             \
  195             (var);                                                      \
  196             (var) = SLIST_NEXT((var), field))
  197 
  198 #define SLIST_FOREACH_SAFE(var, head, field, tvar)                      \
  199         for ((var) = SLIST_FIRST((head));                               \
  200             (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
  201             (var) = (tvar))
  202 
  203 #define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar)                 \
  204         for ((var) = ((var) ? (var) : SLIST_FIRST((head)));             \
  205             (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
  206             (var) = (tvar))
  207 
  208 #define SLIST_FOREACH_PREVPTR(var, varp, head, field)                   \
  209         for ((varp) = &SLIST_FIRST((head));                             \
  210             ((var) = *(varp)) != NULL;                                  \
  211             (varp) = &SLIST_NEXT((var), field))
  212 
  213 #define SLIST_INIT(head) do {                                           \
  214         SLIST_FIRST((head)) = NULL;                                     \
  215 } while (0)
  216 
  217 #define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
  218         SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);       \
  219         SLIST_NEXT((slistelm), field) = (elm);                          \
  220 } while (0)
  221 
  222 #define SLIST_INSERT_HEAD(head, elm, field) do {                        \
  223         SLIST_NEXT((elm), field) = SLIST_FIRST((head));                 \
  224         SLIST_FIRST((head)) = (elm);                                    \
  225 } while (0)
  226 
  227 #define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
  228 
  229 #define SLIST_REMOVE(head, elm, type, field) do {                       \
  230         QMD_SAVELINK(oldnext, (elm)->field.sle_next);                   \
  231         if (SLIST_FIRST((head)) == (elm)) {                             \
  232                 SLIST_REMOVE_HEAD((head), field);                       \
  233         }                                                               \
  234         else {                                                          \
  235                 QUEUE_TYPEOF(type) *curelm = SLIST_FIRST(head);         \
  236                 while (SLIST_NEXT(curelm, field) != (elm))              \
  237                         curelm = SLIST_NEXT(curelm, field);             \
  238                 SLIST_REMOVE_AFTER(curelm, field);                      \
  239         }                                                               \
  240         TRASHIT(*oldnext);                                              \
  241 } while (0)
  242 
  243 #define SLIST_REMOVE_AFTER(elm, field) do {                             \
  244         SLIST_NEXT(elm, field) =                                        \
  245             SLIST_NEXT(SLIST_NEXT(elm, field), field);                  \
  246 } while (0)
  247 
  248 #define SLIST_REMOVE_HEAD(head, field) do {                             \
  249         SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);   \
  250 } while (0)
  251 
  252 #define SLIST_SWAP(head1, head2, type) do {                             \
  253         QUEUE_TYPEOF(type) *swap_first = SLIST_FIRST(head1);            \
  254         SLIST_FIRST(head1) = SLIST_FIRST(head2);                        \
  255         SLIST_FIRST(head2) = swap_first;                                \
  256 } while (0)
  257 
  258 /*
  259  * Singly-linked Tail queue declarations.
  260  */
  261 #define STAILQ_HEAD(name, type)                                         \
  262 struct name {                                                           \
  263         struct type *stqh_first;/* first element */                     \
  264         struct type **stqh_last;/* addr of last next element */         \
  265 }
  266 
  267 #define STAILQ_CLASS_HEAD(name, type)                                   \
  268 struct name {                                                           \
  269         class type *stqh_first; /* first element */                     \
  270         class type **stqh_last; /* addr of last next element */         \
  271 }
  272 
  273 #define STAILQ_HEAD_INITIALIZER(head)                                   \
  274         { NULL, &(head).stqh_first }
  275 
  276 #define STAILQ_ENTRY(type)                                              \
  277 struct {                                                                \
  278         struct type *stqe_next; /* next element */                      \
  279 }
  280 
  281 #define STAILQ_CLASS_ENTRY(type)                                        \
  282 struct {                                                                \
  283         class type *stqe_next;  /* next element */                      \
  284 }
  285 
  286 /*
  287  * Singly-linked Tail queue functions.
  288  */
  289 #define STAILQ_CONCAT(head1, head2) do {                                \
  290         if (!STAILQ_EMPTY((head2))) {                                   \
  291                 *(head1)->stqh_last = (head2)->stqh_first;              \
  292                 (head1)->stqh_last = (head2)->stqh_last;                \
  293                 STAILQ_INIT((head2));                                   \
  294         }                                                               \
  295 } while (0)
  296 
  297 #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
  298 
  299 #define STAILQ_FIRST(head)      ((head)->stqh_first)
  300 
  301 #define STAILQ_FOREACH(var, head, field)                                \
  302         for((var) = STAILQ_FIRST((head));                               \
  303            (var);                                                       \
  304            (var) = STAILQ_NEXT((var), field))
  305 
  306 #define STAILQ_FOREACH_FROM(var, head, field)                           \
  307         for ((var) = ((var) ? (var) : STAILQ_FIRST((head)));            \
  308            (var);                                                       \
  309            (var) = STAILQ_NEXT((var), field))
  310 
  311 #define STAILQ_FOREACH_SAFE(var, head, field, tvar)                     \
  312         for ((var) = STAILQ_FIRST((head));                              \
  313             (var) && ((tvar) = STAILQ_NEXT((var), field), 1);           \
  314             (var) = (tvar))
  315 
  316 #define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar)                \
  317         for ((var) = ((var) ? (var) : STAILQ_FIRST((head)));            \
  318             (var) && ((tvar) = STAILQ_NEXT((var), field), 1);           \
  319             (var) = (tvar))
  320 
  321 #define STAILQ_INIT(head) do {                                          \
  322         STAILQ_FIRST((head)) = NULL;                                    \
  323         (head)->stqh_last = &STAILQ_FIRST((head));                      \
  324 } while (0)
  325 
  326 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
  327         if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  328                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
  329         STAILQ_NEXT((tqelm), field) = (elm);                            \
  330 } while (0)
  331 
  332 #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
  333         if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  334                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
  335         STAILQ_FIRST((head)) = (elm);                                   \
  336 } while (0)
  337 
  338 #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
  339         STAILQ_NEXT((elm), field) = NULL;                               \
  340         *(head)->stqh_last = (elm);                                     \
  341         (head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
  342 } while (0)
  343 
  344 #define STAILQ_LAST(head, type, field)                          \
  345         (STAILQ_EMPTY((head)) ? NULL :                          \
  346             __containerof((head)->stqh_last,                    \
  347             QUEUE_TYPEOF(type), field.stqe_next))
  348 
  349 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  350 
  351 #define STAILQ_REMOVE(head, elm, type, field) do {                      \
  352         QMD_SAVELINK(oldnext, (elm)->field.stqe_next);                  \
  353         if (STAILQ_FIRST((head)) == (elm)) {                            \
  354                 STAILQ_REMOVE_HEAD((head), field);                      \
  355         }                                                               \
  356         else {                                                          \
  357                 QUEUE_TYPEOF(type) *curelm = STAILQ_FIRST(head);        \
  358                 while (STAILQ_NEXT(curelm, field) != (elm))             \
  359                         curelm = STAILQ_NEXT(curelm, field);            \
  360                 STAILQ_REMOVE_AFTER(head, curelm, field);               \
  361         }                                                               \
  362         TRASHIT(*oldnext);                                              \
  363 } while (0)
  364 
  365 #define STAILQ_REMOVE_AFTER(head, elm, field) do {                      \
  366         if ((STAILQ_NEXT(elm, field) =                                  \
  367              STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL)      \
  368                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
  369 } while (0)
  370 
  371 #define STAILQ_REMOVE_HEAD(head, field) do {                            \
  372         if ((STAILQ_FIRST((head)) =                                     \
  373              STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)         \
  374                 (head)->stqh_last = &STAILQ_FIRST((head));              \
  375 } while (0)
  376 
  377 #define STAILQ_SWAP(head1, head2, type) do {                            \
  378         QUEUE_TYPEOF(type) *swap_first = STAILQ_FIRST(head1);           \
  379         QUEUE_TYPEOF(type) **swap_last = (head1)->stqh_last;            \
  380         STAILQ_FIRST(head1) = STAILQ_FIRST(head2);                      \
  381         (head1)->stqh_last = (head2)->stqh_last;                        \
  382         STAILQ_FIRST(head2) = swap_first;                               \
  383         (head2)->stqh_last = swap_last;                                 \
  384         if (STAILQ_EMPTY(head1))                                        \
  385                 (head1)->stqh_last = &STAILQ_FIRST(head1);              \
  386         if (STAILQ_EMPTY(head2))                                        \
  387                 (head2)->stqh_last = &STAILQ_FIRST(head2);              \
  388 } while (0)
  389 
  390 
  391 /*
  392  * List declarations.
  393  */
  394 #define LIST_HEAD(name, type)                                           \
  395 struct name {                                                           \
  396         struct type *lh_first;  /* first element */                     \
  397 }
  398 
  399 #define LIST_CLASS_HEAD(name, type)                                     \
  400 struct name {                                                           \
  401         class type *lh_first;   /* first element */                     \
  402 }
  403 
  404 #define LIST_HEAD_INITIALIZER(head)                                     \
  405         { NULL }
  406 
  407 #define LIST_ENTRY(type)                                                \
  408 struct {                                                                \
  409         struct type *le_next;   /* next element */                      \
  410         struct type **le_prev;  /* address of previous next element */  \
  411 }
  412 
  413 #define LIST_CLASS_ENTRY(type)                                          \
  414 struct {                                                                \
  415         class type *le_next;    /* next element */                      \
  416         class type **le_prev;   /* address of previous next element */  \
  417 }
  418 
  419 /*
  420  * List functions.
  421  */
  422 
  423 #if (defined(_KERNEL) && defined(INVARIANTS))
  424 #define QMD_LIST_CHECK_HEAD(head, field) do {                           \
  425         if (LIST_FIRST((head)) != NULL &&                               \
  426             LIST_FIRST((head))->field.le_prev !=                        \
  427              &LIST_FIRST((head)))                                       \
  428                 panic("Bad list head %p first->prev != head", (head));  \
  429 } while (0)
  430 
  431 #define QMD_LIST_CHECK_NEXT(elm, field) do {                            \
  432         if (LIST_NEXT((elm), field) != NULL &&                          \
  433             LIST_NEXT((elm), field)->field.le_prev !=                   \
  434              &((elm)->field.le_next))                                   \
  435                 panic("Bad link elm %p next->prev != elm", (elm));      \
  436 } while (0)
  437 
  438 #define QMD_LIST_CHECK_PREV(elm, field) do {                            \
  439         if (*(elm)->field.le_prev != (elm))                             \
  440                 panic("Bad link elm %p prev->next != elm", (elm));      \
  441 } while (0)
  442 #else
  443 #define QMD_LIST_CHECK_HEAD(head, field)
  444 #define QMD_LIST_CHECK_NEXT(elm, field)
  445 #define QMD_LIST_CHECK_PREV(elm, field)
  446 #endif /* (_KERNEL && INVARIANTS) */
  447 
  448 #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
  449 
  450 #define LIST_FIRST(head)        ((head)->lh_first)
  451 
  452 #define LIST_FOREACH(var, head, field)                                  \
  453         for ((var) = LIST_FIRST((head));                                \
  454             (var);                                                      \
  455             (var) = LIST_NEXT((var), field))
  456 
  457 #define LIST_FOREACH_FROM(var, head, field)                             \
  458         for ((var) = ((var) ? (var) : LIST_FIRST((head)));              \
  459             (var);                                                      \
  460             (var) = LIST_NEXT((var), field))
  461 
  462 #define LIST_FOREACH_SAFE(var, head, field, tvar)                       \
  463         for ((var) = LIST_FIRST((head));                                \
  464             (var) && ((tvar) = LIST_NEXT((var), field), 1);             \
  465             (var) = (tvar))
  466 
  467 #define LIST_FOREACH_FROM_SAFE(var, head, field, tvar)                  \
  468         for ((var) = ((var) ? (var) : LIST_FIRST((head)));              \
  469             (var) && ((tvar) = LIST_NEXT((var), field), 1);             \
  470             (var) = (tvar))
  471 
  472 #define LIST_INIT(head) do {                                            \
  473         LIST_FIRST((head)) = NULL;                                      \
  474 } while (0)
  475 
  476 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
  477         QMD_LIST_CHECK_NEXT(listelm, field);                            \
  478         if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  479                 LIST_NEXT((listelm), field)->field.le_prev =            \
  480                     &LIST_NEXT((elm), field);                           \
  481         LIST_NEXT((listelm), field) = (elm);                            \
  482         (elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
  483 } while (0)
  484 
  485 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
  486         QMD_LIST_CHECK_PREV(listelm, field);                            \
  487         (elm)->field.le_prev = (listelm)->field.le_prev;                \
  488         LIST_NEXT((elm), field) = (listelm);                            \
  489         *(listelm)->field.le_prev = (elm);                              \
  490         (listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
  491 } while (0)
  492 
  493 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
  494         QMD_LIST_CHECK_HEAD((head), field);                             \
  495         if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
  496                 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  497         LIST_FIRST((head)) = (elm);                                     \
  498         (elm)->field.le_prev = &LIST_FIRST((head));                     \
  499 } while (0)
  500 
  501 #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
  502 
  503 #define LIST_PREV(elm, head, type, field)                       \
  504         ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL :   \
  505             __containerof((elm)->field.le_prev,                 \
  506             QUEUE_TYPEOF(type), field.le_next))
  507 
  508 #define LIST_REMOVE(elm, field) do {                                    \
  509         QMD_SAVELINK(oldnext, (elm)->field.le_next);                    \
  510         QMD_SAVELINK(oldprev, (elm)->field.le_prev);                    \
  511         QMD_LIST_CHECK_NEXT(elm, field);                                \
  512         QMD_LIST_CHECK_PREV(elm, field);                                \
  513         if (LIST_NEXT((elm), field) != NULL)                            \
  514                 LIST_NEXT((elm), field)->field.le_prev =                \
  515                     (elm)->field.le_prev;                               \
  516         *(elm)->field.le_prev = LIST_NEXT((elm), field);                \
  517         TRASHIT(*oldnext);                                              \
  518         TRASHIT(*oldprev);                                              \
  519 } while (0)
  520 
  521 #define LIST_SWAP(head1, head2, type, field) do {                       \
  522         QUEUE_TYPEOF(type) *swap_tmp = LIST_FIRST(head1);               \
  523         LIST_FIRST((head1)) = LIST_FIRST((head2));                      \
  524         LIST_FIRST((head2)) = swap_tmp;                                 \
  525         if ((swap_tmp = LIST_FIRST((head1))) != NULL)                   \
  526                 swap_tmp->field.le_prev = &LIST_FIRST((head1));         \
  527         if ((swap_tmp = LIST_FIRST((head2))) != NULL)                   \
  528                 swap_tmp->field.le_prev = &LIST_FIRST((head2));         \
  529 } while (0)
  530 
  531 /*
  532  * Tail queue declarations.
  533  */
  534 #define TAILQ_HEAD(name, type)                                          \
  535 struct name {                                                           \
  536         struct type *tqh_first; /* first element */                     \
  537         struct type **tqh_last; /* addr of last next element */         \
  538         TRACEBUF                                                        \
  539 }
  540 
  541 #define TAILQ_CLASS_HEAD(name, type)                                    \
  542 struct name {                                                           \
  543         class type *tqh_first;  /* first element */                     \
  544         class type **tqh_last;  /* addr of last next element */         \
  545         TRACEBUF                                                        \
  546 }
  547 
  548 #define TAILQ_HEAD_INITIALIZER(head)                                    \
  549         { NULL, &(head).tqh_first }
  550 
  551 #define TAILQ_ENTRY(type)                                               \
  552 struct {                                                                \
  553         struct type *tqe_next;  /* next element */                      \
  554         struct type **tqe_prev; /* address of previous next element */  \
  555         TRACEBUF                                                        \
  556 }
  557 
  558 #define TAILQ_CLASS_ENTRY(type)                                         \
  559 struct {                                                                \
  560         class type *tqe_next;   /* next element */                      \
  561         class type **tqe_prev;  /* address of previous next element */  \
  562         TRACEBUF                                                        \
  563 }
  564 
  565 /*
  566  * Tail queue functions.
  567  */
  568 #if (defined(_KERNEL) && defined(INVARIANTS))
  569 #define QMD_TAILQ_CHECK_HEAD(head, field) do {                          \
  570         if (!TAILQ_EMPTY(head) &&                                       \
  571             TAILQ_FIRST((head))->field.tqe_prev !=                      \
  572              &TAILQ_FIRST((head)))                                      \
  573                 panic("Bad tailq head %p first->prev != head", (head)); \
  574 } while (0)
  575 
  576 #define QMD_TAILQ_CHECK_TAIL(head, field) do {                          \
  577         if (*(head)->tqh_last != NULL)                                  \
  578                 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head));  \
  579 } while (0)
  580 
  581 #define QMD_TAILQ_CHECK_NEXT(elm, field) do {                           \
  582         if (TAILQ_NEXT((elm), field) != NULL &&                         \
  583             TAILQ_NEXT((elm), field)->field.tqe_prev !=                 \
  584              &((elm)->field.tqe_next))                                  \
  585                 panic("Bad link elm %p next->prev != elm", (elm));      \
  586 } while (0)
  587 
  588 #define QMD_TAILQ_CHECK_PREV(elm, field) do {                           \
  589         if (*(elm)->field.tqe_prev != (elm))                            \
  590                 panic("Bad link elm %p prev->next != elm", (elm));      \
  591 } while (0)
  592 #else
  593 #define QMD_TAILQ_CHECK_HEAD(head, field)
  594 #define QMD_TAILQ_CHECK_TAIL(head, headname)
  595 #define QMD_TAILQ_CHECK_NEXT(elm, field)
  596 #define QMD_TAILQ_CHECK_PREV(elm, field)
  597 #endif /* (_KERNEL && INVARIANTS) */
  598 
  599 #define TAILQ_CONCAT(head1, head2, field) do {                          \
  600         if (!TAILQ_EMPTY(head2)) {                                      \
  601                 *(head1)->tqh_last = (head2)->tqh_first;                \
  602                 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  603                 (head1)->tqh_last = (head2)->tqh_last;                  \
  604                 TAILQ_INIT((head2));                                    \
  605                 QMD_TRACE_HEAD(head1);                                  \
  606                 QMD_TRACE_HEAD(head2);                                  \
  607         }                                                               \
  608 } while (0)
  609 
  610 #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
  611 
  612 #define TAILQ_FIRST(head)       ((head)->tqh_first)
  613 
  614 #define TAILQ_FOREACH(var, head, field)                                 \
  615         for ((var) = TAILQ_FIRST((head));                               \
  616             (var);                                                      \
  617             (var) = TAILQ_NEXT((var), field))
  618 
  619 #define TAILQ_FOREACH_FROM(var, head, field)                            \
  620         for ((var) = ((var) ? (var) : TAILQ_FIRST((head)));             \
  621             (var);                                                      \
  622             (var) = TAILQ_NEXT((var), field))
  623 
  624 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
  625         for ((var) = TAILQ_FIRST((head));                               \
  626             (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
  627             (var) = (tvar))
  628 
  629 #define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar)                 \
  630         for ((var) = ((var) ? (var) : TAILQ_FIRST((head)));             \
  631             (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
  632             (var) = (tvar))
  633 
  634 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
  635         for ((var) = TAILQ_LAST((head), headname);                      \
  636             (var);                                                      \
  637             (var) = TAILQ_PREV((var), headname, field))
  638 
  639 #define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field)          \
  640         for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname));    \
  641             (var);                                                      \
  642             (var) = TAILQ_PREV((var), headname, field))
  643 
  644 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)    \
  645         for ((var) = TAILQ_LAST((head), headname);                      \
  646             (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);  \
  647             (var) = (tvar))
  648 
  649 #define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \
  650         for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname));    \
  651             (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);  \
  652             (var) = (tvar))
  653 
  654 #define TAILQ_INIT(head) do {                                           \
  655         TAILQ_FIRST((head)) = NULL;                                     \
  656         (head)->tqh_last = &TAILQ_FIRST((head));                        \
  657         QMD_TRACE_HEAD(head);                                           \
  658 } while (0)
  659 
  660 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
  661         QMD_TAILQ_CHECK_NEXT(listelm, field);                           \
  662         if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  663                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
  664                     &TAILQ_NEXT((elm), field);                          \
  665         else {                                                          \
  666                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
  667                 QMD_TRACE_HEAD(head);                                   \
  668         }                                                               \
  669         TAILQ_NEXT((listelm), field) = (elm);                           \
  670         (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
  671         QMD_TRACE_ELEM(&(elm)->field);                                  \
  672         QMD_TRACE_ELEM(&(listelm)->field);                              \
  673 } while (0)
  674 
  675 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
  676         QMD_TAILQ_CHECK_PREV(listelm, field);                           \
  677         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
  678         TAILQ_NEXT((elm), field) = (listelm);                           \
  679         *(listelm)->field.tqe_prev = (elm);                             \
  680         (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
  681         QMD_TRACE_ELEM(&(elm)->field);                                  \
  682         QMD_TRACE_ELEM(&(listelm)->field);                              \
  683 } while (0)
  684 
  685 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
  686         QMD_TAILQ_CHECK_HEAD(head, field);                              \
  687         if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
  688                 TAILQ_FIRST((head))->field.tqe_prev =                   \
  689                     &TAILQ_NEXT((elm), field);                          \
  690         else                                                            \
  691                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
  692         TAILQ_FIRST((head)) = (elm);                                    \
  693         (elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
  694         QMD_TRACE_HEAD(head);                                           \
  695         QMD_TRACE_ELEM(&(elm)->field);                                  \
  696 } while (0)
  697 
  698 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
  699         QMD_TAILQ_CHECK_TAIL(head, field);                              \
  700         TAILQ_NEXT((elm), field) = NULL;                                \
  701         (elm)->field.tqe_prev = (head)->tqh_last;                       \
  702         *(head)->tqh_last = (elm);                                      \
  703         (head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
  704         QMD_TRACE_HEAD(head);                                           \
  705         QMD_TRACE_ELEM(&(elm)->field);                                  \
  706 } while (0)
  707 
  708 #define TAILQ_LAST(head, headname)                                      \
  709         (*(((struct headname *)((head)->tqh_last))->tqh_last))
  710 
  711 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  712 
  713 #define TAILQ_PREV(elm, headname, field)                                \
  714         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  715 
  716 #define TAILQ_REMOVE(head, elm, field) do {                             \
  717         QMD_SAVELINK(oldnext, (elm)->field.tqe_next);                   \
  718         QMD_SAVELINK(oldprev, (elm)->field.tqe_prev);                   \
  719         QMD_TAILQ_CHECK_NEXT(elm, field);                               \
  720         QMD_TAILQ_CHECK_PREV(elm, field);                               \
  721         if ((TAILQ_NEXT((elm), field)) != NULL)                         \
  722                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
  723                     (elm)->field.tqe_prev;                              \
  724         else {                                                          \
  725                 (head)->tqh_last = (elm)->field.tqe_prev;               \
  726                 QMD_TRACE_HEAD(head);                                   \
  727         }                                                               \
  728         *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
  729         TRASHIT(*oldnext);                                              \
  730         TRASHIT(*oldprev);                                              \
  731         QMD_TRACE_ELEM(&(elm)->field);                                  \
  732 } while (0)
  733 
  734 #define TAILQ_SWAP(head1, head2, type, field) do {                      \
  735         QUEUE_TYPEOF(type) *swap_first = (head1)->tqh_first;            \
  736         QUEUE_TYPEOF(type) **swap_last = (head1)->tqh_last;             \
  737         (head1)->tqh_first = (head2)->tqh_first;                        \
  738         (head1)->tqh_last = (head2)->tqh_last;                          \
  739         (head2)->tqh_first = swap_first;                                \
  740         (head2)->tqh_last = swap_last;                                  \
  741         if ((swap_first = (head1)->tqh_first) != NULL)                  \
  742                 swap_first->field.tqe_prev = &(head1)->tqh_first;       \
  743         else                                                            \
  744                 (head1)->tqh_last = &(head1)->tqh_first;                \
  745         if ((swap_first = (head2)->tqh_first) != NULL)                  \
  746                 swap_first->field.tqe_prev = &(head2)->tqh_first;       \
  747         else                                                            \
  748                 (head2)->tqh_last = &(head2)->tqh_first;                \
  749 } while (0)
  750 
  751 #endif /* !_SYS_QUEUE_H_ */

Cache object: cc2bd583bb1fa7b6820fec4bb51badc9


[ 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.