sys-queue.h revision 181097
1/*	$OpenBSD: queue.h,v 1.25 2004/04/08 16:08:21 henning 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/* OPENBSD ORIGINAL: sys/sys/queue.h */
36
37#ifndef	_FAKE_QUEUE_H_
38#define	_FAKE_QUEUE_H_
39
40/*
41 * Require for OS/X and other platforms that have old/broken/incomplete
42 * <sys/queue.h>.
43 */
44#undef SLIST_HEAD
45#undef SLIST_HEAD_INITIALIZER
46#undef SLIST_ENTRY
47#undef SLIST_FOREACH_PREVPTR
48#undef SLIST_FIRST
49#undef SLIST_END
50#undef SLIST_EMPTY
51#undef SLIST_NEXT
52#undef SLIST_FOREACH
53#undef SLIST_INIT
54#undef SLIST_INSERT_AFTER
55#undef SLIST_INSERT_HEAD
56#undef SLIST_REMOVE_HEAD
57#undef SLIST_REMOVE
58#undef SLIST_REMOVE_NEXT
59#undef LIST_HEAD
60#undef LIST_HEAD_INITIALIZER
61#undef LIST_ENTRY
62#undef LIST_FIRST
63#undef LIST_END
64#undef LIST_EMPTY
65#undef LIST_NEXT
66#undef LIST_FOREACH
67#undef LIST_INIT
68#undef LIST_INSERT_AFTER
69#undef LIST_INSERT_BEFORE
70#undef LIST_INSERT_HEAD
71#undef LIST_REMOVE
72#undef LIST_REPLACE
73#undef SIMPLEQ_HEAD
74#undef SIMPLEQ_HEAD_INITIALIZER
75#undef SIMPLEQ_ENTRY
76#undef SIMPLEQ_FIRST
77#undef SIMPLEQ_END
78#undef SIMPLEQ_EMPTY
79#undef SIMPLEQ_NEXT
80#undef SIMPLEQ_FOREACH
81#undef SIMPLEQ_INIT
82#undef SIMPLEQ_INSERT_HEAD
83#undef SIMPLEQ_INSERT_TAIL
84#undef SIMPLEQ_INSERT_AFTER
85#undef SIMPLEQ_REMOVE_HEAD
86#undef TAILQ_HEAD
87#undef TAILQ_HEAD_INITIALIZER
88#undef TAILQ_ENTRY
89#undef TAILQ_FIRST
90#undef TAILQ_END
91#undef TAILQ_NEXT
92#undef TAILQ_LAST
93#undef TAILQ_PREV
94#undef TAILQ_EMPTY
95#undef TAILQ_FOREACH
96#undef TAILQ_FOREACH_REVERSE
97#undef TAILQ_INIT
98#undef TAILQ_INSERT_HEAD
99#undef TAILQ_INSERT_TAIL
100#undef TAILQ_INSERT_AFTER
101#undef TAILQ_INSERT_BEFORE
102#undef TAILQ_REMOVE
103#undef TAILQ_REPLACE
104#undef CIRCLEQ_HEAD
105#undef CIRCLEQ_HEAD_INITIALIZER
106#undef CIRCLEQ_ENTRY
107#undef CIRCLEQ_FIRST
108#undef CIRCLEQ_LAST
109#undef CIRCLEQ_END
110#undef CIRCLEQ_NEXT
111#undef CIRCLEQ_PREV
112#undef CIRCLEQ_EMPTY
113#undef CIRCLEQ_FOREACH
114#undef CIRCLEQ_FOREACH_REVERSE
115#undef CIRCLEQ_INIT
116#undef CIRCLEQ_INSERT_AFTER
117#undef CIRCLEQ_INSERT_BEFORE
118#undef CIRCLEQ_INSERT_HEAD
119#undef CIRCLEQ_INSERT_TAIL
120#undef CIRCLEQ_REMOVE
121#undef CIRCLEQ_REPLACE
122
123/*
124 * This file defines five types of data structures: singly-linked lists,
125 * lists, simple queues, tail queues, and circular queues.
126 *
127 *
128 * A singly-linked list is headed by a single forward pointer. The elements
129 * are singly linked for minimum space and pointer manipulation overhead at
130 * the expense of O(n) removal for arbitrary elements. New elements can be
131 * added to the list after an existing element or at the head of the list.
132 * Elements being removed from the head of the list should use the explicit
133 * macro for this purpose for optimum efficiency. A singly-linked list may
134 * only be traversed in the forward direction.  Singly-linked lists are ideal
135 * for applications with large datasets and few or no removals or for
136 * implementing a LIFO queue.
137 *
138 * A list is headed by a single forward pointer (or an array of forward
139 * pointers for a hash table header). The elements are doubly linked
140 * so that an arbitrary element can be removed without a need to
141 * traverse the list. New elements can be added to the list before
142 * or after an existing element or at the head of the list. A list
143 * may only be traversed in the forward direction.
144 *
145 * A simple queue is headed by a pair of pointers, one the head of the
146 * list and the other to the tail of the list. The elements are singly
147 * linked to save space, so elements can only be removed from the
148 * head of the list. New elements can be added to the list before or after
149 * an existing element, at the head of the list, or at the end of the
150 * list. A simple queue may only be traversed in the forward direction.
151 *
152 * A tail queue is headed by a pair of pointers, one to the head of the
153 * list and the other to the tail of the list. The elements are doubly
154 * linked so that an arbitrary element can be removed without a need to
155 * traverse the list. New elements can be added to the list before or
156 * after an existing element, at the head of the list, or at the end of
157 * the list. A tail queue may be traversed in either direction.
158 *
159 * A circle queue is headed by a pair of pointers, one to the head of the
160 * list and the other to the tail of the list. The elements are doubly
161 * linked so that an arbitrary element can be removed without a need to
162 * traverse the list. New elements can be added to the list before or after
163 * an existing element, at the head of the list, or at the end of the list.
164 * A circle queue may be traversed in either direction, but has a more
165 * complex end of list detection.
166 *
167 * For details on the use of these macros, see the queue(3) manual page.
168 */
169
170/*
171 * Singly-linked List definitions.
172 */
173#define SLIST_HEAD(name, type)						\
174struct name {								\
175	struct type *slh_first;	/* first element */			\
176}
177
178#define	SLIST_HEAD_INITIALIZER(head)					\
179	{ NULL }
180
181#define SLIST_ENTRY(type)						\
182struct {								\
183	struct type *sle_next;	/* next element */			\
184}
185
186/*
187 * Singly-linked List access methods.
188 */
189#define	SLIST_FIRST(head)	((head)->slh_first)
190#define	SLIST_END(head)		NULL
191#define	SLIST_EMPTY(head)	(SLIST_FIRST(head) == SLIST_END(head))
192#define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
193
194#define	SLIST_FOREACH(var, head, field)					\
195	for((var) = SLIST_FIRST(head);					\
196	    (var) != SLIST_END(head);					\
197	    (var) = SLIST_NEXT(var, field))
198
199#define	SLIST_FOREACH_PREVPTR(var, varp, head, field)			\
200	for ((varp) = &SLIST_FIRST((head));				\
201	    ((var) = *(varp)) != SLIST_END(head);			\
202	    (varp) = &SLIST_NEXT((var), field))
203
204/*
205 * Singly-linked List functions.
206 */
207#define	SLIST_INIT(head) {						\
208	SLIST_FIRST(head) = SLIST_END(head);				\
209}
210
211#define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
212	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
213	(slistelm)->field.sle_next = (elm);				\
214} while (0)
215
216#define	SLIST_INSERT_HEAD(head, elm, field) do {			\
217	(elm)->field.sle_next = (head)->slh_first;			\
218	(head)->slh_first = (elm);					\
219} while (0)
220
221#define	SLIST_REMOVE_NEXT(head, elm, field) do {			\
222	(elm)->field.sle_next = (elm)->field.sle_next->field.sle_next;	\
223} while (0)
224
225#define	SLIST_REMOVE_HEAD(head, field) do {				\
226	(head)->slh_first = (head)->slh_first->field.sle_next;		\
227} while (0)
228
229#define SLIST_REMOVE(head, elm, type, field) do {			\
230	if ((head)->slh_first == (elm)) {				\
231		SLIST_REMOVE_HEAD((head), field);			\
232	}								\
233	else {								\
234		struct type *curelm = (head)->slh_first;		\
235		while( curelm->field.sle_next != (elm) )		\
236			curelm = curelm->field.sle_next;		\
237		curelm->field.sle_next =				\
238		    curelm->field.sle_next->field.sle_next;		\
239	}								\
240} while (0)
241
242/*
243 * List definitions.
244 */
245#define LIST_HEAD(name, type)						\
246struct name {								\
247	struct type *lh_first;	/* first element */			\
248}
249
250#define LIST_HEAD_INITIALIZER(head)					\
251	{ NULL }
252
253#define LIST_ENTRY(type)						\
254struct {								\
255	struct type *le_next;	/* next element */			\
256	struct type **le_prev;	/* address of previous next element */	\
257}
258
259/*
260 * List access methods
261 */
262#define	LIST_FIRST(head)		((head)->lh_first)
263#define	LIST_END(head)			NULL
264#define	LIST_EMPTY(head)		(LIST_FIRST(head) == LIST_END(head))
265#define	LIST_NEXT(elm, field)		((elm)->field.le_next)
266
267#define LIST_FOREACH(var, head, field)					\
268	for((var) = LIST_FIRST(head);					\
269	    (var)!= LIST_END(head);					\
270	    (var) = LIST_NEXT(var, field))
271
272/*
273 * List functions.
274 */
275#define	LIST_INIT(head) do {						\
276	LIST_FIRST(head) = LIST_END(head);				\
277} while (0)
278
279#define LIST_INSERT_AFTER(listelm, elm, field) do {			\
280	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\
281		(listelm)->field.le_next->field.le_prev =		\
282		    &(elm)->field.le_next;				\
283	(listelm)->field.le_next = (elm);				\
284	(elm)->field.le_prev = &(listelm)->field.le_next;		\
285} while (0)
286
287#define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
288	(elm)->field.le_prev = (listelm)->field.le_prev;		\
289	(elm)->field.le_next = (listelm);				\
290	*(listelm)->field.le_prev = (elm);				\
291	(listelm)->field.le_prev = &(elm)->field.le_next;		\
292} while (0)
293
294#define LIST_INSERT_HEAD(head, elm, field) do {				\
295	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\
296		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
297	(head)->lh_first = (elm);					\
298	(elm)->field.le_prev = &(head)->lh_first;			\
299} while (0)
300
301#define LIST_REMOVE(elm, field) do {					\
302	if ((elm)->field.le_next != NULL)				\
303		(elm)->field.le_next->field.le_prev =			\
304		    (elm)->field.le_prev;				\
305	*(elm)->field.le_prev = (elm)->field.le_next;			\
306} while (0)
307
308#define LIST_REPLACE(elm, elm2, field) do {				\
309	if (((elm2)->field.le_next = (elm)->field.le_next) != NULL)	\
310		(elm2)->field.le_next->field.le_prev =			\
311		    &(elm2)->field.le_next;				\
312	(elm2)->field.le_prev = (elm)->field.le_prev;			\
313	*(elm2)->field.le_prev = (elm2);				\
314} while (0)
315
316/*
317 * Simple queue definitions.
318 */
319#define SIMPLEQ_HEAD(name, type)					\
320struct name {								\
321	struct type *sqh_first;	/* first element */			\
322	struct type **sqh_last;	/* addr of last next element */		\
323}
324
325#define SIMPLEQ_HEAD_INITIALIZER(head)					\
326	{ NULL, &(head).sqh_first }
327
328#define SIMPLEQ_ENTRY(type)						\
329struct {								\
330	struct type *sqe_next;	/* next element */			\
331}
332
333/*
334 * Simple queue access methods.
335 */
336#define	SIMPLEQ_FIRST(head)	    ((head)->sqh_first)
337#define	SIMPLEQ_END(head)	    NULL
338#define	SIMPLEQ_EMPTY(head)	    (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
339#define	SIMPLEQ_NEXT(elm, field)    ((elm)->field.sqe_next)
340
341#define SIMPLEQ_FOREACH(var, head, field)				\
342	for((var) = SIMPLEQ_FIRST(head);				\
343	    (var) != SIMPLEQ_END(head);					\
344	    (var) = SIMPLEQ_NEXT(var, field))
345
346/*
347 * Simple queue functions.
348 */
349#define	SIMPLEQ_INIT(head) do {						\
350	(head)->sqh_first = NULL;					\
351	(head)->sqh_last = &(head)->sqh_first;				\
352} while (0)
353
354#define SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
355	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
356		(head)->sqh_last = &(elm)->field.sqe_next;		\
357	(head)->sqh_first = (elm);					\
358} while (0)
359
360#define SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
361	(elm)->field.sqe_next = NULL;					\
362	*(head)->sqh_last = (elm);					\
363	(head)->sqh_last = &(elm)->field.sqe_next;			\
364} while (0)
365
366#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
367	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
368		(head)->sqh_last = &(elm)->field.sqe_next;		\
369	(listelm)->field.sqe_next = (elm);				\
370} while (0)
371
372#define SIMPLEQ_REMOVE_HEAD(head, elm, field) do {			\
373	if (((head)->sqh_first = (elm)->field.sqe_next) == NULL)	\
374		(head)->sqh_last = &(head)->sqh_first;			\
375} while (0)
376
377/*
378 * Tail queue definitions.
379 */
380#define TAILQ_HEAD(name, type)						\
381struct name {								\
382	struct type *tqh_first;	/* first element */			\
383	struct type **tqh_last;	/* addr of last next element */		\
384}
385
386#define TAILQ_HEAD_INITIALIZER(head)					\
387	{ NULL, &(head).tqh_first }
388
389#define TAILQ_ENTRY(type)						\
390struct {								\
391	struct type *tqe_next;	/* next element */			\
392	struct type **tqe_prev;	/* address of previous next element */	\
393}
394
395/*
396 * tail queue access methods
397 */
398#define	TAILQ_FIRST(head)		((head)->tqh_first)
399#define	TAILQ_END(head)			NULL
400#define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
401#define TAILQ_LAST(head, headname)					\
402	(*(((struct headname *)((head)->tqh_last))->tqh_last))
403/* XXX */
404#define TAILQ_PREV(elm, headname, field)				\
405	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
406#define	TAILQ_EMPTY(head)						\
407	(TAILQ_FIRST(head) == TAILQ_END(head))
408
409#define TAILQ_FOREACH(var, head, field)					\
410	for((var) = TAILQ_FIRST(head);					\
411	    (var) != TAILQ_END(head);					\
412	    (var) = TAILQ_NEXT(var, field))
413
414#define TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
415	for((var) = TAILQ_LAST(head, headname);				\
416	    (var) != TAILQ_END(head);					\
417	    (var) = TAILQ_PREV(var, headname, field))
418
419/*
420 * Tail queue functions.
421 */
422#define	TAILQ_INIT(head) do {						\
423	(head)->tqh_first = NULL;					\
424	(head)->tqh_last = &(head)->tqh_first;				\
425} while (0)
426
427#define TAILQ_INSERT_HEAD(head, elm, field) do {			\
428	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
429		(head)->tqh_first->field.tqe_prev =			\
430		    &(elm)->field.tqe_next;				\
431	else								\
432		(head)->tqh_last = &(elm)->field.tqe_next;		\
433	(head)->tqh_first = (elm);					\
434	(elm)->field.tqe_prev = &(head)->tqh_first;			\
435} while (0)
436
437#define TAILQ_INSERT_TAIL(head, elm, field) do {			\
438	(elm)->field.tqe_next = NULL;					\
439	(elm)->field.tqe_prev = (head)->tqh_last;			\
440	*(head)->tqh_last = (elm);					\
441	(head)->tqh_last = &(elm)->field.tqe_next;			\
442} while (0)
443
444#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
445	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
446		(elm)->field.tqe_next->field.tqe_prev =			\
447		    &(elm)->field.tqe_next;				\
448	else								\
449		(head)->tqh_last = &(elm)->field.tqe_next;		\
450	(listelm)->field.tqe_next = (elm);				\
451	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
452} while (0)
453
454#define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
455	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
456	(elm)->field.tqe_next = (listelm);				\
457	*(listelm)->field.tqe_prev = (elm);				\
458	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
459} while (0)
460
461#define TAILQ_REMOVE(head, elm, field) do {				\
462	if (((elm)->field.tqe_next) != NULL)				\
463		(elm)->field.tqe_next->field.tqe_prev =			\
464		    (elm)->field.tqe_prev;				\
465	else								\
466		(head)->tqh_last = (elm)->field.tqe_prev;		\
467	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
468} while (0)
469
470#define TAILQ_REPLACE(head, elm, elm2, field) do {			\
471	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\
472		(elm2)->field.tqe_next->field.tqe_prev =		\
473		    &(elm2)->field.tqe_next;				\
474	else								\
475		(head)->tqh_last = &(elm2)->field.tqe_next;		\
476	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\
477	*(elm2)->field.tqe_prev = (elm2);				\
478} while (0)
479
480/*
481 * Circular queue definitions.
482 */
483#define CIRCLEQ_HEAD(name, type)					\
484struct name {								\
485	struct type *cqh_first;		/* first element */		\
486	struct type *cqh_last;		/* last element */		\
487}
488
489#define CIRCLEQ_HEAD_INITIALIZER(head)					\
490	{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
491
492#define CIRCLEQ_ENTRY(type)						\
493struct {								\
494	struct type *cqe_next;		/* next element */		\
495	struct type *cqe_prev;		/* previous element */		\
496}
497
498/*
499 * Circular queue access methods
500 */
501#define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
502#define	CIRCLEQ_LAST(head)		((head)->cqh_last)
503#define	CIRCLEQ_END(head)		((void *)(head))
504#define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
505#define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
506#define	CIRCLEQ_EMPTY(head)						\
507	(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
508
509#define CIRCLEQ_FOREACH(var, head, field)				\
510	for((var) = CIRCLEQ_FIRST(head);				\
511	    (var) != CIRCLEQ_END(head);					\
512	    (var) = CIRCLEQ_NEXT(var, field))
513
514#define CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
515	for((var) = CIRCLEQ_LAST(head);					\
516	    (var) != CIRCLEQ_END(head);					\
517	    (var) = CIRCLEQ_PREV(var, field))
518
519/*
520 * Circular queue functions.
521 */
522#define	CIRCLEQ_INIT(head) do {						\
523	(head)->cqh_first = CIRCLEQ_END(head);				\
524	(head)->cqh_last = CIRCLEQ_END(head);				\
525} while (0)
526
527#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
528	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
529	(elm)->field.cqe_prev = (listelm);				\
530	if ((listelm)->field.cqe_next == CIRCLEQ_END(head))		\
531		(head)->cqh_last = (elm);				\
532	else								\
533		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
534	(listelm)->field.cqe_next = (elm);				\
535} while (0)
536
537#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\
538	(elm)->field.cqe_next = (listelm);				\
539	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
540	if ((listelm)->field.cqe_prev == CIRCLEQ_END(head))		\
541		(head)->cqh_first = (elm);				\
542	else								\
543		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
544	(listelm)->field.cqe_prev = (elm);				\
545} while (0)
546
547#define CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
548	(elm)->field.cqe_next = (head)->cqh_first;			\
549	(elm)->field.cqe_prev = CIRCLEQ_END(head);			\
550	if ((head)->cqh_last == CIRCLEQ_END(head))			\
551		(head)->cqh_last = (elm);				\
552	else								\
553		(head)->cqh_first->field.cqe_prev = (elm);		\
554	(head)->cqh_first = (elm);					\
555} while (0)
556
557#define CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
558	(elm)->field.cqe_next = CIRCLEQ_END(head);			\
559	(elm)->field.cqe_prev = (head)->cqh_last;			\
560	if ((head)->cqh_first == CIRCLEQ_END(head))			\
561		(head)->cqh_first = (elm);				\
562	else								\
563		(head)->cqh_last->field.cqe_next = (elm);		\
564	(head)->cqh_last = (elm);					\
565} while (0)
566
567#define	CIRCLEQ_REMOVE(head, elm, field) do {				\
568	if ((elm)->field.cqe_next == CIRCLEQ_END(head))			\
569		(head)->cqh_last = (elm)->field.cqe_prev;		\
570	else								\
571		(elm)->field.cqe_next->field.cqe_prev =			\
572		    (elm)->field.cqe_prev;				\
573	if ((elm)->field.cqe_prev == CIRCLEQ_END(head))			\
574		(head)->cqh_first = (elm)->field.cqe_next;		\
575	else								\
576		(elm)->field.cqe_prev->field.cqe_next =			\
577		    (elm)->field.cqe_next;				\
578} while (0)
579
580#define CIRCLEQ_REPLACE(head, elm, elm2, field) do {			\
581	if (((elm2)->field.cqe_next = (elm)->field.cqe_next) ==		\
582	    CIRCLEQ_END(head))						\
583		(head).cqh_last = (elm2);				\
584	else								\
585		(elm2)->field.cqe_next->field.cqe_prev = (elm2);	\
586	if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) ==		\
587	    CIRCLEQ_END(head))						\
588		(head).cqh_first = (elm2);				\
589	else								\
590		(elm2)->field.cqe_prev->field.cqe_next = (elm2);	\
591} while (0)
592
593#endif	/* !_FAKE_QUEUE_H_ */
594