Home | History | Annotate | Line # | Download | only in utilities
      1  1.1  christos /*
      2  1.1  christos  * The following code is directly copied from <sys/queue.h>.
      3  1.1  christos  * For the usage of <sys/queue.h>, see https://linux.die.net/man/3/queue.
      4  1.1  christos  * =======================================================================================
      5  1.1  christos  *
      6  1.1  christos  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
      9  1.1  christos  *
     10  1.1  christos  * This file contains Original Code and/or Modifications of Original Code
     11  1.1  christos  * as defined in and that are subject to the Apple Public Source License
     12  1.1  christos  * Version 2.0 (the 'License'). You may not use this file except in
     13  1.1  christos  * compliance with the License. The rights granted to you under the License
     14  1.1  christos  * may not be used to create, or enable the creation or redistribution of,
     15  1.1  christos  * unlawful or unlicensed copies of an Apple operating system, or to
     16  1.1  christos  * circumvent, violate, or enable the circumvention or violation of, any
     17  1.1  christos  * terms of an Apple operating system software license agreement.
     18  1.1  christos  *
     19  1.1  christos  * Please obtain a copy of the License at
     20  1.1  christos  * http://www.opensource.apple.com/apsl/ and read it before using this file.
     21  1.1  christos  *
     22  1.1  christos  * The Original Code and all software distributed under the License are
     23  1.1  christos  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
     24  1.1  christos  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
     25  1.1  christos  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
     26  1.1  christos  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
     27  1.1  christos  * Please see the License for the specific language governing rights and
     28  1.1  christos  * limitations under the License.
     29  1.1  christos  *
     30  1.1  christos  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
     31  1.1  christos  */
     32  1.1  christos /*-
     33  1.1  christos  * Copyright (c) 1991, 1993
     34  1.1  christos  *	The Regents of the University of California.  All rights reserved.
     35  1.1  christos  *
     36  1.1  christos  * Redistribution and use in source and binary forms, with or without
     37  1.1  christos  * modification, are permitted provided that the following conditions
     38  1.1  christos  * are met:
     39  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     40  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     41  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     43  1.1  christos  *    documentation and/or other materials provided with the distribution.
     44  1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     45  1.1  christos  *    may be used to endorse or promote products derived from this software
     46  1.1  christos  *    without specific prior written permission.
     47  1.1  christos  *
     48  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  1.1  christos  * SUCH DAMAGE.
     59  1.1  christos  *
     60  1.1  christos  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
     61  1.1  christos  */
     62  1.1  christos 
     63  1.1  christos #ifndef _SYS_QUEUE_H_
     64  1.1  christos #define _SYS_QUEUE_H_
     65  1.1  christos 
     66  1.1  christos #ifndef __improbable
     67  1.1  christos #define __improbable(x) (x)             /* noop in userspace */
     68  1.1  christos #endif /* __improbable */
     69  1.1  christos 
     70  1.1  christos /*
     71  1.1  christos  * This file defines five types of data structures: singly-linked lists,
     72  1.1  christos  * singly-linked tail queues, lists, tail queues, and circular queues.
     73  1.1  christos  *
     74  1.1  christos  * A singly-linked list is headed by a single forward pointer. The elements
     75  1.1  christos  * are singly linked for minimum space and pointer manipulation overhead at
     76  1.1  christos  * the expense of O(n) removal for arbitrary elements. New elements can be
     77  1.1  christos  * added to the list after an existing element or at the head of the list.
     78  1.1  christos  * Elements being removed from the head of the list should use the explicit
     79  1.1  christos  * macro for this purpose for optimum efficiency. A singly-linked list may
     80  1.1  christos  * only be traversed in the forward direction.  Singly-linked lists are ideal
     81  1.1  christos  * for applications with large datasets and few or no removals or for
     82  1.1  christos  * implementing a LIFO queue.
     83  1.1  christos  *
     84  1.1  christos  * A singly-linked tail queue is headed by a pair of pointers, one to the
     85  1.1  christos  * head of the list and the other to the tail of the list. The elements are
     86  1.1  christos  * singly linked for minimum space and pointer manipulation overhead at the
     87  1.1  christos  * expense of O(n) removal for arbitrary elements. New elements can be added
     88  1.1  christos  * to the list after an existing element, at the head of the list, or at the
     89  1.1  christos  * end of the list. Elements being removed from the head of the tail queue
     90  1.1  christos  * should use the explicit macro for this purpose for optimum efficiency.
     91  1.1  christos  * A singly-linked tail queue may only be traversed in the forward direction.
     92  1.1  christos  * Singly-linked tail queues are ideal for applications with large datasets
     93  1.1  christos  * and few or no removals or for implementing a FIFO queue.
     94  1.1  christos  *
     95  1.1  christos  * A list is headed by a single forward pointer (or an array of forward
     96  1.1  christos  * pointers for a hash table header). The elements are doubly linked
     97  1.1  christos  * so that an arbitrary element can be removed without a need to
     98  1.1  christos  * traverse the list. New elements can be added to the list before
     99  1.1  christos  * or after an existing element or at the head of the list. A list
    100  1.1  christos  * may only be traversed in the forward direction.
    101  1.1  christos  *
    102  1.1  christos  * A tail queue is headed by a pair of pointers, one to the head of the
    103  1.1  christos  * list and the other to the tail of the list. The elements are doubly
    104  1.1  christos  * linked so that an arbitrary element can be removed without a need to
    105  1.1  christos  * traverse the list. New elements can be added to the list before or
    106  1.1  christos  * after an existing element, at the head of the list, or at the end of
    107  1.1  christos  * the list. A tail queue may be traversed in either direction.
    108  1.1  christos  *
    109  1.1  christos  * A circle queue is headed by a pair of pointers, one to the head of the
    110  1.1  christos  * list and the other to the tail of the list. The elements are doubly
    111  1.1  christos  * linked so that an arbitrary element can be removed without a need to
    112  1.1  christos  * traverse the list. New elements can be added to the list before or after
    113  1.1  christos  * an existing element, at the head of the list, or at the end of the list.
    114  1.1  christos  * A circle queue may be traversed in either direction, but has a more
    115  1.1  christos  * complex end of list detection.
    116  1.1  christos  * Note that circle queues are deprecated, because, as the removal log
    117  1.1  christos  * in FreeBSD states, "CIRCLEQs are a disgrace to everything Knuth taught
    118  1.1  christos  * us in Volume 1 Chapter 2. [...] Use TAILQ instead, it provides the same
    119  1.1  christos  * functionality." Code using them will continue to compile, but they
    120  1.1  christos  * are no longer documented on the man page.
    121  1.1  christos  *
    122  1.1  christos  * For details on the use of these macros, see the queue(3) manual page.
    123  1.1  christos  *
    124  1.1  christos  *
    125  1.1  christos  *				SLIST	LIST	STAILQ	TAILQ	CIRCLEQ
    126  1.1  christos  * _HEAD			+	+	+	+	+
    127  1.1  christos  * _HEAD_INITIALIZER		+	+	+	+	-
    128  1.1  christos  * _ENTRY			+	+	+	+	+
    129  1.1  christos  * _INIT			+	+	+	+	+
    130  1.1  christos  * _EMPTY			+	+	+	+	+
    131  1.1  christos  * _FIRST			+	+	+	+	+
    132  1.1  christos  * _NEXT			+	+	+	+	+
    133  1.1  christos  * _PREV			-	-	-	+	+
    134  1.1  christos  * _LAST			-	-	+	+	+
    135  1.1  christos  * _FOREACH			+	+	+	+	+
    136  1.1  christos  * _FOREACH_SAFE		+	+	+	+	-
    137  1.1  christos  * _FOREACH_REVERSE		-	-	-	+	-
    138  1.1  christos  * _FOREACH_REVERSE_SAFE	-	-	-	+	-
    139  1.1  christos  * _INSERT_HEAD			+	+	+	+	+
    140  1.1  christos  * _INSERT_BEFORE		-	+	-	+	+
    141  1.1  christos  * _INSERT_AFTER		+	+	+	+	+
    142  1.1  christos  * _INSERT_TAIL			-	-	+	+	+
    143  1.1  christos  * _CONCAT			-	-	+	+	-
    144  1.1  christos  * _REMOVE_AFTER		+	-	+	-	-
    145  1.1  christos  * _REMOVE_HEAD			+	-	+	-	-
    146  1.1  christos  * _REMOVE_HEAD_UNTIL		-	-	+	-	-
    147  1.1  christos  * _REMOVE			+	+	+	+	+
    148  1.1  christos  * _SWAP			-	+	+	+	-
    149  1.1  christos  *
    150  1.1  christos  */
    151  1.1  christos #ifdef QUEUE_MACRO_DEBUG
    152  1.1  christos /* Store the last 2 places the queue element or head was altered */
    153  1.1  christos struct qm_trace {
    154  1.1  christos 	char * lastfile;
    155  1.1  christos 	int lastline;
    156  1.1  christos 	char * prevfile;
    157  1.1  christos 	int prevline;
    158  1.1  christos };
    159  1.1  christos 
    160  1.1  christos #define TRACEBUF        struct qm_trace trace;
    161  1.1  christos #define TRASHIT(x)      do {(x) = (void *)-1;} while (0)
    162  1.1  christos 
    163  1.1  christos #define QMD_TRACE_HEAD(head) do {                                       \
    164  1.1  christos 	(head)->trace.prevline = (head)->trace.lastline;                \
    165  1.1  christos 	(head)->trace.prevfile = (head)->trace.lastfile;                \
    166  1.1  christos 	(head)->trace.lastline = __LINE__;                              \
    167  1.1  christos 	(head)->trace.lastfile = __FILE__;                              \
    168  1.1  christos } while (0)
    169  1.1  christos 
    170  1.1  christos #define QMD_TRACE_ELEM(elem) do {                                       \
    171  1.1  christos 	(elem)->trace.prevline = (elem)->trace.lastline;                \
    172  1.1  christos 	(elem)->trace.prevfile = (elem)->trace.lastfile;                \
    173  1.1  christos 	(elem)->trace.lastline = __LINE__;                              \
    174  1.1  christos 	(elem)->trace.lastfile = __FILE__;                              \
    175  1.1  christos } while (0)
    176  1.1  christos 
    177  1.1  christos #else
    178  1.1  christos #define QMD_TRACE_ELEM(elem)
    179  1.1  christos #define QMD_TRACE_HEAD(head)
    180  1.1  christos #define TRACEBUF
    181  1.1  christos #define TRASHIT(x)      do {(x) = (void *)0;} while (0)
    182  1.1  christos #endif  /* QUEUE_MACRO_DEBUG */
    183  1.1  christos 
    184  1.1  christos /*
    185  1.1  christos  * Horrible macros to enable use of code that was meant to be C-specific
    186  1.1  christos  *   (and which push struct onto type) in C++; without these, C++ code
    187  1.1  christos  *   that uses these macros in the context of a class will blow up
    188  1.1  christos  *   due to "struct" being preprended to "type" by the macros, causing
    189  1.1  christos  *   inconsistent use of tags.
    190  1.1  christos  *
    191  1.1  christos  * This approach is necessary because these are macros; we have to use
    192  1.1  christos  *   these on a per-macro basis (because the queues are implemented as
    193  1.1  christos  *   macros, disabling this warning in the scope of the header file is
    194  1.1  christos  *   insufficient), whuch means we can't use #pragma, and have to use
    195  1.1  christos  *   _Pragma.  We only need to use these for the queue macros that
    196  1.1  christos  *   prepend "struct" to "type" and will cause C++ to blow up.
    197  1.1  christos  */
    198  1.1  christos #if defined(__clang__) && defined(__cplusplus)
    199  1.1  christos #define __MISMATCH_TAGS_PUSH                                            \
    200  1.1  christos 	_Pragma("clang diagnostic push")                                \
    201  1.1  christos 	_Pragma("clang diagnostic ignored \"-Wmismatched-tags\"")
    202  1.1  christos #define __MISMATCH_TAGS_POP                                             \
    203  1.1  christos 	_Pragma("clang diagnostic pop")
    204  1.1  christos #else
    205  1.1  christos #define __MISMATCH_TAGS_PUSH
    206  1.1  christos #define __MISMATCH_TAGS_POP
    207  1.1  christos #endif
    208  1.1  christos 
    209  1.1  christos /*!
    210  1.1  christos  * Ensures that these macros can safely be used in structs when compiling with
    211  1.1  christos  * clang. The macros do not allow for nullability attributes to be specified due
    212  1.1  christos  * to how they are expanded. For example:
    213  1.1  christos  *
    214  1.1  christos  *     SLIST_HEAD(, foo _Nullable) bar;
    215  1.1  christos  *
    216  1.1  christos  * expands to
    217  1.1  christos  *
    218  1.1  christos  *     struct {
    219  1.1  christos  *         struct foo _Nullable *slh_first;
    220  1.1  christos  *     }
    221  1.1  christos  *
    222  1.1  christos  * which is not valid because the nullability specifier has to apply to the
    223  1.1  christos  * pointer. So just ignore nullability completeness in all the places where this
    224  1.1  christos  * is an issue.
    225  1.1  christos  */
    226  1.1  christos #if defined(__clang__)
    227  1.1  christos #define __NULLABILITY_COMPLETENESS_PUSH \
    228  1.1  christos 	_Pragma("clang diagnostic push") \
    229  1.1  christos 	_Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
    230  1.1  christos #define __NULLABILITY_COMPLETENESS_POP \
    231  1.1  christos 	_Pragma("clang diagnostic pop")
    232  1.1  christos #else
    233  1.1  christos #define __NULLABILITY_COMPLETENESS_PUSH
    234  1.1  christos #define __NULLABILITY_COMPLETENESS_POP
    235  1.1  christos #endif
    236  1.1  christos 
    237  1.1  christos /*
    238  1.1  christos  * Singly-linked List declarations.
    239  1.1  christos  */
    240  1.1  christos #define SLIST_HEAD(name, type)                                          \
    241  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    242  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    243  1.1  christos struct name {                                                           \
    244  1.1  christos 	struct type *slh_first; /* first element */                     \
    245  1.1  christos }                                                                       \
    246  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    247  1.1  christos __MISMATCH_TAGS_POP
    248  1.1  christos 
    249  1.1  christos #define SLIST_HEAD_INITIALIZER(head)                                    \
    250  1.1  christos 	{ NULL }
    251  1.1  christos 
    252  1.1  christos #define SLIST_ENTRY(type)                                               \
    253  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    254  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    255  1.1  christos struct {                                                                \
    256  1.1  christos 	struct type *sle_next;  /* next element */                      \
    257  1.1  christos }                                                                       \
    258  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    259  1.1  christos __MISMATCH_TAGS_POP
    260  1.1  christos 
    261  1.1  christos /*
    262  1.1  christos  * Singly-linked List functions.
    263  1.1  christos  */
    264  1.1  christos #define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
    265  1.1  christos 
    266  1.1  christos #define SLIST_FIRST(head)       ((head)->slh_first)
    267  1.1  christos 
    268  1.1  christos #define SLIST_FOREACH(var, head, field)                                 \
    269  1.1  christos 	for ((var) = SLIST_FIRST((head));                               \
    270  1.1  christos 	    (var);                                                      \
    271  1.1  christos 	    (var) = SLIST_NEXT((var), field))
    272  1.1  christos 
    273  1.1  christos #define SLIST_FOREACH_SAFE(var, head, field, tvar)                      \
    274  1.1  christos 	for ((var) = SLIST_FIRST((head));                               \
    275  1.1  christos 	    (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
    276  1.1  christos 	    (var) = (tvar))
    277  1.1  christos 
    278  1.1  christos #define SLIST_FOREACH_PREVPTR(var, varp, head, field)                   \
    279  1.1  christos 	for ((varp) = &SLIST_FIRST((head));                             \
    280  1.1  christos 	    ((var) = *(varp)) != NULL;                                  \
    281  1.1  christos 	    (varp) = &SLIST_NEXT((var), field))
    282  1.1  christos 
    283  1.1  christos #define SLIST_INIT(head) do {                                           \
    284  1.1  christos 	SLIST_FIRST((head)) = NULL;                                     \
    285  1.1  christos } while (0)
    286  1.1  christos 
    287  1.1  christos #define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
    288  1.1  christos 	SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);       \
    289  1.1  christos 	SLIST_NEXT((slistelm), field) = (elm);                          \
    290  1.1  christos } while (0)
    291  1.1  christos 
    292  1.1  christos #define SLIST_INSERT_HEAD(head, elm, field) do {                        \
    293  1.1  christos 	SLIST_NEXT((elm), field) = SLIST_FIRST((head));                 \
    294  1.1  christos 	SLIST_FIRST((head)) = (elm);                                    \
    295  1.1  christos } while (0)
    296  1.1  christos 
    297  1.1  christos #define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
    298  1.1  christos 
    299  1.1  christos #define SLIST_REMOVE(head, elm, type, field)                            \
    300  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    301  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    302  1.1  christos do {                                                                    \
    303  1.1  christos 	if (SLIST_FIRST((head)) == (elm)) {                             \
    304  1.1  christos 	        SLIST_REMOVE_HEAD((head), field);                       \
    305  1.1  christos 	}                                                               \
    306  1.1  christos 	else {                                                          \
    307  1.1  christos 	        struct type *curelm = SLIST_FIRST((head));              \
    308  1.1  christos 	        while (SLIST_NEXT(curelm, field) != (elm))              \
    309  1.1  christos 	                curelm = SLIST_NEXT(curelm, field);             \
    310  1.1  christos 	        SLIST_REMOVE_AFTER(curelm, field);                      \
    311  1.1  christos 	}                                                               \
    312  1.1  christos } while (0)                                                             \
    313  1.1  christos __NULLABILITY_COMPLETENESS_POP                                      \
    314  1.1  christos __MISMATCH_TAGS_POP
    315  1.1  christos 
    316  1.1  christos #define SLIST_REMOVE_AFTER(elm, field) do {                             \
    317  1.1  christos 	__typeof__(elm) __remove_elem = SLIST_NEXT(elm, field);         \
    318  1.1  christos 	SLIST_NEXT(elm, field) =                                        \
    319  1.1  christos 	    SLIST_NEXT(__remove_elem, field);                           \
    320  1.1  christos 	TRASHIT(__remove_elem->field.sle_next);                         \
    321  1.1  christos } while (0)
    322  1.1  christos 
    323  1.1  christos #define SLIST_REMOVE_HEAD(head, field) do {                             \
    324  1.1  christos 	__typeof__(SLIST_FIRST((head))) __remove_elem =                 \
    325  1.1  christos 	    SLIST_FIRST((head));                                        \
    326  1.1  christos 	SLIST_FIRST((head)) = SLIST_NEXT(__remove_elem, field);         \
    327  1.1  christos 	TRASHIT(__remove_elem->field.sle_next);                         \
    328  1.1  christos } while (0)
    329  1.1  christos 
    330  1.1  christos /*
    331  1.1  christos  * Singly-linked Tail queue declarations.
    332  1.1  christos  */
    333  1.1  christos #define STAILQ_HEAD(name, type)                                         \
    334  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    335  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    336  1.1  christos struct name {                                                           \
    337  1.1  christos 	struct type *stqh_first;/* first element */                     \
    338  1.1  christos 	struct type **stqh_last;/* addr of last next element */         \
    339  1.1  christos }                                                                       \
    340  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    341  1.1  christos __MISMATCH_TAGS_POP
    342  1.1  christos 
    343  1.1  christos #define STAILQ_HEAD_INITIALIZER(head)                                   \
    344  1.1  christos 	{ NULL, &(head).stqh_first }
    345  1.1  christos 
    346  1.1  christos #define STAILQ_ENTRY(type)                                              \
    347  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    348  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    349  1.1  christos struct {                                                                \
    350  1.1  christos 	struct type *stqe_next; /* next element */                      \
    351  1.1  christos }                                                                       \
    352  1.1  christos __NULLABILITY_COMPLETENESS_POP                                         \
    353  1.1  christos __MISMATCH_TAGS_POP
    354  1.1  christos 
    355  1.1  christos /*
    356  1.1  christos  * Singly-linked Tail queue functions.
    357  1.1  christos  */
    358  1.1  christos #define STAILQ_CONCAT(head1, head2) do {                                \
    359  1.1  christos 	if (!STAILQ_EMPTY((head2))) {                                   \
    360  1.1  christos 	        *(head1)->stqh_last = (head2)->stqh_first;              \
    361  1.1  christos 	        (head1)->stqh_last = (head2)->stqh_last;                \
    362  1.1  christos 	        STAILQ_INIT((head2));                                   \
    363  1.1  christos 	}                                                               \
    364  1.1  christos } while (0)
    365  1.1  christos 
    366  1.1  christos #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
    367  1.1  christos 
    368  1.1  christos #define STAILQ_FIRST(head)      ((head)->stqh_first)
    369  1.1  christos 
    370  1.1  christos #define STAILQ_FOREACH(var, head, field)                                \
    371  1.1  christos 	for((var) = STAILQ_FIRST((head));                               \
    372  1.1  christos 	   (var);                                                       \
    373  1.1  christos 	   (var) = STAILQ_NEXT((var), field))
    374  1.1  christos 
    375  1.1  christos 
    376  1.1  christos #define STAILQ_FOREACH_SAFE(var, head, field, tvar)                     \
    377  1.1  christos 	for ((var) = STAILQ_FIRST((head));                              \
    378  1.1  christos 	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);           \
    379  1.1  christos 	    (var) = (tvar))
    380  1.1  christos 
    381  1.1  christos #define STAILQ_INIT(head) do {                                          \
    382  1.1  christos 	STAILQ_FIRST((head)) = NULL;                                    \
    383  1.1  christos 	(head)->stqh_last = &STAILQ_FIRST((head));                      \
    384  1.1  christos } while (0)
    385  1.1  christos 
    386  1.1  christos #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
    387  1.1  christos 	if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
    388  1.1  christos 	        (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
    389  1.1  christos 	STAILQ_NEXT((tqelm), field) = (elm);                            \
    390  1.1  christos } while (0)
    391  1.1  christos 
    392  1.1  christos #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
    393  1.1  christos 	if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
    394  1.1  christos 	        (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
    395  1.1  christos 	STAILQ_FIRST((head)) = (elm);                                   \
    396  1.1  christos } while (0)
    397  1.1  christos 
    398  1.1  christos #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
    399  1.1  christos 	STAILQ_NEXT((elm), field) = NULL;                               \
    400  1.1  christos 	*(head)->stqh_last = (elm);                                     \
    401  1.1  christos 	(head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
    402  1.1  christos } while (0)
    403  1.1  christos 
    404  1.1  christos #define STAILQ_LAST(head, type, field)                                  \
    405  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    406  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    407  1.1  christos 	(STAILQ_EMPTY((head)) ?                                         \
    408  1.1  christos 	        NULL :                                                  \
    409  1.1  christos 	        ((struct type *)(void *)                                \
    410  1.1  christos 	        ((char *)((head)->stqh_last) - __offsetof(struct type, field))))\
    411  1.1  christos __NULLABILITY_COMPLETENESS_POP                                         \
    412  1.1  christos __MISMATCH_TAGS_POP
    413  1.1  christos 
    414  1.1  christos #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
    415  1.1  christos 
    416  1.1  christos #define STAILQ_REMOVE(head, elm, type, field)                           \
    417  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    418  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    419  1.1  christos do {                                                                    \
    420  1.1  christos 	if (STAILQ_FIRST((head)) == (elm)) {                            \
    421  1.1  christos 	        STAILQ_REMOVE_HEAD((head), field);                      \
    422  1.1  christos 	}                                                               \
    423  1.1  christos 	else {                                                          \
    424  1.1  christos 	        struct type *curelm = STAILQ_FIRST((head));             \
    425  1.1  christos 	        while (STAILQ_NEXT(curelm, field) != (elm))             \
    426  1.1  christos 	                curelm = STAILQ_NEXT(curelm, field);            \
    427  1.1  christos 	        STAILQ_REMOVE_AFTER(head, curelm, field);               \
    428  1.1  christos 	}                                                               \
    429  1.1  christos } while (0)                                                             \
    430  1.1  christos __NULLABILITY_COMPLETENESS_POP                                      \
    431  1.1  christos __MISMATCH_TAGS_POP
    432  1.1  christos 
    433  1.1  christos #define STAILQ_REMOVE_HEAD(head, field) do {                            \
    434  1.1  christos 	__typeof__(STAILQ_FIRST((head))) __remove_elem =                \
    435  1.1  christos 	    STAILQ_FIRST((head));                                       \
    436  1.1  christos 	if ((STAILQ_FIRST((head)) =                                     \
    437  1.1  christos 	     STAILQ_NEXT(__remove_elem, field)) == NULL)                \
    438  1.1  christos 	        (head)->stqh_last = &STAILQ_FIRST((head));              \
    439  1.1  christos 	TRASHIT(__remove_elem->field.stqe_next);                        \
    440  1.1  christos } while (0)
    441  1.1  christos 
    442  1.1  christos #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {                 \
    443  1.1  christos 	if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
    444  1.1  christos 	       (head)->stqh_last = &STAILQ_FIRST((head));               \
    445  1.1  christos 	TRASHIT((elm)->field.stqe_next);                                \
    446  1.1  christos } while (0)
    447  1.1  christos 
    448  1.1  christos #define STAILQ_REMOVE_AFTER(head, elm, field) do {                      \
    449  1.1  christos 	__typeof__(elm) __remove_elem = STAILQ_NEXT(elm, field);        \
    450  1.1  christos 	if ((STAILQ_NEXT(elm, field) =                                  \
    451  1.1  christos 	    STAILQ_NEXT(__remove_elem, field)) == NULL)                 \
    452  1.1  christos 	        (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
    453  1.1  christos 	TRASHIT(__remove_elem->field.stqe_next);                        \
    454  1.1  christos } while (0)
    455  1.1  christos 
    456  1.1  christos #define STAILQ_SWAP(head1, head2, type)                                 \
    457  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    458  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    459  1.1  christos do {                                                                    \
    460  1.1  christos 	struct type *swap_first = STAILQ_FIRST(head1);                  \
    461  1.1  christos 	struct type **swap_last = (head1)->stqh_last;                   \
    462  1.1  christos 	STAILQ_FIRST(head1) = STAILQ_FIRST(head2);                      \
    463  1.1  christos 	(head1)->stqh_last = (head2)->stqh_last;                        \
    464  1.1  christos 	STAILQ_FIRST(head2) = swap_first;                               \
    465  1.1  christos 	(head2)->stqh_last = swap_last;                                 \
    466  1.1  christos 	if (STAILQ_EMPTY(head1))                                        \
    467  1.1  christos 	        (head1)->stqh_last = &STAILQ_FIRST(head1);              \
    468  1.1  christos 	if (STAILQ_EMPTY(head2))                                        \
    469  1.1  christos 	        (head2)->stqh_last = &STAILQ_FIRST(head2);              \
    470  1.1  christos } while (0)                                                             \
    471  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    472  1.1  christos __MISMATCH_TAGS_POP
    473  1.1  christos 
    474  1.1  christos 
    475  1.1  christos /*
    476  1.1  christos  * List declarations.
    477  1.1  christos  */
    478  1.1  christos #define LIST_HEAD(name, type)                                           \
    479  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    480  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    481  1.1  christos struct name {                                                           \
    482  1.1  christos 	struct type *lh_first;  /* first element */                     \
    483  1.1  christos }                                                                       \
    484  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    485  1.1  christos __MISMATCH_TAGS_POP
    486  1.1  christos 
    487  1.1  christos #define LIST_HEAD_INITIALIZER(head)                                     \
    488  1.1  christos 	{ NULL }
    489  1.1  christos 
    490  1.1  christos #define LIST_ENTRY(type)                                                \
    491  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    492  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    493  1.1  christos struct {                                                                \
    494  1.1  christos 	struct type *le_next;   /* next element */                      \
    495  1.1  christos 	struct type **le_prev;  /* address of previous next element */  \
    496  1.1  christos }                                                                       \
    497  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    498  1.1  christos __MISMATCH_TAGS_POP
    499  1.1  christos 
    500  1.1  christos /*
    501  1.1  christos  * List functions.
    502  1.1  christos  */
    503  1.1  christos 
    504  1.1  christos #define LIST_CHECK_HEAD(head, field)
    505  1.1  christos #define LIST_CHECK_NEXT(elm, field)
    506  1.1  christos #define LIST_CHECK_PREV(elm, field)
    507  1.1  christos 
    508  1.1  christos #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
    509  1.1  christos 
    510  1.1  christos #define LIST_FIRST(head)        ((head)->lh_first)
    511  1.1  christos 
    512  1.1  christos #define LIST_FOREACH(var, head, field)                                  \
    513  1.1  christos 	for ((var) = LIST_FIRST((head));                                \
    514  1.1  christos 	    (var);                                                      \
    515  1.1  christos 	    (var) = LIST_NEXT((var), field))
    516  1.1  christos 
    517  1.1  christos #define LIST_FOREACH_SAFE(var, head, field, tvar)                       \
    518  1.1  christos 	for ((var) = LIST_FIRST((head));                                \
    519  1.1  christos 	    (var) && ((tvar) = LIST_NEXT((var), field), 1);             \
    520  1.1  christos 	    (var) = (tvar))
    521  1.1  christos 
    522  1.1  christos #define LIST_INIT(head) do {                                            \
    523  1.1  christos 	LIST_FIRST((head)) = NULL;                                      \
    524  1.1  christos } while (0)
    525  1.1  christos 
    526  1.1  christos #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
    527  1.1  christos 	LIST_CHECK_NEXT(listelm, field);                                \
    528  1.1  christos 	if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
    529  1.1  christos 	        LIST_NEXT((listelm), field)->field.le_prev =            \
    530  1.1  christos 	            &LIST_NEXT((elm), field);                           \
    531  1.1  christos 	LIST_NEXT((listelm), field) = (elm);                            \
    532  1.1  christos 	(elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
    533  1.1  christos } while (0)
    534  1.1  christos 
    535  1.1  christos #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
    536  1.1  christos 	LIST_CHECK_PREV(listelm, field);                                \
    537  1.1  christos 	(elm)->field.le_prev = (listelm)->field.le_prev;                \
    538  1.1  christos 	LIST_NEXT((elm), field) = (listelm);                            \
    539  1.1  christos 	*(listelm)->field.le_prev = (elm);                              \
    540  1.1  christos 	(listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
    541  1.1  christos } while (0)
    542  1.1  christos 
    543  1.1  christos #define LIST_INSERT_HEAD(head, elm, field) do {                         \
    544  1.1  christos 	LIST_CHECK_HEAD((head), field);                         \
    545  1.1  christos 	if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
    546  1.1  christos 	        LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
    547  1.1  christos 	LIST_FIRST((head)) = (elm);                                     \
    548  1.1  christos 	(elm)->field.le_prev = &LIST_FIRST((head));                     \
    549  1.1  christos } while (0)
    550  1.1  christos 
    551  1.1  christos #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
    552  1.1  christos 
    553  1.1  christos #define LIST_REMOVE(elm, field) do {                                    \
    554  1.1  christos 	LIST_CHECK_NEXT(elm, field);                            \
    555  1.1  christos 	LIST_CHECK_PREV(elm, field);                            \
    556  1.1  christos 	if (LIST_NEXT((elm), field) != NULL)                            \
    557  1.1  christos 	        LIST_NEXT((elm), field)->field.le_prev =                \
    558  1.1  christos 	            (elm)->field.le_prev;                               \
    559  1.1  christos 	*(elm)->field.le_prev = LIST_NEXT((elm), field);                \
    560  1.1  christos 	TRASHIT((elm)->field.le_next);                                  \
    561  1.1  christos 	TRASHIT((elm)->field.le_prev);                                  \
    562  1.1  christos } while (0)
    563  1.1  christos 
    564  1.1  christos #define LIST_SWAP(head1, head2, type, field)                            \
    565  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    566  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    567  1.1  christos do {                                                                    \
    568  1.1  christos 	struct type *swap_tmp = LIST_FIRST((head1));                    \
    569  1.1  christos 	LIST_FIRST((head1)) = LIST_FIRST((head2));                      \
    570  1.1  christos 	LIST_FIRST((head2)) = swap_tmp;                                 \
    571  1.1  christos 	if ((swap_tmp = LIST_FIRST((head1))) != NULL)                   \
    572  1.1  christos 	        swap_tmp->field.le_prev = &LIST_FIRST((head1));         \
    573  1.1  christos 	if ((swap_tmp = LIST_FIRST((head2))) != NULL)                   \
    574  1.1  christos 	        swap_tmp->field.le_prev = &LIST_FIRST((head2));         \
    575  1.1  christos } while (0)                                                             \
    576  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    577  1.1  christos __MISMATCH_TAGS_POP
    578  1.1  christos 
    579  1.1  christos /*
    580  1.1  christos  * Tail queue declarations.
    581  1.1  christos  */
    582  1.1  christos #define TAILQ_HEAD(name, type)                                          \
    583  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    584  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    585  1.1  christos struct name {                                                           \
    586  1.1  christos 	struct type *tqh_first; /* first element */                     \
    587  1.1  christos 	struct type **tqh_last; /* addr of last next element */         \
    588  1.1  christos 	TRACEBUF                                                        \
    589  1.1  christos }                                                                       \
    590  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    591  1.1  christos __MISMATCH_TAGS_POP
    592  1.1  christos 
    593  1.1  christos #define TAILQ_HEAD_INITIALIZER(head)                                    \
    594  1.1  christos 	{ NULL, &(head).tqh_first }
    595  1.1  christos 
    596  1.1  christos #define TAILQ_ENTRY(type)                                               \
    597  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    598  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    599  1.1  christos struct {                                                                \
    600  1.1  christos 	struct type *tqe_next;  /* next element */                      \
    601  1.1  christos 	struct type **tqe_prev; /* address of previous next element */  \
    602  1.1  christos 	TRACEBUF                                                        \
    603  1.1  christos }                                                                       \
    604  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    605  1.1  christos __MISMATCH_TAGS_POP
    606  1.1  christos 
    607  1.1  christos /*
    608  1.1  christos  * Tail queue functions.
    609  1.1  christos  */
    610  1.1  christos #define TAILQ_CHECK_HEAD(head, field)
    611  1.1  christos #define TAILQ_CHECK_NEXT(elm, field)
    612  1.1  christos #define TAILQ_CHECK_PREV(elm, field)
    613  1.1  christos 
    614  1.1  christos #define TAILQ_CONCAT(head1, head2, field) do {                          \
    615  1.1  christos 	if (!TAILQ_EMPTY(head2)) {                                      \
    616  1.1  christos 	        *(head1)->tqh_last = (head2)->tqh_first;                \
    617  1.1  christos 	        (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
    618  1.1  christos 	        (head1)->tqh_last = (head2)->tqh_last;                  \
    619  1.1  christos 	        TAILQ_INIT((head2));                                    \
    620  1.1  christos 	        QMD_TRACE_HEAD(head1);                                  \
    621  1.1  christos 	        QMD_TRACE_HEAD(head2);                                  \
    622  1.1  christos 	}                                                               \
    623  1.1  christos } while (0)
    624  1.1  christos 
    625  1.1  christos #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
    626  1.1  christos 
    627  1.1  christos #define TAILQ_FIRST(head)       ((head)->tqh_first)
    628  1.1  christos 
    629  1.1  christos #define TAILQ_FOREACH(var, head, field)                                 \
    630  1.1  christos 	for ((var) = TAILQ_FIRST((head));                               \
    631  1.1  christos 	    (var);                                                      \
    632  1.1  christos 	    (var) = TAILQ_NEXT((var), field))
    633  1.1  christos 
    634  1.1  christos #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
    635  1.1  christos 	for ((var) = TAILQ_FIRST((head));                               \
    636  1.1  christos 	    (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
    637  1.1  christos 	    (var) = (tvar))
    638  1.1  christos 
    639  1.1  christos #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
    640  1.1  christos 	for ((var) = TAILQ_LAST((head), headname);                      \
    641  1.1  christos 	    (var);                                                      \
    642  1.1  christos 	    (var) = TAILQ_PREV((var), headname, field))
    643  1.1  christos 
    644  1.1  christos #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)    \
    645  1.1  christos 	for ((var) = TAILQ_LAST((head), headname);                      \
    646  1.1  christos 	    (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1);  \
    647  1.1  christos 	    (var) = (tvar))
    648  1.1  christos 
    649  1.1  christos 
    650  1.1  christos #define TAILQ_INIT(head) do {                                           \
    651  1.1  christos 	TAILQ_FIRST((head)) = NULL;                                     \
    652  1.1  christos 	(head)->tqh_last = &TAILQ_FIRST((head));                        \
    653  1.1  christos 	QMD_TRACE_HEAD(head);                                           \
    654  1.1  christos } while (0)
    655  1.1  christos 
    656  1.1  christos 
    657  1.1  christos #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
    658  1.1  christos 	TAILQ_CHECK_NEXT(listelm, field);                               \
    659  1.1  christos 	if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
    660  1.1  christos 	        TAILQ_NEXT((elm), field)->field.tqe_prev =              \
    661  1.1  christos 	            &TAILQ_NEXT((elm), field);                          \
    662  1.1  christos 	else {                                                          \
    663  1.1  christos 	        (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
    664  1.1  christos 	        QMD_TRACE_HEAD(head);                                   \
    665  1.1  christos 	}                                                               \
    666  1.1  christos 	TAILQ_NEXT((listelm), field) = (elm);                           \
    667  1.1  christos 	(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
    668  1.1  christos 	QMD_TRACE_ELEM(&(elm)->field);                                  \
    669  1.1  christos 	QMD_TRACE_ELEM(&listelm->field);                                \
    670  1.1  christos } while (0)
    671  1.1  christos 
    672  1.1  christos #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
    673  1.1  christos 	TAILQ_CHECK_PREV(listelm, field);                               \
    674  1.1  christos 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
    675  1.1  christos 	TAILQ_NEXT((elm), field) = (listelm);                           \
    676  1.1  christos 	*(listelm)->field.tqe_prev = (elm);                             \
    677  1.1  christos 	(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
    678  1.1  christos 	QMD_TRACE_ELEM(&(elm)->field);                                  \
    679  1.1  christos 	QMD_TRACE_ELEM(&listelm->field);                                \
    680  1.1  christos } while (0)
    681  1.1  christos 
    682  1.1  christos #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
    683  1.1  christos 	TAILQ_CHECK_HEAD(head, field);                                  \
    684  1.1  christos 	if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
    685  1.1  christos 	        TAILQ_FIRST((head))->field.tqe_prev =                   \
    686  1.1  christos 	            &TAILQ_NEXT((elm), field);                          \
    687  1.1  christos 	else                                                            \
    688  1.1  christos 	        (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
    689  1.1  christos 	TAILQ_FIRST((head)) = (elm);                                    \
    690  1.1  christos 	(elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
    691  1.1  christos 	QMD_TRACE_HEAD(head);                                           \
    692  1.1  christos 	QMD_TRACE_ELEM(&(elm)->field);                                  \
    693  1.1  christos } while (0)
    694  1.1  christos 
    695  1.1  christos #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
    696  1.1  christos 	TAILQ_NEXT((elm), field) = NULL;                                \
    697  1.1  christos 	(elm)->field.tqe_prev = (head)->tqh_last;                       \
    698  1.1  christos 	*(head)->tqh_last = (elm);                                      \
    699  1.1  christos 	(head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
    700  1.1  christos 	QMD_TRACE_HEAD(head);                                           \
    701  1.1  christos 	QMD_TRACE_ELEM(&(elm)->field);                                  \
    702  1.1  christos } while (0)
    703  1.1  christos 
    704  1.1  christos #define TAILQ_LAST(head, headname)                                      \
    705  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    706  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    707  1.1  christos 	(*(((struct headname *)((head)->tqh_last))->tqh_last))          \
    708  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    709  1.1  christos __MISMATCH_TAGS_POP
    710  1.1  christos 
    711  1.1  christos #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
    712  1.1  christos 
    713  1.1  christos #define TAILQ_PREV(elm, headname, field)                                \
    714  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    715  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    716  1.1  christos 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))     \
    717  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    718  1.1  christos __MISMATCH_TAGS_POP
    719  1.1  christos 
    720  1.1  christos #define TAILQ_REMOVE(head, elm, field) do {                             \
    721  1.1  christos 	TAILQ_CHECK_NEXT(elm, field);                                   \
    722  1.1  christos 	TAILQ_CHECK_PREV(elm, field);                                   \
    723  1.1  christos 	if ((TAILQ_NEXT((elm), field)) != NULL)                         \
    724  1.1  christos 	        TAILQ_NEXT((elm), field)->field.tqe_prev =              \
    725  1.1  christos 	            (elm)->field.tqe_prev;                              \
    726  1.1  christos 	else {                                                          \
    727  1.1  christos 	        (head)->tqh_last = (elm)->field.tqe_prev;               \
    728  1.1  christos 	        QMD_TRACE_HEAD(head);                                   \
    729  1.1  christos 	}                                                               \
    730  1.1  christos 	*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
    731  1.1  christos 	TRASHIT((elm)->field.tqe_next);                                 \
    732  1.1  christos 	TRASHIT((elm)->field.tqe_prev);                                 \
    733  1.1  christos 	QMD_TRACE_ELEM(&(elm)->field);                                  \
    734  1.1  christos } while (0)
    735  1.1  christos 
    736  1.1  christos /*
    737  1.1  christos  * Why did they switch to spaces for this one macro?
    738  1.1  christos  */
    739  1.1  christos #define TAILQ_SWAP(head1, head2, type, field)                           \
    740  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    741  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    742  1.1  christos do {                                                                    \
    743  1.1  christos 	struct type *swap_first = (head1)->tqh_first;                   \
    744  1.1  christos 	struct type **swap_last = (head1)->tqh_last;                    \
    745  1.1  christos 	(head1)->tqh_first = (head2)->tqh_first;                        \
    746  1.1  christos 	(head1)->tqh_last = (head2)->tqh_last;                          \
    747  1.1  christos 	(head2)->tqh_first = swap_first;                                \
    748  1.1  christos 	(head2)->tqh_last = swap_last;                                  \
    749  1.1  christos 	if ((swap_first = (head1)->tqh_first) != NULL)                  \
    750  1.1  christos 	        swap_first->field.tqe_prev = &(head1)->tqh_first;       \
    751  1.1  christos 	else                                                            \
    752  1.1  christos 	        (head1)->tqh_last = &(head1)->tqh_first;                \
    753  1.1  christos 	if ((swap_first = (head2)->tqh_first) != NULL)                  \
    754  1.1  christos 	        swap_first->field.tqe_prev = &(head2)->tqh_first;       \
    755  1.1  christos 	else                                                            \
    756  1.1  christos 	        (head2)->tqh_last = &(head2)->tqh_first;                \
    757  1.1  christos } while (0)                                                             \
    758  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    759  1.1  christos __MISMATCH_TAGS_POP
    760  1.1  christos 
    761  1.1  christos /*
    762  1.1  christos  * Circular queue definitions.
    763  1.1  christos  */
    764  1.1  christos #define CIRCLEQ_HEAD(name, type)                                        \
    765  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    766  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    767  1.1  christos struct name {                                                           \
    768  1.1  christos 	struct type *cqh_first;         /* first element */             \
    769  1.1  christos 	struct type *cqh_last;          /* last element */              \
    770  1.1  christos }                                                                       \
    771  1.1  christos __NULLABILITY_COMPLETENESS_POP                                          \
    772  1.1  christos __MISMATCH_TAGS_POP
    773  1.1  christos 
    774  1.1  christos #define CIRCLEQ_ENTRY(type)                                             \
    775  1.1  christos __MISMATCH_TAGS_PUSH                                                    \
    776  1.1  christos __NULLABILITY_COMPLETENESS_PUSH                                         \
    777  1.1  christos struct {                                                                \
    778  1.1  christos 	struct type *cqe_next;          /* next element */              \
    779  1.1  christos 	struct type *cqe_prev;          /* previous element */          \
    780  1.1  christos }                                                                       \
    781  1.1  christos __NULLABILITY_COMPLETENESS_POP                                         \
    782  1.1  christos __MISMATCH_TAGS_POP
    783  1.1  christos 
    784  1.1  christos /*
    785  1.1  christos  * Circular queue functions.
    786  1.1  christos  */
    787  1.1  christos #define CIRCLEQ_CHECK_HEAD(head, field)
    788  1.1  christos #define CIRCLEQ_CHECK_NEXT(head, elm, field)
    789  1.1  christos #define CIRCLEQ_CHECK_PREV(head, elm, field)
    790  1.1  christos 
    791  1.1  christos #define CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
    792  1.1  christos 
    793  1.1  christos #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
    794  1.1  christos 
    795  1.1  christos #define CIRCLEQ_FOREACH(var, head, field)                               \
    796  1.1  christos 	for((var) = (head)->cqh_first;                                  \
    797  1.1  christos 	    (var) != (void *)(head);                                    \
    798  1.1  christos 	    (var) = (var)->field.cqe_next)
    799  1.1  christos 
    800  1.1  christos #define CIRCLEQ_INIT(head) do {                                         \
    801  1.1  christos 	(head)->cqh_first = (void *)(head);                             \
    802  1.1  christos 	(head)->cqh_last = (void *)(head);                              \
    803  1.1  christos } while (0)
    804  1.1  christos 
    805  1.1  christos #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {            \
    806  1.1  christos 	CIRCLEQ_CHECK_NEXT(head, listelm, field);                       \
    807  1.1  christos 	(elm)->field.cqe_next = (listelm)->field.cqe_next;              \
    808  1.1  christos 	(elm)->field.cqe_prev = (listelm);                              \
    809  1.1  christos 	if ((listelm)->field.cqe_next == (void *)(head))                \
    810  1.1  christos 	        (head)->cqh_last = (elm);                               \
    811  1.1  christos 	else                                                            \
    812  1.1  christos 	        (listelm)->field.cqe_next->field.cqe_prev = (elm);      \
    813  1.1  christos 	(listelm)->field.cqe_next = (elm);                              \
    814  1.1  christos } while (0)
    815  1.1  christos 
    816  1.1  christos #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {           \
    817  1.1  christos 	CIRCLEQ_CHECK_PREV(head, listelm, field);                       \
    818  1.1  christos 	(elm)->field.cqe_next = (listelm);                              \
    819  1.1  christos 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
    820  1.1  christos 	if ((listelm)->field.cqe_prev == (void *)(head))                \
    821  1.1  christos 	        (head)->cqh_first = (elm);                              \
    822  1.1  christos 	else                                                            \
    823  1.1  christos 	        (listelm)->field.cqe_prev->field.cqe_next = (elm);      \
    824  1.1  christos 	(listelm)->field.cqe_prev = (elm);                              \
    825  1.1  christos } while (0)
    826  1.1  christos 
    827  1.1  christos #define CIRCLEQ_INSERT_HEAD(head, elm, field) do {                      \
    828  1.1  christos 	CIRCLEQ_CHECK_HEAD(head, field);                                \
    829  1.1  christos 	(elm)->field.cqe_next = (head)->cqh_first;                      \
    830  1.1  christos 	(elm)->field.cqe_prev = (void *)(head);                         \
    831  1.1  christos 	if ((head)->cqh_last == (void *)(head))                         \
    832  1.1  christos 	        (head)->cqh_last = (elm);                               \
    833  1.1  christos 	else                                                            \
    834  1.1  christos 	        (head)->cqh_first->field.cqe_prev = (elm);              \
    835  1.1  christos 	(head)->cqh_first = (elm);                                      \
    836  1.1  christos } while (0)
    837  1.1  christos 
    838  1.1  christos #define CIRCLEQ_INSERT_TAIL(head, elm, field) do {                      \
    839  1.1  christos 	(elm)->field.cqe_next = (void *)(head);                         \
    840  1.1  christos 	(elm)->field.cqe_prev = (head)->cqh_last;                       \
    841  1.1  christos 	if ((head)->cqh_first == (void *)(head))                        \
    842  1.1  christos 	        (head)->cqh_first = (elm);                              \
    843  1.1  christos 	else                                                            \
    844  1.1  christos 	        (head)->cqh_last->field.cqe_next = (elm);               \
    845  1.1  christos 	(head)->cqh_last = (elm);                                       \
    846  1.1  christos } while (0)
    847  1.1  christos 
    848  1.1  christos #define CIRCLEQ_LAST(head) ((head)->cqh_last)
    849  1.1  christos 
    850  1.1  christos #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
    851  1.1  christos 
    852  1.1  christos #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
    853  1.1  christos 
    854  1.1  christos #define CIRCLEQ_REMOVE(head, elm, field) do {                           \
    855  1.1  christos 	CIRCLEQ_CHECK_NEXT(head, elm, field);                           \
    856  1.1  christos 	CIRCLEQ_CHECK_PREV(head, elm, field);                           \
    857  1.1  christos 	if ((elm)->field.cqe_next == (void *)(head))                    \
    858  1.1  christos 	        (head)->cqh_last = (elm)->field.cqe_prev;               \
    859  1.1  christos 	else                                                            \
    860  1.1  christos 	        (elm)->field.cqe_next->field.cqe_prev =                 \
    861  1.1  christos 	            (elm)->field.cqe_prev;                              \
    862  1.1  christos 	if ((elm)->field.cqe_prev == (void *)(head))                    \
    863  1.1  christos 	        (head)->cqh_first = (elm)->field.cqe_next;              \
    864  1.1  christos 	else                                                            \
    865  1.1  christos 	        (elm)->field.cqe_prev->field.cqe_next =                 \
    866  1.1  christos 	            (elm)->field.cqe_next;                              \
    867  1.1  christos 	TRASHIT((elm)->field.cqe_next);                                 \
    868  1.1  christos 	TRASHIT((elm)->field.cqe_prev);                                 \
    869  1.1  christos } while (0)
    870  1.1  christos 
    871  1.1  christos #ifdef _KERNEL
    872  1.1  christos 
    873  1.1  christos #if NOTFB31
    874  1.1  christos 
    875  1.1  christos /*
    876  1.1  christos  * XXX insque() and remque() are an old way of handling certain queues.
    877  1.1  christos  * They bogusly assumes that all queue heads look alike.
    878  1.1  christos  */
    879  1.1  christos 
    880  1.1  christos struct quehead {
    881  1.1  christos 	struct quehead *qh_link;
    882  1.1  christos 	struct quehead *qh_rlink;
    883  1.1  christos };
    884  1.1  christos 
    885  1.1  christos #ifdef __GNUC__
    886  1.1  christos #define chkquenext(a)
    887  1.1  christos #define chkqueprev(a)
    888  1.1  christos 
    889  1.1  christos static __inline void
    890  1.1  christos insque(void *a, void *b)
    891  1.1  christos {
    892  1.1  christos 	struct quehead *element = (struct quehead *)a,
    893  1.1  christos 	    *head = (struct quehead *)b;
    894  1.1  christos 	chkquenext(head);
    895  1.1  christos 
    896  1.1  christos 	element->qh_link = head->qh_link;
    897  1.1  christos 	element->qh_rlink = head;
    898  1.1  christos 	head->qh_link = element;
    899  1.1  christos 	element->qh_link->qh_rlink = element;
    900  1.1  christos }
    901  1.1  christos 
    902  1.1  christos static __inline void
    903  1.1  christos remque(void *a)
    904  1.1  christos {
    905  1.1  christos 	struct quehead *element = (struct quehead *)a;
    906  1.1  christos 	chkquenext(element);
    907  1.1  christos 	chkqueprev(element);
    908  1.1  christos 
    909  1.1  christos 	element->qh_link->qh_rlink = element->qh_rlink;
    910  1.1  christos 	element->qh_rlink->qh_link = element->qh_link;
    911  1.1  christos 	element->qh_rlink = 0;
    912  1.1  christos }
    913  1.1  christos 
    914  1.1  christos #else /* !__GNUC__ */
    915  1.1  christos 
    916  1.1  christos void    insque(void *a, void *b);
    917  1.1  christos void    remque(void *a);
    918  1.1  christos 
    919  1.1  christos #endif /* __GNUC__ */
    920  1.1  christos 
    921  1.1  christos #endif /* NOTFB31 */
    922  1.1  christos #endif /* _KERNEL */
    923  1.1  christos 
    924  1.1  christos #endif /* !_SYS_QUEUE_H_ */
    925