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