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