__P() != wanted, begin removal, in order to preserve white space this needs
[dragonfly.git] / sys / sys / queue.h
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)queue.h     8.5 (Berkeley) 8/20/94
34  * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.7 2002/04/17 14:21:02 des Exp $
35  * $DragonFly: src/sys/sys/queue.h,v 1.3 2003/08/20 07:31:21 rob Exp $
36  */
37
38 #ifndef _SYS_QUEUE_H_
39 #define _SYS_QUEUE_H_
40
41 #include <machine/ansi.h>       /* for __offsetof */
42
43 /*
44  * This file defines five types of data structures: singly-linked lists,
45  * singly-linked tail queues, lists, tail queues, and circular queues.
46  *
47  * A singly-linked list is headed by a single forward pointer. The elements
48  * are singly linked for minimum space and pointer manipulation overhead at
49  * the expense of O(n) removal for arbitrary elements. New elements can be
50  * added to the list after an existing element or at the head of the list.
51  * Elements being removed from the head of the list should use the explicit
52  * macro for this purpose for optimum efficiency. A singly-linked list may
53  * only be traversed in the forward direction.  Singly-linked lists are ideal
54  * for applications with large datasets and few or no removals or for
55  * implementing a LIFO queue.
56  *
57  * A singly-linked tail queue is headed by a pair of pointers, one to the
58  * head of the list and the other to the tail of the list. The elements are
59  * singly linked for minimum space and pointer manipulation overhead at the
60  * expense of O(n) removal for arbitrary elements. New elements can be added
61  * to the list after an existing element, at the head of the list, or at the
62  * end of the list. Elements being removed from the head of the tail queue
63  * should use the explicit macro for this purpose for optimum efficiency.
64  * A singly-linked tail queue may only be traversed in the forward direction.
65  * Singly-linked tail queues are ideal for applications with large datasets
66  * and few or no removals or for implementing a FIFO queue.
67  *
68  * A list is headed by a single forward pointer (or an array of forward
69  * pointers for a hash table header). The elements are doubly linked
70  * so that an arbitrary element can be removed without a need to
71  * traverse the list. New elements can be added to the list before
72  * or after an existing element or at the head of the list. A list
73  * may only be traversed in the forward direction.
74  *
75  * A tail queue is headed by a pair of pointers, one to the head of the
76  * list and the other to the tail of the list. The elements are doubly
77  * linked so that an arbitrary element can be removed without a need to
78  * traverse the list. New elements can be added to the list before or
79  * after an existing element, at the head of the list, or at the end of
80  * the list. A tail queue may be traversed in either direction.
81  *
82  * A circle queue is headed by a pair of pointers, one to the head of the
83  * list and the other to the tail of the list. The elements are doubly
84  * linked so that an arbitrary element can be removed without a need to
85  * traverse the list. New elements can be added to the list before or after
86  * an existing element, at the head of the list, or at the end of the list.
87  * A circle queue may be traversed in either direction, but has a more
88  * complex end of list detection.
89  *
90  * For details on the use of these macros, see the queue(3) manual page.
91  *
92  *
93  *                      SLIST   LIST    STAILQ  TAILQ   CIRCLEQ
94  * _HEAD                +       +       +       +       +
95  * _HEAD_INITIALIZER    +       +       +       +       +
96  * _ENTRY               +       +       +       +       +
97  * _INIT                +       +       +       +       +
98  * _EMPTY               +       +       +       +       +
99  * _FIRST               +       +       +       +       +
100  * _NEXT                +       +       +       +       +
101  * _PREV                -       -       -       +       +
102  * _LAST                -       -       +       +       +
103  * _FOREACH             +       +       +       +       +
104  * _FOREACH_REVERSE     -       -       -       +       +
105  * _INSERT_HEAD         +       +       +       +       +
106  * _INSERT_BEFORE       -       +       -       +       +
107  * _INSERT_AFTER        +       +       +       +       +
108  * _INSERT_TAIL         -       -       +       +       +
109  * _REMOVE_HEAD         +       -       +       -       -
110  * _REMOVE              +       +       +       +       +
111  *
112  */
113
114 /*
115  * Singly-linked List declarations.
116  */
117 #define SLIST_HEAD(name, type)                                          \
118 struct name {                                                           \
119         struct type *slh_first; /* first element */                     \
120 }
121
122 #define SLIST_HEAD_INITIALIZER(head)                                    \
123         { NULL }
124  
125 #define SLIST_ENTRY(type)                                               \
126 struct {                                                                \
127         struct type *sle_next;  /* next element */                      \
128 }
129  
130 /*
131  * Singly-linked List functions.
132  */
133 #define SLIST_EMPTY(head)       ((head)->slh_first == NULL)
134
135 #define SLIST_FIRST(head)       ((head)->slh_first)
136
137 #define SLIST_FOREACH(var, head, field)                                 \
138         for ((var) = SLIST_FIRST((head));                               \
139             (var);                                                      \
140             (var) = SLIST_NEXT((var), field))
141
142 #define SLIST_INIT(head) do {                                           \
143         SLIST_FIRST((head)) = NULL;                                     \
144 } while (0)
145
146 #define SLIST_INSERT_AFTER(slistelm, elm, field) do {                   \
147         SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field);       \
148         SLIST_NEXT((slistelm), field) = (elm);                          \
149 } while (0)
150
151 #define SLIST_INSERT_HEAD(head, elm, field) do {                        \
152         SLIST_NEXT((elm), field) = SLIST_FIRST((head));                 \
153         SLIST_FIRST((head)) = (elm);                                    \
154 } while (0)
155
156 #define SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
157
158 #define SLIST_REMOVE(head, elm, type, field) do {                       \
159         if (SLIST_FIRST((head)) == (elm)) {                             \
160                 SLIST_REMOVE_HEAD((head), field);                       \
161         }                                                               \
162         else {                                                          \
163                 struct type *curelm = SLIST_FIRST((head));              \
164                 while (SLIST_NEXT(curelm, field) != (elm))              \
165                         curelm = SLIST_NEXT(curelm, field);             \
166                 SLIST_NEXT(curelm, field) =                             \
167                     SLIST_NEXT(SLIST_NEXT(curelm, field), field);       \
168         }                                                               \
169 } while (0)
170
171 #define SLIST_REMOVE_HEAD(head, field) do {                             \
172         SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field);   \
173 } while (0)
174
175 /*
176  * Singly-linked Tail queue declarations.
177  */
178 #define STAILQ_HEAD(name, type)                                         \
179 struct name {                                                           \
180         struct type *stqh_first;/* first element */                     \
181         struct type **stqh_last;/* addr of last next element */         \
182 }
183
184 #define STAILQ_HEAD_INITIALIZER(head)                                   \
185         { NULL, &(head).stqh_first }
186
187 #define STAILQ_ENTRY(type)                                              \
188 struct {                                                                \
189         struct type *stqe_next; /* next element */                      \
190 }
191
192 /*
193  * Singly-linked Tail queue functions.
194  */
195 #define STAILQ_EMPTY(head)      ((head)->stqh_first == NULL)
196
197 #define STAILQ_FIRST(head)      ((head)->stqh_first)
198
199 #define STAILQ_FOREACH(var, head, field)                                \
200         for((var) = STAILQ_FIRST((head));                               \
201            (var);                                                       \
202            (var) = STAILQ_NEXT((var), field))
203
204 #define STAILQ_INIT(head) do {                                          \
205         STAILQ_FIRST((head)) = NULL;                                    \
206         (head)->stqh_last = &STAILQ_FIRST((head));                      \
207 } while (0)
208
209 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {               \
210         if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
211                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
212         STAILQ_NEXT((tqelm), field) = (elm);                            \
213 } while (0)
214
215 #define STAILQ_INSERT_HEAD(head, elm, field) do {                       \
216         if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
217                 (head)->stqh_last = &STAILQ_NEXT((elm), field);         \
218         STAILQ_FIRST((head)) = (elm);                                   \
219 } while (0)
220
221 #define STAILQ_INSERT_TAIL(head, elm, field) do {                       \
222         STAILQ_NEXT((elm), field) = NULL;                               \
223         *(head)->stqh_last = (elm);                                     \
224         (head)->stqh_last = &STAILQ_NEXT((elm), field);                 \
225 } while (0)
226
227 #define STAILQ_LAST(head, type, field)                                  \
228         (STAILQ_EMPTY(head) ?                                           \
229                 NULL :                                                  \
230                 ((struct type *)                                        \
231                 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
232
233 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
234
235 #define STAILQ_REMOVE(head, elm, type, field) do {                      \
236         if (STAILQ_FIRST((head)) == (elm)) {                            \
237                 STAILQ_REMOVE_HEAD(head, field);                        \
238         }                                                               \
239         else {                                                          \
240                 struct type *curelm = STAILQ_FIRST((head));             \
241                 while (STAILQ_NEXT(curelm, field) != (elm))             \
242                         curelm = STAILQ_NEXT(curelm, field);            \
243                 if ((STAILQ_NEXT(curelm, field) =                       \
244                      STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
245                         (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
246         }                                                               \
247 } while (0)
248
249 #define STAILQ_REMOVE_HEAD(head, field) do {                            \
250         if ((STAILQ_FIRST((head)) =                                     \
251              STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL)         \
252                 (head)->stqh_last = &STAILQ_FIRST((head));              \
253 } while (0)
254
255 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {                 \
256         if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
257                 (head)->stqh_last = &STAILQ_FIRST((head));              \
258 } while (0)
259
260 /*
261  * List declarations.
262  */
263 #define LIST_HEAD(name, type)                                           \
264 struct name {                                                           \
265         struct type *lh_first;  /* first element */                     \
266 }
267
268 #define LIST_HEAD_INITIALIZER(head)                                     \
269         { NULL }
270
271 #define LIST_ENTRY(type)                                                \
272 struct {                                                                \
273         struct type *le_next;   /* next element */                      \
274         struct type **le_prev;  /* address of previous next element */  \
275 }
276
277 /*
278  * List functions.
279  */
280
281 #define LIST_EMPTY(head)        ((head)->lh_first == NULL)
282
283 #define LIST_FIRST(head)        ((head)->lh_first)
284
285 #define LIST_FOREACH(var, head, field)                                  \
286         for ((var) = LIST_FIRST((head));                                \
287             (var);                                                      \
288             (var) = LIST_NEXT((var), field))
289
290 #define LIST_INIT(head) do {                                            \
291         LIST_FIRST((head)) = NULL;                                      \
292 } while (0)
293
294 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
295         if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
296                 LIST_NEXT((listelm), field)->field.le_prev =            \
297                     &LIST_NEXT((elm), field);                           \
298         LIST_NEXT((listelm), field) = (elm);                            \
299         (elm)->field.le_prev = &LIST_NEXT((listelm), field);            \
300 } while (0)
301
302 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
303         (elm)->field.le_prev = (listelm)->field.le_prev;                \
304         LIST_NEXT((elm), field) = (listelm);                            \
305         *(listelm)->field.le_prev = (elm);                              \
306         (listelm)->field.le_prev = &LIST_NEXT((elm), field);            \
307 } while (0)
308
309 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
310         if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL)     \
311                 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
312         LIST_FIRST((head)) = (elm);                                     \
313         (elm)->field.le_prev = &LIST_FIRST((head));                     \
314 } while (0)
315
316 #define LIST_NEXT(elm, field)   ((elm)->field.le_next)
317
318 #define LIST_REMOVE(elm, field) do {                                    \
319         if (LIST_NEXT((elm), field) != NULL)                            \
320                 LIST_NEXT((elm), field)->field.le_prev =                \
321                     (elm)->field.le_prev;                               \
322         *(elm)->field.le_prev = LIST_NEXT((elm), field);                \
323 } while (0)
324
325 /*
326  * Tail queue declarations.
327  */
328 #define TAILQ_HEAD(name, type)                                          \
329 struct name {                                                           \
330         struct type *tqh_first; /* first element */                     \
331         struct type **tqh_last; /* addr of last next element */         \
332 }
333
334 #define TAILQ_HEAD_INITIALIZER(head)                                    \
335         { NULL, &(head).tqh_first }
336
337 #define TAILQ_ENTRY(type)                                               \
338 struct {                                                                \
339         struct type *tqe_next;  /* next element */                      \
340         struct type **tqe_prev; /* address of previous next element */  \
341 }
342
343 /*
344  * Tail queue functions.
345  */
346 #define TAILQ_EMPTY(head)       ((head)->tqh_first == NULL)
347
348 #define TAILQ_FIRST(head)       ((head)->tqh_first)
349
350 #define TAILQ_FOREACH(var, head, field)                                 \
351         for ((var) = TAILQ_FIRST((head));                               \
352             (var);                                                      \
353             (var) = TAILQ_NEXT((var), field))
354
355 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)               \
356         for ((var) = TAILQ_LAST((head), headname);                      \
357             (var);                                                      \
358             (var) = TAILQ_PREV((var), headname, field))
359
360 #define TAILQ_INIT(head) do {                                           \
361         TAILQ_FIRST((head)) = NULL;                                     \
362         (head)->tqh_last = &TAILQ_FIRST((head));                        \
363 } while (0)
364
365 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
366         if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
367                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
368                     &TAILQ_NEXT((elm), field);                          \
369         else                                                            \
370                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
371         TAILQ_NEXT((listelm), field) = (elm);                           \
372         (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);          \
373 } while (0)
374
375 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
376         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
377         TAILQ_NEXT((elm), field) = (listelm);                           \
378         *(listelm)->field.tqe_prev = (elm);                             \
379         (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);          \
380 } while (0)
381
382 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
383         if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)   \
384                 TAILQ_FIRST((head))->field.tqe_prev =                   \
385                     &TAILQ_NEXT((elm), field);                          \
386         else                                                            \
387                 (head)->tqh_last = &TAILQ_NEXT((elm), field);           \
388         TAILQ_FIRST((head)) = (elm);                                    \
389         (elm)->field.tqe_prev = &TAILQ_FIRST((head));                   \
390 } while (0)
391
392 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
393         TAILQ_NEXT((elm), field) = NULL;                                \
394         (elm)->field.tqe_prev = (head)->tqh_last;                       \
395         *(head)->tqh_last = (elm);                                      \
396         (head)->tqh_last = &TAILQ_NEXT((elm), field);                   \
397 } while (0)
398
399 #define TAILQ_LAST(head, headname)                                      \
400         (*(((struct headname *)((head)->tqh_last))->tqh_last))
401
402 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
403
404 #define TAILQ_PREV(elm, headname, field)                                \
405         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
406
407 #define TAILQ_REMOVE(head, elm, field) do {                             \
408         if ((TAILQ_NEXT((elm), field)) != NULL)                         \
409                 TAILQ_NEXT((elm), field)->field.tqe_prev =              \
410                     (elm)->field.tqe_prev;                              \
411         else                                                            \
412                 (head)->tqh_last = (elm)->field.tqe_prev;               \
413         *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);              \
414 } while (0)
415
416 /*
417  * Circular queue declarations.
418  */
419 #define CIRCLEQ_HEAD(name, type)                                        \
420 struct name {                                                           \
421         struct type *cqh_first;         /* first element */             \
422         struct type *cqh_last;          /* last element */              \
423 }
424
425 #define CIRCLEQ_HEAD_INITIALIZER(head)                                  \
426         { (void *)&(head), (void *)&(head) }
427
428 #define CIRCLEQ_ENTRY(type)                                             \
429 struct {                                                                \
430         struct type *cqe_next;          /* next element */              \
431         struct type *cqe_prev;          /* previous element */          \
432 }
433
434 /*
435  * Circular queue functions.
436  */
437 #define CIRCLEQ_EMPTY(head)     ((head)->cqh_first == (void *)(head))
438
439 #define CIRCLEQ_FIRST(head)     ((head)->cqh_first)
440
441 #define CIRCLEQ_FOREACH(var, head, field)                               \
442         for ((var) = CIRCLEQ_FIRST((head));                             \
443             (var) != (void *)(head) || ((var) = NULL);                  \
444             (var) = CIRCLEQ_NEXT((var), field))
445
446 #define CIRCLEQ_FOREACH_REVERSE(var, head, field)                       \
447         for ((var) = CIRCLEQ_LAST((head));                              \
448             (var) != (void *)(head) || ((var) = NULL);                  \
449             (var) = CIRCLEQ_PREV((var), field))
450
451 #define CIRCLEQ_INIT(head) do {                                         \
452         CIRCLEQ_FIRST((head)) = (void *)(head);                         \
453         CIRCLEQ_LAST((head)) = (void *)(head);                          \
454 } while (0)
455
456 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {            \
457         CIRCLEQ_NEXT((elm), field) = CIRCLEQ_NEXT((listelm), field);    \
458         CIRCLEQ_PREV((elm), field) = (listelm);                         \
459         if (CIRCLEQ_NEXT((listelm), field) == (void *)(head))           \
460                 CIRCLEQ_LAST((head)) = (elm);                           \
461         else                                                            \
462                 CIRCLEQ_PREV(CIRCLEQ_NEXT((listelm), field), field) = (elm);\
463         CIRCLEQ_NEXT((listelm), field) = (elm);                         \
464 } while (0)
465
466 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {           \
467         CIRCLEQ_NEXT((elm), field) = (listelm);                         \
468         CIRCLEQ_PREV((elm), field) = CIRCLEQ_PREV((listelm), field);    \
469         if (CIRCLEQ_PREV((listelm), field) == (void *)(head))           \
470                 CIRCLEQ_FIRST((head)) = (elm);                          \
471         else                                                            \
472                 CIRCLEQ_NEXT(CIRCLEQ_PREV((listelm), field), field) = (elm);\
473         CIRCLEQ_PREV((listelm), field) = (elm);                         \
474 } while (0)
475
476 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do {                      \
477         CIRCLEQ_NEXT((elm), field) = CIRCLEQ_FIRST((head));             \
478         CIRCLEQ_PREV((elm), field) = (void *)(head);                    \
479         if (CIRCLEQ_LAST((head)) == (void *)(head))                     \
480                 CIRCLEQ_LAST((head)) = (elm);                           \
481         else                                                            \
482                 CIRCLEQ_PREV(CIRCLEQ_FIRST((head)), field) = (elm);     \
483         CIRCLEQ_FIRST((head)) = (elm);                                  \
484 } while (0)
485
486 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do {                      \
487         CIRCLEQ_NEXT((elm), field) = (void *)(head);                    \
488         CIRCLEQ_PREV((elm), field) = CIRCLEQ_LAST((head));              \
489         if (CIRCLEQ_FIRST((head)) == (void *)(head))                    \
490                 CIRCLEQ_FIRST((head)) = (elm);                          \
491         else                                                            \
492                 CIRCLEQ_NEXT(CIRCLEQ_LAST((head)), field) = (elm);      \
493         CIRCLEQ_LAST((head)) = (elm);                                   \
494 } while (0)
495
496 #define CIRCLEQ_LAST(head)      ((head)->cqh_last)
497
498 #define CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
499
500 #define CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
501
502 #define CIRCLEQ_REMOVE(head, elm, field) do {                           \
503         if (CIRCLEQ_NEXT((elm), field) == (void *)(head))               \
504                 CIRCLEQ_LAST((head)) = CIRCLEQ_PREV((elm), field);      \
505         else                                                            \
506                 CIRCLEQ_PREV(CIRCLEQ_NEXT((elm), field), field) =       \
507                     CIRCLEQ_PREV((elm), field);                         \
508         if (CIRCLEQ_PREV((elm), field) == (void *)(head))               \
509                 CIRCLEQ_FIRST((head)) = CIRCLEQ_NEXT((elm), field);     \
510         else                                                            \
511                 CIRCLEQ_NEXT(CIRCLEQ_PREV((elm), field), field) =       \
512                     CIRCLEQ_NEXT((elm), field);                         \
513 } while (0)
514
515 #ifdef _KERNEL
516
517 /*
518  * XXX insque() and remque() are an old way of handling certain queues.
519  * They bogusly assumes that all queue heads look alike.
520  */
521
522 struct quehead {
523         struct quehead *qh_link;
524         struct quehead *qh_rlink;
525 };
526
527 #ifdef  __GNUC__
528
529 static __inline void
530 insque(void *a, void *b)
531 {
532         struct quehead *element = (struct quehead *)a,
533                  *head = (struct quehead *)b;
534
535         element->qh_link = head->qh_link;
536         element->qh_rlink = head;
537         head->qh_link = element;
538         element->qh_link->qh_rlink = element;
539 }
540
541 static __inline void
542 remque(void *a)
543 {
544         struct quehead *element = (struct quehead *)a;
545
546         element->qh_link->qh_rlink = element->qh_rlink;
547         element->qh_rlink->qh_link = element->qh_link;
548         element->qh_rlink = 0;
549 }
550
551 #else /* !__GNUC__ */
552
553 void    insque (void *a, void *b);
554 void    remque (void *a);
555
556 #endif /* __GNUC__ */
557
558 #endif /* _KERNEL */
559
560 #endif /* !_SYS_QUEUE_H_ */