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/7/sys/sys/queue.h 222262 2011-05-24 16:06:26Z mdf $
   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_SAFE                +       +       +       +
   92  * _FOREACH_REVERSE             -       -       -       +
   93  * _FOREACH_REVERSE_SAFE        -       -       -       +
   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 /*
  157  * Singly-linked List functions.
  158  */
  159 #define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
  160 
  161 #define SLIST_FIRST(head)       ((head)->slh_first)
  162 
  163 #define SLIST_FOREACH(var, head, field)                                 \
  164         for ((var) = SLIST_FIRST((head));                               \
  165             (var);                                                      \
  166             (var) = SLIST_NEXT((var), field))
  167 
  168 #define SLIST_FOREACH_SAFE(var, head, field, tvar)                      \
  169         for ((var) = SLIST_FIRST((head));                               \
  170             (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
  171             (var) = (tvar))
  172 
  173 #define SLIST_FOREACH_PREVPTR(var, varp, head, field)                   \
  174         for ((varp) = &SLIST_FIRST((head));                             \
  175             ((var) = *(varp)) != NULL;                                  \
  176             (varp) = &SLIST_NEXT((var), field))
  177 
  178 #define SLIST_INIT(head) do {                                           \
  179         SLIST_FIRST((head)) = NULL;                                     \
  180 } while (0)
  181 
  182 #define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
  183         SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);       \
  184         SLIST_NEXT((slistelm), field) = (elm);                          \
  185 } while (0)
  186 
  187 #define SLIST_INSERT_HEAD(head, elm, field) do {                        \
  188         SLIST_NEXT((elm), field) = SLIST_FIRST((head));                 \
  189         SLIST_FIRST((head)) = (elm);                                    \
  190 } while (0)
  191 
  192 #define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
  193 
  194 #define SLIST_REMOVE(head, elm, type, field) do {                       \
  195         QMD_SAVELINK(oldnext, (elm)->field.sle_next);                   \
  196         if (SLIST_FIRST((head)) == (elm)) {                             \
  197                 SLIST_REMOVE_HEAD((head), field);                       \
  198         }                                                               \
  199         else {                                                          \
  200                 struct type *curelm = SLIST_FIRST((head));              \
  201                 while (SLIST_NEXT(curelm, field) != (elm))              \
  202                         curelm = SLIST_NEXT(curelm, field);             \
  203                 SLIST_REMOVE_AFTER(curelm, field);                      \
  204         }                                                               \
  205         TRASHIT(*oldnext);                                              \
  206 } while (0)
  207 
  208 #define SLIST_REMOVE_AFTER(elm, field) do {                             \
  209         SLIST_NEXT(elm, field) =                                        \
  210             SLIST_NEXT(SLIST_NEXT(elm, field), field);                  \
  211 } while (0)
  212 
  213 #define SLIST_REMOVE_HEAD(head, field) do {                             \
  214         SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);   \
  215 } while (0)
  216 
  217 /*
  218  * Singly-linked Tail queue declarations.
  219  */
  220 #define STAILQ_HEAD(name, type)                                         \
  221 struct name {                                                           \
  222         struct type *stqh_first;/* first element */                     \
  223         struct type **stqh_last;/* addr of last next element */         \
  224 }
  225 
  226 #define STAILQ_HEAD_INITIALIZER(head)                                   \
  227         { NULL, &(head).stqh_first }
  228 
  229 #define STAILQ_ENTRY(type)                                              \
  230 struct {                                                                \
  231         struct type *stqe_next; /* next element */                      \
  232 }
  233 
  234 /*
  235  * Singly-linked Tail queue functions.
  236  */
  237 #define STAILQ_CONCAT(head1, head2) do {                                \
  238         if (!STAILQ_EMPTY((head2))) {                                   \
  239                 *(head1)->stqh_last = (head2)->stqh_first;              \
  240                 (head1)->stqh_last = (head2)->stqh_last;                \
  241                 STAILQ_INIT((head2));                                   \
  242         }                                                               \
  243 } while (0)
  244 
  245 #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
  246 
  247 #define STAILQ_FIRST(head)      ((head)->stqh_first)
  248 
  249 #define STAILQ_FOREACH(var, head, field)                                \
  250         for((var) = STAILQ_FIRST((head));                               \
  251            (var);                                                       \
  252            (var) = STAILQ_NEXT((var), field))
  253 
  254 
  255 #define STAILQ_FOREACH_SAFE(var, head, field, tvar)                     \
  256         for ((var) = STAILQ_FIRST((head));                              \
  257             (var) && ((tvar) = STAILQ_NEXT((var), field), 1);           \
  258             (var) = (tvar))
  259 
  260 #define STAILQ_INIT(head) do {                                          \
  261         STAILQ_FIRST((head)) = NULL;                                    \
  262         (head)->stqh_last = &STAILQ_FIRST((head));                      \
  263 } while (0)
  264 
  265 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
  266         if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
  267                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
  268         STAILQ_NEXT((tqelm), field) = (elm);                            \
  269 } while (0)
  270 
  271 #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
  272         if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
  273                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
  274         STAILQ_FIRST((head)) = (elm);                                   \
  275 } while (0)
  276 
  277 #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
  278         STAILQ_NEXT((elm), field) = NULL;                               \
  279         *(head)->stqh_last = (elm);                                     \
  280         (head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
  281 } while (0)
  282 
  283 #define STAILQ_LAST(head, type, field)                                  \
  284         (STAILQ_EMPTY((head)) ?                                         \
  285                 NULL :                                                  \
  286                 ((struct type *)(void *)                                \
  287                 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
  288 
  289 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
  290 
  291 #define STAILQ_REMOVE(head, elm, type, field) do {                      \
  292         QMD_SAVELINK(oldnext, (elm)->field.stqe_next);                  \
  293         if (STAILQ_FIRST((head)) == (elm)) {                            \
  294                 STAILQ_REMOVE_HEAD((head), field);                      \
  295         }                                                               \
  296         else {                                                          \
  297                 struct type *curelm = STAILQ_FIRST((head));             \
  298                 while (STAILQ_NEXT(curelm, field) != (elm))             \
  299                         curelm = STAILQ_NEXT(curelm, field);            \
  300                 STAILQ_REMOVE_AFTER(head, curelm, field);               \
  301         }                                                               \
  302         TRASHIT(*oldnext);                                              \
  303 } while (0)
  304 
  305 #define STAILQ_REMOVE_AFTER(head, elm, field) do {                      \
  306         if ((STAILQ_NEXT(elm, field) =                                  \
  307              STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL)      \
  308                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
  309 } while (0)
  310 
  311 #define STAILQ_REMOVE_HEAD(head, field) do {                            \
  312         if ((STAILQ_FIRST((head)) =                                     \
  313              STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)         \
  314                 (head)->stqh_last = &STAILQ_FIRST((head));              \
  315 } while (0)
  316 
  317 #define STAILQ_SWAP(head1, head2, type) do {                            \
  318         struct type *swap_first = STAILQ_FIRST(head1);                  \
  319         struct type **swap_last = (head1)->stqh_last;                   \
  320         STAILQ_FIRST(head1) = STAILQ_FIRST(head2);                      \
  321         (head1)->stqh_last = (head2)->stqh_last;                        \
  322         STAILQ_FIRST(head2) = swap_first;                               \
  323         (head2)->stqh_last = swap_last;                                 \
  324         if (STAILQ_EMPTY(head1))                                        \
  325                 (head1)->stqh_last = &STAILQ_FIRST(head1);              \
  326         if (STAILQ_EMPTY(head2))                                        \
  327                 (head2)->stqh_last = &STAILQ_FIRST(head2);              \
  328 } while (0)
  329 
  330 
  331 /*
  332  * List declarations.
  333  */
  334 #define LIST_HEAD(name, type)                                           \
  335 struct name {                                                           \
  336         struct type *lh_first;  /* first element */                     \
  337 }
  338 
  339 #define LIST_HEAD_INITIALIZER(head)                                     \
  340         { NULL }
  341 
  342 #define LIST_ENTRY(type)                                                \
  343 struct {                                                                \
  344         struct type *le_next;   /* next element */                      \
  345         struct type **le_prev;  /* address of previous next element */  \
  346 }
  347 
  348 /*
  349  * List functions.
  350  */
  351 
  352 #if (defined(_KERNEL) && defined(INVARIANTS))
  353 #define QMD_LIST_CHECK_HEAD(head, field) do {                           \
  354         if (LIST_FIRST((head)) != NULL &&                               \
  355             LIST_FIRST((head))->field.le_prev !=                        \
  356              &LIST_FIRST((head)))                                       \
  357                 panic("Bad list head %p first->prev != head", (head));  \
  358 } while (0)
  359 
  360 #define QMD_LIST_CHECK_NEXT(elm, field) do {                            \
  361         if (LIST_NEXT((elm), field) != NULL &&                          \
  362             LIST_NEXT((elm), field)->field.le_prev !=                   \
  363              &((elm)->field.le_next))                                   \
  364                 panic("Bad link elm %p next->prev != elm", (elm));      \
  365 } while (0)
  366 
  367 #define QMD_LIST_CHECK_PREV(elm, field) do {                            \
  368         if (*(elm)->field.le_prev != (elm))                             \
  369                 panic("Bad link elm %p prev->next != elm", (elm));      \
  370 } while (0)
  371 #else
  372 #define QMD_LIST_CHECK_HEAD(head, field)
  373 #define QMD_LIST_CHECK_NEXT(elm, field)
  374 #define QMD_LIST_CHECK_PREV(elm, field)
  375 #endif /* (_KERNEL && INVARIANTS) */
  376 
  377 #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
  378 
  379 #define LIST_FIRST(head)        ((head)->lh_first)
  380 
  381 #define LIST_FOREACH(var, head, field)                                  \
  382         for ((var) = LIST_FIRST((head));                                \
  383             (var);                                                      \
  384             (var) = LIST_NEXT((var), field))
  385 
  386 #define LIST_FOREACH_SAFE(var, head, field, tvar)                       \
  387         for ((var) = LIST_FIRST((head));                                \
  388             (var) && ((tvar) = LIST_NEXT((var), field), 1);             \
  389             (var) = (tvar))
  390 
  391 #define LIST_INIT(head) do {                                            \
  392         LIST_FIRST((head)) = NULL;                                      \
  393 } while (0)
  394 
  395 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
  396         QMD_LIST_CHECK_NEXT(listelm, field);                            \
  397         if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
  398                 LIST_NEXT((listelm), field)->field.le_prev =            \
  399                     &LIST_NEXT((elm), field);                           \
  400         LIST_NEXT((listelm), field) = (elm);                            \
  401         (elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
  402 } while (0)
  403 
  404 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
  405         QMD_LIST_CHECK_PREV(listelm, field);                            \
  406         (elm)->field.le_prev = (listelm)->field.le_prev;                \
  407         LIST_NEXT((elm), field) = (listelm);                            \
  408         *(listelm)->field.le_prev = (elm);                              \
  409         (listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
  410 } while (0)
  411 
  412 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
  413         QMD_LIST_CHECK_HEAD((head), field);                             \
  414         if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
  415                 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
  416         LIST_FIRST((head)) = (elm);                                     \
  417         (elm)->field.le_prev = &LIST_FIRST((head));                     \
  418 } while (0)
  419 
  420 #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
  421 
  422 #define LIST_REMOVE(elm, field) do {                                    \
  423         QMD_SAVELINK(oldnext, (elm)->field.le_next);                    \
  424         QMD_SAVELINK(oldprev, (elm)->field.le_prev);                    \
  425         QMD_LIST_CHECK_NEXT(elm, field);                                \
  426         QMD_LIST_CHECK_PREV(elm, field);                                \
  427         if (LIST_NEXT((elm), field) != NULL)                            \
  428                 LIST_NEXT((elm), field)->field.le_prev =                \
  429                     (elm)->field.le_prev;                               \
  430         *(elm)->field.le_prev = LIST_NEXT((elm), field);                \
  431         TRASHIT(*oldnext);                                              \
  432         TRASHIT(*oldprev);                                              \
  433 } while (0)
  434 
  435 #define LIST_SWAP(head1, head2, type, field) do {                       \
  436         struct type *swap_tmp = LIST_FIRST((head1));                    \
  437         LIST_FIRST((head1)) = LIST_FIRST((head2));                      \
  438         LIST_FIRST((head2)) = swap_tmp;                                 \
  439         if ((swap_tmp = LIST_FIRST((head1))) != NULL)                   \
  440                 swap_tmp->field.le_prev = &LIST_FIRST((head1));         \
  441         if ((swap_tmp = LIST_FIRST((head2))) != NULL)                   \
  442                 swap_tmp->field.le_prev = &LIST_FIRST((head2));         \
  443 } while (0)
  444 
  445 /*
  446  * Tail queue declarations.
  447  */
  448 #define TAILQ_HEAD(name, type)                                          \
  449 struct name {                                                           \
  450         struct type *tqh_first; /* first element */                     \
  451         struct type **tqh_last; /* addr of last next element */         \
  452         TRACEBUF                                                        \
  453 }
  454 
  455 #define TAILQ_HEAD_INITIALIZER(head)                                    \
  456         { NULL, &(head).tqh_first }
  457 
  458 #define TAILQ_ENTRY(type)                                               \
  459 struct {                                                                \
  460         struct type *tqe_next;  /* next element */                      \
  461         struct type **tqe_prev; /* address of previous next element */  \
  462         TRACEBUF                                                        \
  463 }
  464 
  465 /*
  466  * Tail queue functions.
  467  */
  468 #if (defined(_KERNEL) && defined(INVARIANTS))
  469 #define QMD_TAILQ_CHECK_HEAD(head, field) do {                          \
  470         if (!TAILQ_EMPTY(head) &&                                       \
  471             TAILQ_FIRST((head))->field.tqe_prev !=                      \
  472              &TAILQ_FIRST((head)))                                      \
  473                 panic("Bad tailq head %p first->prev != head", (head)); \
  474 } while (0)
  475 
  476 #define QMD_TAILQ_CHECK_TAIL(head, field) do {                          \
  477         if (*(head)->tqh_last != NULL)                                  \
  478                 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head));  \
  479 } while (0)
  480 
  481 #define QMD_TAILQ_CHECK_NEXT(elm, field) do {                           \
  482         if (TAILQ_NEXT((elm), field) != NULL &&                         \
  483             TAILQ_NEXT((elm), field)->field.tqe_prev !=                 \
  484              &((elm)->field.tqe_next))                                  \
  485                 panic("Bad link elm %p next->prev != elm", (elm));      \
  486 } while (0)
  487 
  488 #define QMD_TAILQ_CHECK_PREV(elm, field) do {                           \
  489         if (*(elm)->field.tqe_prev != (elm))                            \
  490                 panic("Bad link elm %p prev->next != elm", (elm));      \
  491 } while (0)
  492 #else
  493 #define QMD_TAILQ_CHECK_HEAD(head, field)
  494 #define QMD_TAILQ_CHECK_TAIL(head, headname)
  495 #define QMD_TAILQ_CHECK_NEXT(elm, field)
  496 #define QMD_TAILQ_CHECK_PREV(elm, field)
  497 #endif /* (_KERNEL && INVARIANTS) */
  498 
  499 #define TAILQ_CONCAT(head1, head2, field) do {                          \
  500         if (!TAILQ_EMPTY(head2)) {                                      \
  501                 *(head1)->tqh_last = (head2)->tqh_first;                \
  502                 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
  503                 (head1)->tqh_last = (head2)->tqh_last;                  \
  504                 TAILQ_INIT((head2));                                    \
  505                 QMD_TRACE_HEAD(head1);                                  \
  506                 QMD_TRACE_HEAD(head2);                                  \
  507         }                                                               \
  508 } while (0)
  509 
  510 #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
  511 
  512 #define TAILQ_FIRST(head)       ((head)->tqh_first)
  513 
  514 #define TAILQ_FOREACH(var, head, field)                                 \
  515         for ((var) = TAILQ_FIRST((head));                               \
  516             (var);                                                      \
  517             (var) = TAILQ_NEXT((var), field))
  518 
  519 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
  520         for ((var) = TAILQ_FIRST((head));                               \
  521             (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
  522             (var) = (tvar))
  523 
  524 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
  525         for ((var) = TAILQ_LAST((head), headname);                      \
  526             (var);                                                      \
  527             (var) = TAILQ_PREV((var), headname, field))
  528 
  529 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)    \
  530         for ((var) = TAILQ_LAST((head), headname);                      \
  531             (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);  \
  532             (var) = (tvar))
  533 
  534 #define TAILQ_INIT(head) do {                                           \
  535         TAILQ_FIRST((head)) = NULL;                                     \
  536         (head)->tqh_last = &TAILQ_FIRST((head));                        \
  537         QMD_TRACE_HEAD(head);                                           \
  538 } while (0)
  539 
  540 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
  541         QMD_TAILQ_CHECK_NEXT(listelm, field);                           \
  542         if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
  543                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
  544                     &TAILQ_NEXT((elm), field);                          \
  545         else {                                                          \
  546                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
  547                 QMD_TRACE_HEAD(head);                                   \
  548         }                                                               \
  549         TAILQ_NEXT((listelm), field) = (elm);                           \
  550         (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
  551         QMD_TRACE_ELEM(&(elm)->field);                                  \
  552         QMD_TRACE_ELEM(&listelm->field);                                \
  553 } while (0)
  554 
  555 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
  556         QMD_TAILQ_CHECK_PREV(listelm, field);                           \
  557         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
  558         TAILQ_NEXT((elm), field) = (listelm);                           \
  559         *(listelm)->field.tqe_prev = (elm);                             \
  560         (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
  561         QMD_TRACE_ELEM(&(elm)->field);                                  \
  562         QMD_TRACE_ELEM(&listelm->field);                                \
  563 } while (0)
  564 
  565 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
  566         QMD_TAILQ_CHECK_HEAD(head, field);                              \
  567         if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
  568                 TAILQ_FIRST((head))->field.tqe_prev =                   \
  569                     &TAILQ_NEXT((elm), field);                          \
  570         else                                                            \
  571                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
  572         TAILQ_FIRST((head)) = (elm);                                    \
  573         (elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
  574         QMD_TRACE_HEAD(head);                                           \
  575         QMD_TRACE_ELEM(&(elm)->field);                                  \
  576 } while (0)
  577 
  578 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
  579         QMD_TAILQ_CHECK_TAIL(head, field);                              \
  580         TAILQ_NEXT((elm), field) = NULL;                                \
  581         (elm)->field.tqe_prev = (head)->tqh_last;                       \
  582         *(head)->tqh_last = (elm);                                      \
  583         (head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
  584         QMD_TRACE_HEAD(head);                                           \
  585         QMD_TRACE_ELEM(&(elm)->field);                                  \
  586 } while (0)
  587 
  588 #define TAILQ_LAST(head, headname)                                      \
  589         (*(((struct headname *)((head)->tqh_last))->tqh_last))
  590 
  591 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  592 
  593 #define TAILQ_PREV(elm, headname, field)                                \
  594         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  595 
  596 #define TAILQ_REMOVE(head, elm, field) do {                             \
  597         QMD_SAVELINK(oldnext, (elm)->field.tqe_next);                   \
  598         QMD_SAVELINK(oldprev, (elm)->field.tqe_prev);                   \
  599         QMD_TAILQ_CHECK_NEXT(elm, field);                               \
  600         QMD_TAILQ_CHECK_PREV(elm, field);                               \
  601         if ((TAILQ_NEXT((elm), field)) != NULL)                         \
  602                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
  603                     (elm)->field.tqe_prev;                              \
  604         else {                                                          \
  605                 (head)->tqh_last = (elm)->field.tqe_prev;               \
  606                 QMD_TRACE_HEAD(head);                                   \
  607         }                                                               \
  608         *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
  609         TRASHIT(*oldnext);                                              \
  610         TRASHIT(*oldprev);                                              \
  611         QMD_TRACE_ELEM(&(elm)->field);                                  \
  612 } while (0)
  613 
  614 #define TAILQ_SWAP(head1, head2, type, field) do {                      \
  615         struct type *swap_first = (head1)->tqh_first;                   \
  616         struct type **swap_last = (head1)->tqh_last;                    \
  617         (head1)->tqh_first = (head2)->tqh_first;                        \
  618         (head1)->tqh_last = (head2)->tqh_last;                          \
  619         (head2)->tqh_first = swap_first;                                \
  620         (head2)->tqh_last = swap_last;                                  \
  621         if ((swap_first = (head1)->tqh_first) != NULL)                  \
  622                 swap_first->field.tqe_prev = &(head1)->tqh_first;       \
  623         else                                                            \
  624                 (head1)->tqh_last = &(head1)->tqh_first;                \
  625         if ((swap_first = (head2)->tqh_first) != NULL)                  \
  626                 swap_first->field.tqe_prev = &(head2)->tqh_first;       \
  627         else                                                            \
  628                 (head2)->tqh_last = &(head2)->tqh_first;                \
  629 } while (0)
  630 
  631 #ifdef _KERNEL
  632 
  633 /*
  634  * XXX insque() and remque() are an old way of handling certain queues.
  635  * They bogusly assumes that all queue heads look alike.
  636  */
  637 
  638 struct quehead {
  639         struct quehead *qh_link;
  640         struct quehead *qh_rlink;
  641 };
  642 
  643 #ifdef __CC_SUPPORTS___INLINE
  644 
  645 static __inline void
  646 insque(void *a, void *b)
  647 {
  648         struct quehead *element = (struct quehead *)a,
  649                  *head = (struct quehead *)b;
  650 
  651         element->qh_link = head->qh_link;
  652         element->qh_rlink = head;
  653         head->qh_link = element;
  654         element->qh_link->qh_rlink = element;
  655 }
  656 
  657 static __inline void
  658 remque(void *a)
  659 {
  660         struct quehead *element = (struct quehead *)a;
  661 
  662         element->qh_link->qh_rlink = element->qh_rlink;
  663         element->qh_rlink->qh_link = element->qh_link;
  664         element->qh_rlink = 0;
  665 }
  666 
  667 #else /* !__CC_SUPPORTS___INLINE */
  668 
  669 void    insque(void *a, void *b);
  670 void    remque(void *a);
  671 
  672 #endif /* __CC_SUPPORTS___INLINE */
  673 
  674 #endif /* _KERNEL */
  675 
  676 #endif /* !_SYS_QUEUE_H_ */

Cache object: 361bacc8f42b9a0186432c8d2d1c41a5


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