1 /*	$OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange 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 _DWC_LIST_H_
36 #define _DWC_LIST_H_
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /** @file
43  *
44  * This file defines linked list operations.  It is derived from BSD with
45  * only the MACRO names being prefixed with DWC_.  This is because a few of
46  * these names conflict with those on Linux.  For documentation on use, see the
47  * inline comments in the source code.  The original license for this source
48  * code applies and is preserved in the dwc_list.h source file.
49  */
50 
51 /*
52  * This file defines five types of data structures: singly-linked lists,
53  * lists, simple queues, tail queues, and circular queues.
54  *
55  *
56  * A singly-linked list is headed by a single forward pointer. The elements
57  * are singly linked for minimum space and pointer manipulation overhead at
58  * the expense of O(n) removal for arbitrary elements. New elements can be
59  * added to the list after an existing element or at the head of the list.
60  * Elements being removed from the head of the list should use the explicit
61  * macro for this purpose for optimum efficiency. A singly-linked list may
62  * only be traversed in the forward direction.  Singly-linked lists are ideal
63  * for applications with large datasets and few or no removals or for
64  * implementing a LIFO queue.
65  *
66  * A list is headed by a single forward pointer (or an array of forward
67  * pointers for a hash table header). The elements are doubly linked
68  * 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
70  * or after an existing element or at the head of the list. A list
71  * may only be traversed in the forward direction.
72  *
73  * A simple queue is headed by a pair of pointers, one the head of the
74  * list and the other to the tail of the list. The elements are singly
75  * linked to save space, so elements can only be removed from the
76  * head of 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
78  * list. A simple queue may only be traversed in the forward direction.
79  *
80  * A tail queue is headed by a pair of pointers, one to the head of the
81  * list and the other to the tail of the list. The elements are doubly
82  * linked so that an arbitrary element can be removed without a need to
83  * traverse the list. New elements can be added to the list before or
84  * after an existing element, at the head of the list, or at the end of
85  * the list. A tail queue may be traversed in either direction.
86  *
87  * A circle queue is headed by a pair of pointers, one to the head of the
88  * list and the other to the tail of the list. The elements are doubly
89  * linked so that an arbitrary element can be removed without a need to
90  * traverse the list. New elements can be added to the list before or after
91  * an existing element, at the head of the list, or at the end of the list.
92  * A circle queue may be traversed in either direction, but has a more
93  * complex end of list detection.
94  *
95  * For details on the use of these macros, see the queue(3) manual page.
96  */
97 
98 /*
99  * Double-linked List.
100  */
101 
102 typedef struct dwc_list_link {
103 	struct dwc_list_link *next;
104 	struct dwc_list_link *prev;
105 } dwc_list_link_t;
106 
107 #define DWC_LIST_INIT(link) do {	\
108 	(link)->next = (link);		\
109 	(link)->prev = (link);		\
110 } while (0)
111 
112 #define DWC_LIST_FIRST(link)	((link)->next)
113 #define DWC_LIST_LAST(link)	((link)->prev)
114 #define DWC_LIST_END(link)	(link)
115 #define DWC_LIST_NEXT(link)	((link)->next)
116 #define DWC_LIST_PREV(link)	((link)->prev)
117 #define DWC_LIST_EMPTY(link)	\
118 	(DWC_LIST_FIRST(link) == DWC_LIST_END(link))
119 #define DWC_LIST_ENTRY(link, type, field)			\
120 	(type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
121 
122 #if 0
123 #define DWC_LIST_INSERT_HEAD(list, link) do {			\
124 	(link)->next = (list)->next;				\
125 	(link)->prev = (list);					\
126 	(list)->next->prev = (link);				\
127 	(list)->next = (link);					\
128 } while (0)
129 
130 #define DWC_LIST_INSERT_TAIL(list, link) do {			\
131 	(link)->next = (list);					\
132 	(link)->prev = (list)->prev;				\
133 	(list)->prev->next = (link);				\
134 	(list)->prev = (link);					\
135 } while (0)
136 #else
137 #define DWC_LIST_INSERT_HEAD(list, link) do {			\
138 	dwc_list_link_t *__next__ = (list)->next;		\
139 	__next__->prev = (link);				\
140 	(link)->next = __next__;				\
141 	(link)->prev = (list);					\
142 	(list)->next = (link);					\
143 } while (0)
144 
145 #define DWC_LIST_INSERT_TAIL(list, link) do {			\
146 	dwc_list_link_t *__prev__ = (list)->prev;		\
147 	(list)->prev = (link);					\
148 	(link)->next = (list);					\
149 	(link)->prev = __prev__;				\
150 	__prev__->next = (link);				\
151 } while (0)
152 #endif
153 
154 #if 0
155 static inline void __list_add(struct list_head *new,
156                               struct list_head *prev,
157                               struct list_head *next)
158 {
159         next->prev = new;
160         new->next = next;
161         new->prev = prev;
162         prev->next = new;
163 }
164 
165 static inline void list_add(struct list_head *new, struct list_head *head)
166 {
167         __list_add(new, head, head->next);
168 }
169 
170 static inline void list_add_tail(struct list_head *new, struct list_head *head)
171 {
172         __list_add(new, head->prev, head);
173 }
174 
175 static inline void __list_del(struct list_head * prev, struct list_head * next)
176 {
177         next->prev = prev;
178         prev->next = next;
179 }
180 
181 static inline void list_del(struct list_head *entry)
182 {
183         __list_del(entry->prev, entry->next);
184         entry->next = LIST_POISON1;
185         entry->prev = LIST_POISON2;
186 }
187 #endif
188 
189 #define DWC_LIST_REMOVE(link) do {				\
190 	(link)->next->prev = (link)->prev;			\
191 	(link)->prev->next = (link)->next;			\
192 } while (0)
193 
194 #define DWC_LIST_REMOVE_INIT(link) do {				\
195 	DWC_LIST_REMOVE(link);					\
196 	DWC_LIST_INIT(link);					\
197 } while (0)
198 
199 #define DWC_LIST_MOVE_HEAD(list, link) do {			\
200 	DWC_LIST_REMOVE(link);					\
201 	DWC_LIST_INSERT_HEAD(list, link);			\
202 } while (0)
203 
204 #define DWC_LIST_MOVE_TAIL(list, link) do {			\
205 	DWC_LIST_REMOVE(link);					\
206 	DWC_LIST_INSERT_TAIL(list, link);			\
207 } while (0)
208 
209 #define DWC_LIST_FOREACH(var, list)				\
210 	for((var) = DWC_LIST_FIRST(list);			\
211 	    (var) != DWC_LIST_END(list);			\
212 	    (var) = DWC_LIST_NEXT(var))
213 
214 #define DWC_LIST_FOREACH_SAFE(var, var2, list)			\
215 	for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var);	\
216 	    (var) != DWC_LIST_END(list);			\
217 	    (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
218 
219 #define DWC_LIST_FOREACH_REVERSE(var, list)			\
220 	for((var) = DWC_LIST_LAST(list);			\
221 	    (var) != DWC_LIST_END(list);			\
222 	    (var) = DWC_LIST_PREV(var))
223 
224 /*
225  * Singly-linked List definitions.
226  */
227 #define DWC_SLIST_HEAD(name, type)					\
228 struct name {								\
229 	struct type *slh_first;	/* first element */			\
230 }
231 
232 #define DWC_SLIST_HEAD_INITIALIZER(head)				\
233 	{ NULL }
234 
235 #define DWC_SLIST_ENTRY(type)						\
236 struct {								\
237 	struct type *sle_next;	/* next element */			\
238 }
239 
240 /*
241  * Singly-linked List access methods.
242  */
243 #define DWC_SLIST_FIRST(head)	((head)->slh_first)
244 #define DWC_SLIST_END(head)		NULL
245 #define DWC_SLIST_EMPTY(head)	(SLIST_FIRST(head) == SLIST_END(head))
246 #define DWC_SLIST_NEXT(elm, field)	((elm)->field.sle_next)
247 
248 #define DWC_SLIST_FOREACH(var, head, field)				\
249 	for((var) = SLIST_FIRST(head);					\
250 	    (var) != SLIST_END(head);					\
251 	    (var) = SLIST_NEXT(var, field))
252 
253 #define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field)		\
254 	for((varp) = &SLIST_FIRST((head));				\
255 	    ((var) = *(varp)) != SLIST_END(head);			\
256 	    (varp) = &SLIST_NEXT((var), field))
257 
258 /*
259  * Singly-linked List functions.
260  */
261 #define DWC_SLIST_INIT(head) {						\
262 	SLIST_FIRST(head) = SLIST_END(head);				\
263 }
264 
265 #define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do {		\
266 	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
267 	(slistelm)->field.sle_next = (elm);				\
268 } while (0)
269 
270 #define DWC_SLIST_INSERT_HEAD(head, elm, field) do {			\
271 	(elm)->field.sle_next = (head)->slh_first;			\
272 	(head)->slh_first = (elm);					\
273 } while (0)
274 
275 #define DWC_SLIST_REMOVE_NEXT(head, elm, field) do {			\
276 	(elm)->field.sle_next = (elm)->field.sle_next->field.sle_next;	\
277 } while (0)
278 
279 #define DWC_SLIST_REMOVE_HEAD(head, field) do {				\
280 	(head)->slh_first = (head)->slh_first->field.sle_next;		\
281 } while (0)
282 
283 #define DWC_SLIST_REMOVE(head, elm, type, field) do {			\
284 	if ((head)->slh_first == (elm)) {				\
285 		SLIST_REMOVE_HEAD((head), field);			\
286 	}								\
287 	else {								\
288 		struct type *curelm = (head)->slh_first;		\
289 		while( curelm->field.sle_next != (elm) )		\
290 			curelm = curelm->field.sle_next;		\
291 		curelm->field.sle_next =				\
292 		    curelm->field.sle_next->field.sle_next;		\
293 	}								\
294 } while (0)
295 
296 /*
297  * Simple queue definitions.
298  */
299 #define DWC_SIMPLEQ_HEAD(name, type)					\
300 struct name {								\
301 	struct type *sqh_first;	/* first element */			\
302 	struct type **sqh_last;	/* addr of last next element */		\
303 }
304 
305 #define DWC_SIMPLEQ_HEAD_INITIALIZER(head)				\
306 	{ NULL, &(head).sqh_first }
307 
308 #define DWC_SIMPLEQ_ENTRY(type)						\
309 struct {								\
310 	struct type *sqe_next;	/* next element */			\
311 }
312 
313 /*
314  * Simple queue access methods.
315  */
316 #define DWC_SIMPLEQ_FIRST(head)	    ((head)->sqh_first)
317 #define DWC_SIMPLEQ_END(head)	    NULL
318 #define DWC_SIMPLEQ_EMPTY(head)	    (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
319 #define DWC_SIMPLEQ_NEXT(elm, field)    ((elm)->field.sqe_next)
320 
321 #define DWC_SIMPLEQ_FOREACH(var, head, field)				\
322 	for((var) = SIMPLEQ_FIRST(head);				\
323 	    (var) != SIMPLEQ_END(head);					\
324 	    (var) = SIMPLEQ_NEXT(var, field))
325 
326 /*
327  * Simple queue functions.
328  */
329 #define DWC_SIMPLEQ_INIT(head) do {					\
330 	(head)->sqh_first = NULL;					\
331 	(head)->sqh_last = &(head)->sqh_first;				\
332 } while (0)
333 
334 #define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
335 	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
336 		(head)->sqh_last = &(elm)->field.sqe_next;		\
337 	(head)->sqh_first = (elm);					\
338 } while (0)
339 
340 #define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
341 	(elm)->field.sqe_next = NULL;					\
342 	*(head)->sqh_last = (elm);					\
343 	(head)->sqh_last = &(elm)->field.sqe_next;			\
344 } while (0)
345 
346 #define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {	\
347 	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
348 		(head)->sqh_last = &(elm)->field.sqe_next;		\
349 	(listelm)->field.sqe_next = (elm);				\
350 } while (0)
351 
352 #define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do {			\
353 	if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
354 		(head)->sqh_last = &(head)->sqh_first;			\
355 } while (0)
356 
357 /*
358  * Tail queue definitions.
359  */
360 #define DWC_TAILQ_HEAD(name, type)					\
361 struct name {								\
362 	struct type *tqh_first;	/* first element */			\
363 	struct type **tqh_last;	/* addr of last next element */		\
364 }
365 
366 #define DWC_TAILQ_HEAD_INITIALIZER(head)				\
367 	{ NULL, &(head).tqh_first }
368 
369 #define DWC_TAILQ_ENTRY(type)						\
370 struct {								\
371 	struct type *tqe_next;	/* next element */			\
372 	struct type **tqe_prev;	/* address of previous next element */	\
373 }
374 
375 /*
376  * tail queue access methods
377  */
378 #define DWC_TAILQ_FIRST(head)		((head)->tqh_first)
379 #define DWC_TAILQ_END(head)		NULL
380 #define DWC_TAILQ_NEXT(elm, field)	((elm)->field.tqe_next)
381 #define DWC_TAILQ_LAST(head, headname)					\
382 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
383 /* XXX */
384 #define DWC_TAILQ_PREV(elm, headname, field)				\
385 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
386 #define DWC_TAILQ_EMPTY(head)						\
387 	(TAILQ_FIRST(head) == TAILQ_END(head))
388 
389 #define DWC_TAILQ_FOREACH(var, head, field)				\
390 	for((var) = TAILQ_FIRST(head);					\
391 	    (var) != TAILQ_END(head);					\
392 	    (var) = TAILQ_NEXT(var, field))
393 
394 #define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
395 	for((var) = TAILQ_LAST(head, headname);				\
396 	    (var) != TAILQ_END(head);					\
397 	    (var) = TAILQ_PREV(var, headname, field))
398 
399 /*
400  * Tail queue functions.
401  */
402 #define DWC_TAILQ_INIT(head) do {					\
403 	(head)->tqh_first = NULL;					\
404 	(head)->tqh_last = &(head)->tqh_first;				\
405 } while (0)
406 
407 #define DWC_TAILQ_INSERT_HEAD(head, elm, field) do {			\
408 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
409 		(head)->tqh_first->field.tqe_prev =			\
410 		    &(elm)->field.tqe_next;				\
411 	else								\
412 		(head)->tqh_last = &(elm)->field.tqe_next;		\
413 	(head)->tqh_first = (elm);					\
414 	(elm)->field.tqe_prev = &(head)->tqh_first;			\
415 } while (0)
416 
417 #define DWC_TAILQ_INSERT_TAIL(head, elm, field) do {			\
418 	(elm)->field.tqe_next = NULL;					\
419 	(elm)->field.tqe_prev = (head)->tqh_last;			\
420 	*(head)->tqh_last = (elm);					\
421 	(head)->tqh_last = &(elm)->field.tqe_next;			\
422 } while (0)
423 
424 #define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
425 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
426 		(elm)->field.tqe_next->field.tqe_prev =			\
427 		    &(elm)->field.tqe_next;				\
428 	else								\
429 		(head)->tqh_last = &(elm)->field.tqe_next;		\
430 	(listelm)->field.tqe_next = (elm);				\
431 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
432 } while (0)
433 
434 #define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do {		\
435 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
436 	(elm)->field.tqe_next = (listelm);				\
437 	*(listelm)->field.tqe_prev = (elm);				\
438 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
439 } while (0)
440 
441 #define DWC_TAILQ_REMOVE(head, elm, field) do {				\
442 	if (((elm)->field.tqe_next) != NULL)				\
443 		(elm)->field.tqe_next->field.tqe_prev =			\
444 		    (elm)->field.tqe_prev;				\
445 	else								\
446 		(head)->tqh_last = (elm)->field.tqe_prev;		\
447 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
448 } while (0)
449 
450 #define DWC_TAILQ_REPLACE(head, elm, elm2, field) do {			\
451 	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\
452 		(elm2)->field.tqe_next->field.tqe_prev =		\
453 		    &(elm2)->field.tqe_next;				\
454 	else								\
455 		(head)->tqh_last = &(elm2)->field.tqe_next;		\
456 	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\
457 	*(elm2)->field.tqe_prev = (elm2);				\
458 } while (0)
459 
460 /*
461  * Circular queue definitions.
462  */
463 #define DWC_CIRCLEQ_HEAD(name, type)					\
464 struct name {								\
465 	struct type *cqh_first;		/* first element */		\
466 	struct type *cqh_last;		/* last element */		\
467 }
468 
469 #define DWC_CIRCLEQ_HEAD_INITIALIZER(head)				\
470 	{ DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
471 
472 #define DWC_CIRCLEQ_ENTRY(type)						\
473 struct {								\
474 	struct type *cqe_next;		/* next element */		\
475 	struct type *cqe_prev;		/* previous element */		\
476 }
477 
478 /*
479  * Circular queue access methods
480  */
481 #define DWC_CIRCLEQ_FIRST(head)		((head)->cqh_first)
482 #define DWC_CIRCLEQ_LAST(head)		((head)->cqh_last)
483 #define DWC_CIRCLEQ_END(head)		((void *)(head))
484 #define DWC_CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
485 #define DWC_CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
486 #define DWC_CIRCLEQ_EMPTY(head)						\
487 	(DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
488 
489 #define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
490 
491 #define DWC_CIRCLEQ_FOREACH(var, head, field)				\
492 	for((var) = DWC_CIRCLEQ_FIRST(head);				\
493 	    (var) != DWC_CIRCLEQ_END(head);				\
494 	    (var) = DWC_CIRCLEQ_NEXT(var, field))
495 
496 #define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field)			\
497 	for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
498 	    (var) != DWC_CIRCLEQ_END(head);					\
499 	    (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
500 
501 #define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
502 	for((var) = DWC_CIRCLEQ_LAST(head);				\
503 	    (var) != DWC_CIRCLEQ_END(head);				\
504 	    (var) = DWC_CIRCLEQ_PREV(var, field))
505 
506 /*
507  * Circular queue functions.
508  */
509 #define DWC_CIRCLEQ_INIT(head) do {					\
510 	(head)->cqh_first = DWC_CIRCLEQ_END(head);			\
511 	(head)->cqh_last = DWC_CIRCLEQ_END(head);			\
512 } while (0)
513 
514 #define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do {				\
515 	(elm)->field.cqe_next = NULL;					\
516 	(elm)->field.cqe_prev = NULL;					\
517 } while (0)
518 
519 #define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {	\
520 	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
521 	(elm)->field.cqe_prev = (listelm);				\
522 	if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head))		\
523 		(head)->cqh_last = (elm);				\
524 	else								\
525 		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
526 	(listelm)->field.cqe_next = (elm);				\
527 } while (0)
528 
529 #define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {	\
530 	(elm)->field.cqe_next = (listelm);				\
531 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
532 	if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head))		\
533 		(head)->cqh_first = (elm);				\
534 	else								\
535 		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
536 	(listelm)->field.cqe_prev = (elm);				\
537 } while (0)
538 
539 #define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
540 	(elm)->field.cqe_next = (head)->cqh_first;			\
541 	(elm)->field.cqe_prev = DWC_CIRCLEQ_END(head);			\
542 	if ((head)->cqh_last == DWC_CIRCLEQ_END(head))			\
543 		(head)->cqh_last = (elm);				\
544 	else								\
545 		(head)->cqh_first->field.cqe_prev = (elm);		\
546 	(head)->cqh_first = (elm);					\
547 } while (0)
548 
549 #define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
550 	(elm)->field.cqe_next = DWC_CIRCLEQ_END(head);			\
551 	(elm)->field.cqe_prev = (head)->cqh_last;			\
552 	if ((head)->cqh_first == DWC_CIRCLEQ_END(head))			\
553 		(head)->cqh_first = (elm);				\
554 	else								\
555 		(head)->cqh_last->field.cqe_next = (elm);		\
556 	(head)->cqh_last = (elm);					\
557 } while (0)
558 
559 #define DWC_CIRCLEQ_REMOVE(head, elm, field) do {			\
560 	if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head))		\
561 		(head)->cqh_last = (elm)->field.cqe_prev;		\
562 	else								\
563 		(elm)->field.cqe_next->field.cqe_prev =			\
564 		    (elm)->field.cqe_prev;				\
565 	if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head))		\
566 		(head)->cqh_first = (elm)->field.cqe_next;		\
567 	else								\
568 		(elm)->field.cqe_prev->field.cqe_next =			\
569 		    (elm)->field.cqe_next;				\
570 } while (0)
571 
572 #define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do {			\
573 	DWC_CIRCLEQ_REMOVE(head, elm, field);				\
574 	DWC_CIRCLEQ_INIT_ENTRY(elm, field);				\
575 } while (0)
576 
577 #define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do {		\
578 	if (((elm2)->field.cqe_next = (elm)->field.cqe_next) ==		\
579 	    DWC_CIRCLEQ_END(head))					\
580 		(head).cqh_last = (elm2);				\
581 	else								\
582 		(elm2)->field.cqe_next->field.cqe_prev = (elm2);	\
583 	if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) ==		\
584 	    DWC_CIRCLEQ_END(head))					\
585 		(head).cqh_first = (elm2);				\
586 	else								\
587 		(elm2)->field.cqe_prev->field.cqe_next = (elm2);	\
588 } while (0)
589 
590 #ifdef __cplusplus
591 }
592 #endif
593 
594 #endif /* _DWC_LIST_H_ */
595