xref: /freebsd/crypto/openssh/openbsd-compat/sys-queue.h (revision e858faa9bb929e040dfa2ee6f9e4806cec4d685d)
1 /*	$OpenBSD: queue.h,v 1.23 2003/06/02 23:28:21 millert Exp $	*/
2 /*	$NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
33  */
34 
35 #ifndef	_FAKE_QUEUE_H_
36 #define	_FAKE_QUEUE_H_
37 
38 /*
39  * Ignore all <sys/queue.h> since older platforms have broken/incomplete
40  * <sys/queue.h> that are too hard to work around.
41  */
42 #undef SLIST_HEAD
43 #undef SLIST_HEAD_INITIALIZER
44 #undef SLIST_ENTRY
45 #undef SLIST_FIRST
46 #undef SLIST_END
47 #undef SLIST_EMPTY
48 #undef SLIST_NEXT
49 #undef SLIST_FOREACH
50 #undef SLIST_INIT
51 #undef SLIST_INSERT_AFTER
52 #undef SLIST_INSERT_HEAD
53 #undef SLIST_REMOVE_HEAD
54 #undef SLIST_REMOVE
55 #undef LIST_HEAD
56 #undef LIST_HEAD_INITIALIZER
57 #undef LIST_ENTRY
58 #undef LIST_FIRST
59 #undef LIST_END
60 #undef LIST_EMPTY
61 #undef LIST_NEXT
62 #undef LIST_FOREACH
63 #undef LIST_INIT
64 #undef LIST_INSERT_AFTER
65 #undef LIST_INSERT_BEFORE
66 #undef LIST_INSERT_HEAD
67 #undef LIST_REMOVE
68 #undef LIST_REPLACE
69 #undef SIMPLEQ_HEAD
70 #undef SIMPLEQ_HEAD_INITIALIZER
71 #undef SIMPLEQ_ENTRY
72 #undef SIMPLEQ_FIRST
73 #undef SIMPLEQ_END
74 #undef SIMPLEQ_EMPTY
75 #undef SIMPLEQ_NEXT
76 #undef SIMPLEQ_FOREACH
77 #undef SIMPLEQ_INIT
78 #undef SIMPLEQ_INSERT_HEAD
79 #undef SIMPLEQ_INSERT_TAIL
80 #undef SIMPLEQ_INSERT_AFTER
81 #undef SIMPLEQ_REMOVE_HEAD
82 #undef TAILQ_HEAD
83 #undef TAILQ_HEAD_INITIALIZER
84 #undef TAILQ_ENTRY
85 #undef TAILQ_FIRST
86 #undef TAILQ_END
87 #undef TAILQ_NEXT
88 #undef TAILQ_LAST
89 #undef TAILQ_PREV
90 #undef TAILQ_EMPTY
91 #undef TAILQ_FOREACH
92 #undef TAILQ_FOREACH_REVERSE
93 #undef TAILQ_INIT
94 #undef TAILQ_INSERT_HEAD
95 #undef TAILQ_INSERT_TAIL
96 #undef TAILQ_INSERT_AFTER
97 #undef TAILQ_INSERT_BEFORE
98 #undef TAILQ_REMOVE
99 #undef TAILQ_REPLACE
100 #undef CIRCLEQ_HEAD
101 #undef CIRCLEQ_HEAD_INITIALIZER
102 #undef CIRCLEQ_ENTRY
103 #undef CIRCLEQ_FIRST
104 #undef CIRCLEQ_LAST
105 #undef CIRCLEQ_END
106 #undef CIRCLEQ_NEXT
107 #undef CIRCLEQ_PREV
108 #undef CIRCLEQ_EMPTY
109 #undef CIRCLEQ_FOREACH
110 #undef CIRCLEQ_FOREACH_REVERSE
111 #undef CIRCLEQ_INIT
112 #undef CIRCLEQ_INSERT_AFTER
113 #undef CIRCLEQ_INSERT_BEFORE
114 #undef CIRCLEQ_INSERT_HEAD
115 #undef CIRCLEQ_INSERT_TAIL
116 #undef CIRCLEQ_REMOVE
117 #undef CIRCLEQ_REPLACE
118 
119 /*
120  * This file defines five types of data structures: singly-linked lists,
121  * lists, simple queues, tail queues, and circular queues.
122  *
123  *
124  * A singly-linked list is headed by a single forward pointer. The elements
125  * are singly linked for minimum space and pointer manipulation overhead at
126  * the expense of O(n) removal for arbitrary elements. New elements can be
127  * added to the list after an existing element or at the head of the list.
128  * Elements being removed from the head of the list should use the explicit
129  * macro for this purpose for optimum efficiency. A singly-linked list may
130  * only be traversed in the forward direction.  Singly-linked lists are ideal
131  * for applications with large datasets and few or no removals or for
132  * implementing a LIFO queue.
133  *
134  * A list is headed by a single forward pointer (or an array of forward
135  * pointers for a hash table header). The elements are doubly linked
136  * so that an arbitrary element can be removed without a need to
137  * traverse the list. New elements can be added to the list before
138  * or after an existing element or at the head of the list. A list
139  * may only be traversed in the forward direction.
140  *
141  * A simple queue is headed by a pair of pointers, one the head of the
142  * list and the other to the tail of the list. The elements are singly
143  * linked to save space, so elements can only be removed from the
144  * head of the list. New elements can be added to the list before or after
145  * an existing element, at the head of the list, or at the end of the
146  * list. A simple queue may only be traversed in the forward direction.
147  *
148  * A tail queue is headed by a pair of pointers, one to the head of the
149  * list and the other to the tail of the list. The elements are doubly
150  * linked so that an arbitrary element can be removed without a need to
151  * traverse the list. New elements can be added to the list before or
152  * after an existing element, at the head of the list, or at the end of
153  * the list. A tail queue may be traversed in either direction.
154  *
155  * A circle queue is headed by a pair of pointers, one to the head of the
156  * list and the other to the tail of the list. The elements are doubly
157  * linked so that an arbitrary element can be removed without a need to
158  * traverse the list. New elements can be added to the list before or after
159  * an existing element, at the head of the list, or at the end of the list.
160  * A circle queue may be traversed in either direction, but has a more
161  * complex end of list detection.
162  *
163  * For details on the use of these macros, see the queue(3) manual page.
164  */
165 
166 /*
167  * Singly-linked List definitions.
168  */
169 #define SLIST_HEAD(name, type)						\
170 struct name {								\
171 	struct type *slh_first;	/* first element */			\
172 }
173 
174 #define	SLIST_HEAD_INITIALIZER(head)					\
175 	{ NULL }
176 
177 #define SLIST_ENTRY(type)						\
178 struct {								\
179 	struct type *sle_next;	/* next element */			\
180 }
181 
182 /*
183  * Singly-linked List access methods.
184  */
185 #define	SLIST_FIRST(head)	((head)->slh_first)
186 #define	SLIST_END(head)		NULL
187 #define	SLIST_EMPTY(head)	(SLIST_FIRST(head) == SLIST_END(head))
188 #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
189 
190 #define	SLIST_FOREACH(var, head, field)					\
191 	for((var) = SLIST_FIRST(head);					\
192 	    (var) != SLIST_END(head);					\
193 	    (var) = SLIST_NEXT(var, field))
194 
195 /*
196  * Singly-linked List functions.
197  */
198 #define	SLIST_INIT(head) {						\
199 	SLIST_FIRST(head) = SLIST_END(head);				\
200 }
201 
202 #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
203 	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
204 	(slistelm)->field.sle_next = (elm);				\
205 } while (0)
206 
207 #define	SLIST_INSERT_HEAD(head, elm, field) do {			\
208 	(elm)->field.sle_next = (head)->slh_first;			\
209 	(head)->slh_first = (elm);					\
210 } while (0)
211 
212 #define	SLIST_REMOVE_HEAD(head, field) do {				\
213 	(head)->slh_first = (head)->slh_first->field.sle_next;		\
214 } while (0)
215 
216 #define SLIST_REMOVE(head, elm, type, field) do {			\
217 	if ((head)->slh_first == (elm)) {				\
218 		SLIST_REMOVE_HEAD((head), field);			\
219 	}								\
220 	else {								\
221 		struct type *curelm = (head)->slh_first;		\
222 		while( curelm->field.sle_next != (elm) )		\
223 			curelm = curelm->field.sle_next;		\
224 		curelm->field.sle_next =				\
225 		    curelm->field.sle_next->field.sle_next;		\
226 	}								\
227 } while (0)
228 
229 /*
230  * List definitions.
231  */
232 #define LIST_HEAD(name, type)						\
233 struct name {								\
234 	struct type *lh_first;	/* first element */			\
235 }
236 
237 #define LIST_HEAD_INITIALIZER(head)					\
238 	{ NULL }
239 
240 #define LIST_ENTRY(type)						\
241 struct {								\
242 	struct type *le_next;	/* next element */			\
243 	struct type **le_prev;	/* address of previous next element */	\
244 }
245 
246 /*
247  * List access methods
248  */
249 #define	LIST_FIRST(head)		((head)->lh_first)
250 #define	LIST_END(head)			NULL
251 #define	LIST_EMPTY(head)		(LIST_FIRST(head) == LIST_END(head))
252 #define	LIST_NEXT(elm, field)		((elm)->field.le_next)
253 
254 #define LIST_FOREACH(var, head, field)					\
255 	for((var) = LIST_FIRST(head);					\
256 	    (var)!= LIST_END(head);					\
257 	    (var) = LIST_NEXT(var, field))
258 
259 /*
260  * List functions.
261  */
262 #define	LIST_INIT(head) do {						\
263 	LIST_FIRST(head) = LIST_END(head);				\
264 } while (0)
265 
266 #define LIST_INSERT_AFTER(listelm, elm, field) do {			\
267 	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\
268 		(listelm)->field.le_next->field.le_prev =		\
269 		    &(elm)->field.le_next;				\
270 	(listelm)->field.le_next = (elm);				\
271 	(elm)->field.le_prev = &(listelm)->field.le_next;		\
272 } while (0)
273 
274 #define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
275 	(elm)->field.le_prev = (listelm)->field.le_prev;		\
276 	(elm)->field.le_next = (listelm);				\
277 	*(listelm)->field.le_prev = (elm);				\
278 	(listelm)->field.le_prev = &(elm)->field.le_next;		\
279 } while (0)
280 
281 #define LIST_INSERT_HEAD(head, elm, field) do {				\
282 	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\
283 		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
284 	(head)->lh_first = (elm);					\
285 	(elm)->field.le_prev = &(head)->lh_first;			\
286 } while (0)
287 
288 #define LIST_REMOVE(elm, field) do {					\
289 	if ((elm)->field.le_next != NULL)				\
290 		(elm)->field.le_next->field.le_prev =			\
291 		    (elm)->field.le_prev;				\
292 	*(elm)->field.le_prev = (elm)->field.le_next;			\
293 } while (0)
294 
295 #define LIST_REPLACE(elm, elm2, field) do {				\
296 	if (((elm2)->field.le_next = (elm)->field.le_next) != NULL)	\
297 		(elm2)->field.le_next->field.le_prev =			\
298 		    &(elm2)->field.le_next;				\
299 	(elm2)->field.le_prev = (elm)->field.le_prev;			\
300 	*(elm2)->field.le_prev = (elm2);				\
301 } while (0)
302 
303 /*
304  * Simple queue definitions.
305  */
306 #define SIMPLEQ_HEAD(name, type)					\
307 struct name {								\
308 	struct type *sqh_first;	/* first element */			\
309 	struct type **sqh_last;	/* addr of last next element */		\
310 }
311 
312 #define SIMPLEQ_HEAD_INITIALIZER(head)					\
313 	{ NULL, &(head).sqh_first }
314 
315 #define SIMPLEQ_ENTRY(type)						\
316 struct {								\
317 	struct type *sqe_next;	/* next element */			\
318 }
319 
320 /*
321  * Simple queue access methods.
322  */
323 #define	SIMPLEQ_FIRST(head)	    ((head)->sqh_first)
324 #define	SIMPLEQ_END(head)	    NULL
325 #define	SIMPLEQ_EMPTY(head)	    (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
326 #define	SIMPLEQ_NEXT(elm, field)    ((elm)->field.sqe_next)
327 
328 #define SIMPLEQ_FOREACH(var, head, field)				\
329 	for((var) = SIMPLEQ_FIRST(head);				\
330 	    (var) != SIMPLEQ_END(head);					\
331 	    (var) = SIMPLEQ_NEXT(var, field))
332 
333 /*
334  * Simple queue functions.
335  */
336 #define	SIMPLEQ_INIT(head) do {						\
337 	(head)->sqh_first = NULL;					\
338 	(head)->sqh_last = &(head)->sqh_first;				\
339 } while (0)
340 
341 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
342 	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
343 		(head)->sqh_last = &(elm)->field.sqe_next;		\
344 	(head)->sqh_first = (elm);					\
345 } while (0)
346 
347 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
348 	(elm)->field.sqe_next = NULL;					\
349 	*(head)->sqh_last = (elm);					\
350 	(head)->sqh_last = &(elm)->field.sqe_next;			\
351 } while (0)
352 
353 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
354 	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
355 		(head)->sqh_last = &(elm)->field.sqe_next;		\
356 	(listelm)->field.sqe_next = (elm);				\
357 } while (0)
358 
359 #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do {			\
360 	if (((head)->sqh_first = (elm)->field.sqe_next) == NULL)	\
361 		(head)->sqh_last = &(head)->sqh_first;			\
362 } while (0)
363 
364 /*
365  * Tail queue definitions.
366  */
367 #define TAILQ_HEAD(name, type)						\
368 struct name {								\
369 	struct type *tqh_first;	/* first element */			\
370 	struct type **tqh_last;	/* addr of last next element */		\
371 }
372 
373 #define TAILQ_HEAD_INITIALIZER(head)					\
374 	{ NULL, &(head).tqh_first }
375 
376 #define TAILQ_ENTRY(type)						\
377 struct {								\
378 	struct type *tqe_next;	/* next element */			\
379 	struct type **tqe_prev;	/* address of previous next element */	\
380 }
381 
382 /*
383  * tail queue access methods
384  */
385 #define	TAILQ_FIRST(head)		((head)->tqh_first)
386 #define	TAILQ_END(head)			NULL
387 #define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
388 #define TAILQ_LAST(head, headname)					\
389 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
390 /* XXX */
391 #define TAILQ_PREV(elm, headname, field)				\
392 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
393 #define	TAILQ_EMPTY(head)						\
394 	(TAILQ_FIRST(head) == TAILQ_END(head))
395 
396 #define TAILQ_FOREACH(var, head, field)					\
397 	for((var) = TAILQ_FIRST(head);					\
398 	    (var) != TAILQ_END(head);					\
399 	    (var) = TAILQ_NEXT(var, field))
400 
401 #define TAILQ_FOREACH_REVERSE(var, head, field, headname)		\
402 	for((var) = TAILQ_LAST(head, headname);				\
403 	    (var) != TAILQ_END(head);					\
404 	    (var) = TAILQ_PREV(var, headname, field))
405 
406 /*
407  * Tail queue functions.
408  */
409 #define	TAILQ_INIT(head) do {						\
410 	(head)->tqh_first = NULL;					\
411 	(head)->tqh_last = &(head)->tqh_first;				\
412 } while (0)
413 
414 #define TAILQ_INSERT_HEAD(head, elm, field) do {			\
415 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
416 		(head)->tqh_first->field.tqe_prev =			\
417 		    &(elm)->field.tqe_next;				\
418 	else								\
419 		(head)->tqh_last = &(elm)->field.tqe_next;		\
420 	(head)->tqh_first = (elm);					\
421 	(elm)->field.tqe_prev = &(head)->tqh_first;			\
422 } while (0)
423 
424 #define TAILQ_INSERT_TAIL(head, elm, field) do {			\
425 	(elm)->field.tqe_next = NULL;					\
426 	(elm)->field.tqe_prev = (head)->tqh_last;			\
427 	*(head)->tqh_last = (elm);					\
428 	(head)->tqh_last = &(elm)->field.tqe_next;			\
429 } while (0)
430 
431 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
432 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
433 		(elm)->field.tqe_next->field.tqe_prev =			\
434 		    &(elm)->field.tqe_next;				\
435 	else								\
436 		(head)->tqh_last = &(elm)->field.tqe_next;		\
437 	(listelm)->field.tqe_next = (elm);				\
438 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
439 } while (0)
440 
441 #define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
442 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
443 	(elm)->field.tqe_next = (listelm);				\
444 	*(listelm)->field.tqe_prev = (elm);				\
445 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
446 } while (0)
447 
448 #define TAILQ_REMOVE(head, elm, field) do {				\
449 	if (((elm)->field.tqe_next) != NULL)				\
450 		(elm)->field.tqe_next->field.tqe_prev =			\
451 		    (elm)->field.tqe_prev;				\
452 	else								\
453 		(head)->tqh_last = (elm)->field.tqe_prev;		\
454 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
455 } while (0)
456 
457 #define TAILQ_REPLACE(head, elm, elm2, field) do {			\
458 	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\
459 		(elm2)->field.tqe_next->field.tqe_prev =		\
460 		    &(elm2)->field.tqe_next;				\
461 	else								\
462 		(head)->tqh_last = &(elm2)->field.tqe_next;		\
463 	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\
464 	*(elm2)->field.tqe_prev = (elm2);				\
465 } while (0)
466 
467 /*
468  * Circular queue definitions.
469  */
470 #define CIRCLEQ_HEAD(name, type)					\
471 struct name {								\
472 	struct type *cqh_first;		/* first element */		\
473 	struct type *cqh_last;		/* last element */		\
474 }
475 
476 #define CIRCLEQ_HEAD_INITIALIZER(head)					\
477 	{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
478 
479 #define CIRCLEQ_ENTRY(type)						\
480 struct {								\
481 	struct type *cqe_next;		/* next element */		\
482 	struct type *cqe_prev;		/* previous element */		\
483 }
484 
485 /*
486  * Circular queue access methods
487  */
488 #define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
489 #define	CIRCLEQ_LAST(head)		((head)->cqh_last)
490 #define	CIRCLEQ_END(head)		((void *)(head))
491 #define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
492 #define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
493 #define	CIRCLEQ_EMPTY(head)						\
494 	(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
495 
496 #define CIRCLEQ_FOREACH(var, head, field)				\
497 	for((var) = CIRCLEQ_FIRST(head);				\
498 	    (var) != CIRCLEQ_END(head);					\
499 	    (var) = CIRCLEQ_NEXT(var, field))
500 
501 #define CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
502 	for((var) = CIRCLEQ_LAST(head);					\
503 	    (var) != CIRCLEQ_END(head);					\
504 	    (var) = CIRCLEQ_PREV(var, field))
505 
506 /*
507  * Circular queue functions.
508  */
509 #define	CIRCLEQ_INIT(head) do {						\
510 	(head)->cqh_first = CIRCLEQ_END(head);				\
511 	(head)->cqh_last = CIRCLEQ_END(head);				\
512 } while (0)
513 
514 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
515 	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
516 	(elm)->field.cqe_prev = (listelm);				\
517 	if ((listelm)->field.cqe_next == CIRCLEQ_END(head))		\
518 		(head)->cqh_last = (elm);				\
519 	else								\
520 		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
521 	(listelm)->field.cqe_next = (elm);				\
522 } while (0)
523 
524 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\
525 	(elm)->field.cqe_next = (listelm);				\
526 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
527 	if ((listelm)->field.cqe_prev == CIRCLEQ_END(head))		\
528 		(head)->cqh_first = (elm);				\
529 	else								\
530 		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
531 	(listelm)->field.cqe_prev = (elm);				\
532 } while (0)
533 
534 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
535 	(elm)->field.cqe_next = (head)->cqh_first;			\
536 	(elm)->field.cqe_prev = CIRCLEQ_END(head);			\
537 	if ((head)->cqh_last == CIRCLEQ_END(head))			\
538 		(head)->cqh_last = (elm);				\
539 	else								\
540 		(head)->cqh_first->field.cqe_prev = (elm);		\
541 	(head)->cqh_first = (elm);					\
542 } while (0)
543 
544 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
545 	(elm)->field.cqe_next = CIRCLEQ_END(head);			\
546 	(elm)->field.cqe_prev = (head)->cqh_last;			\
547 	if ((head)->cqh_first == CIRCLEQ_END(head))			\
548 		(head)->cqh_first = (elm);				\
549 	else								\
550 		(head)->cqh_last->field.cqe_next = (elm);		\
551 	(head)->cqh_last = (elm);					\
552 } while (0)
553 
554 #define	CIRCLEQ_REMOVE(head, elm, field) do {				\
555 	if ((elm)->field.cqe_next == CIRCLEQ_END(head))			\
556 		(head)->cqh_last = (elm)->field.cqe_prev;		\
557 	else								\
558 		(elm)->field.cqe_next->field.cqe_prev =			\
559 		    (elm)->field.cqe_prev;				\
560 	if ((elm)->field.cqe_prev == CIRCLEQ_END(head))			\
561 		(head)->cqh_first = (elm)->field.cqe_next;		\
562 	else								\
563 		(elm)->field.cqe_prev->field.cqe_next =			\
564 		    (elm)->field.cqe_next;				\
565 } while (0)
566 
567 #define CIRCLEQ_REPLACE(head, elm, elm2, field) do {			\
568 	if (((elm2)->field.cqe_next = (elm)->field.cqe_next) ==		\
569 	    CIRCLEQ_END(head))						\
570 		(head).cqh_last = (elm2);				\
571 	else								\
572 		(elm2)->field.cqe_next->field.cqe_prev = (elm2);	\
573 	if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) ==		\
574 	    CIRCLEQ_END(head))						\
575 		(head).cqh_first = (elm2);				\
576 	else								\
577 		(elm2)->field.cqe_prev->field.cqe_next = (elm2);	\
578 } while (0)
579 
580 #endif	/* !_FAKE_QUEUE_H_ */
581