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

Cache object: e15313501794b5826568ea12f51fe42b


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