rk: restore file mode
[firefly-linux-kernel-4.4.55.git] / drivers / usb / dwc_otg_310 / common_port / 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 _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,
166                                       struct list_head *head)
167         {
168                 __list_add(new, head, head->next);
169         }
170
171         static inline void list_add_tail(struct list_head *new,
172                                          struct list_head *head)
173         {
174                 __list_add(new, head->prev, head);
175         }
176
177         static inline void __list_del(struct list_head *prev,
178                                       struct list_head *next)
179         {
180                 next->prev = prev;
181                 prev->next = next;
182         }
183
184         static inline void list_del(struct list_head *entry)
185         {
186                 __list_del(entry->prev, entry->next);
187                 entry->next = LIST_POISON1;
188                 entry->prev = LIST_POISON2;
189         }
190 #endif
191
192 #define DWC_LIST_REMOVE(link) do {                              \
193         (link)->next->prev = (link)->prev;                      \
194         (link)->prev->next = (link)->next;                      \
195 } while (0)
196
197 #define DWC_LIST_REMOVE_INIT(link) do {                         \
198         DWC_LIST_REMOVE(link);                                  \
199         DWC_LIST_INIT(link);                                    \
200 } while (0)
201
202 #define DWC_LIST_MOVE_HEAD(list, link) do {                     \
203         DWC_LIST_REMOVE(link);                                  \
204         DWC_LIST_INSERT_HEAD(list, link);                       \
205 } while (0)
206
207 #define DWC_LIST_MOVE_TAIL(list, link) do {                     \
208         DWC_LIST_REMOVE(link);                                  \
209         DWC_LIST_INSERT_TAIL(list, link);                       \
210 } while (0)
211
212 #define DWC_LIST_FOREACH(var, list)                             \
213         for ((var) = DWC_LIST_FIRST(list);                      \
214             (var) != DWC_LIST_END(list);                        \
215             (var) = DWC_LIST_NEXT(var))
216
217 #define DWC_LIST_FOREACH_SAFE(var, var2, list)                  \
218         for ((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
219             (var) != DWC_LIST_END(list);                        \
220             (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
221
222 #define DWC_LIST_FOREACH_REVERSE(var, list)                     \
223         for ((var) = DWC_LIST_LAST(list);                       \
224             (var) != DWC_LIST_END(list);                        \
225             (var) = DWC_LIST_PREV(var))
226
227 /*
228  * Singly-linked List definitions.
229  */
230 #define DWC_SLIST_HEAD(name, type)                                      \
231 struct name {                                                           \
232         struct type *slh_first; /* first element */                     \
233 }
234
235 #define DWC_SLIST_HEAD_INITIALIZER(head)                                \
236         { NULL }
237
238 #define DWC_SLIST_ENTRY(type)                                           \
239 struct {                                                                \
240         struct type *sle_next;  /* next element */                      \
241 }
242
243 /*
244  * Singly-linked List access methods.
245  */
246 #define DWC_SLIST_FIRST(head)   ((head)->slh_first)
247 #define DWC_SLIST_END(head)             NULL
248 #define DWC_SLIST_EMPTY(head)   (SLIST_FIRST(head) == SLIST_END(head))
249 #define DWC_SLIST_NEXT(elm, field)      ((elm)->field.sle_next)
250
251 #define DWC_SLIST_FOREACH(var, head, field)                             \
252         for ((var) = SLIST_FIRST(head);                                 \
253             (var) != SLIST_END(head);                                   \
254             (var) = SLIST_NEXT(var, field))
255
256 #define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field)               \
257         for ((varp) = &SLIST_FIRST((head));                             \
258             ((var) = *(varp)) != SLIST_END(head);                       \
259             (varp) = &SLIST_NEXT((var), field))
260
261 /*
262  * Singly-linked List functions.
263  */
264 #define DWC_SLIST_INIT(head) {                                          \
265         SLIST_FIRST(head) = SLIST_END(head);                            \
266 }
267
268 #define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do {               \
269         (elm)->field.sle_next = (slistelm)->field.sle_next;             \
270         (slistelm)->field.sle_next = (elm);                             \
271 } while (0)
272
273 #define DWC_SLIST_INSERT_HEAD(head, elm, field) do {                    \
274         (elm)->field.sle_next = (head)->slh_first;                      \
275         (head)->slh_first = (elm);                                      \
276 } while (0)
277
278 #define DWC_SLIST_REMOVE_NEXT(head, elm, field) do {                    \
279         (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next;  \
280 } while (0)
281
282 #define DWC_SLIST_REMOVE_HEAD(head, field) do {                         \
283         (head)->slh_first = (head)->slh_first->field.sle_next;          \
284 } while (0)
285
286 #define DWC_SLIST_REMOVE(head, elm, type, field) do {                   \
287         if ((head)->slh_first == (elm)) {                               \
288                 SLIST_REMOVE_HEAD((head), field);                       \
289         }                                                               \
290         else {                                                          \
291                 struct type *curelm = (head)->slh_first;                \
292                 while (curelm->field.sle_next != (elm))                 \
293                         curelm = curelm->field.sle_next;                \
294                 curelm->field.sle_next =                                \
295                     curelm->field.sle_next->field.sle_next;             \
296         }                                                               \
297 } while (0)
298
299 /*
300  * Simple queue definitions.
301  */
302 #define DWC_SIMPLEQ_HEAD(name, type)                                    \
303 struct name {                                                           \
304         struct type *sqh_first; /* first element */                     \
305         struct type **sqh_last; /* addr of last next element */         \
306 }
307
308 #define DWC_SIMPLEQ_HEAD_INITIALIZER(head)                              \
309         { NULL, &(head).sqh_first }
310
311 #define DWC_SIMPLEQ_ENTRY(type)                                         \
312 struct {                                                                \
313         struct type *sqe_next;  /* next element */                      \
314 }
315
316 /*
317  * Simple queue access methods.
318  */
319 #define DWC_SIMPLEQ_FIRST(head)     ((head)->sqh_first)
320 #define DWC_SIMPLEQ_END(head)       NULL
321 #define DWC_SIMPLEQ_EMPTY(head)     (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
322 #define DWC_SIMPLEQ_NEXT(elm, field)    ((elm)->field.sqe_next)
323
324 #define DWC_SIMPLEQ_FOREACH(var, head, field)                           \
325         for ((var) = SIMPLEQ_FIRST(head);                               \
326             (var) != SIMPLEQ_END(head);                                 \
327             (var) = SIMPLEQ_NEXT(var, field))
328
329 /*
330  * Simple queue functions.
331  */
332 #define DWC_SIMPLEQ_INIT(head) do {                                     \
333         (head)->sqh_first = NULL;                                       \
334         (head)->sqh_last = &(head)->sqh_first;                          \
335 } while (0)
336
337 #define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do {                  \
338         (elm)->field.sqe_next = (head)->sqh_first;                      \
339         if ((elm)->field.sqe_next == NULL)                              \
340                 (head)->sqh_last = &(elm)->field.sqe_next;              \
341         (head)->sqh_first = (elm);                                      \
342 } while (0)
343
344 #define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do {                  \
345         (elm)->field.sqe_next = NULL;                                   \
346         *(head)->sqh_last = (elm);                                      \
347         (head)->sqh_last = &(elm)->field.sqe_next;                      \
348 } while (0)
349
350 #define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {        \
351         (elm)->field.sqe_next = (listelm)->field.sqe_next;              \
352         if ((elm)->field.sqe_next == NULL)                              \
353                 (head)->sqh_last = &(elm)->field.sqe_next;              \
354         (listelm)->field.sqe_next = (elm);                              \
355 } while (0)
356
357 #define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do {                       \
358         (head)->sqh_first = (head)->sqh_first->field.sqe_next;          \
359         if ((head)->sqh_first == NULL)                                  \
360                 (head)->sqh_last = &(head)->sqh_first;                  \
361 } while (0)
362
363 /*
364  * Tail queue definitions.
365  */
366 #define DWC_TAILQ_HEAD(name, type)                                      \
367 struct name {                                                           \
368         struct type *tqh_first; /* first element */                     \
369         struct type **tqh_last; /* addr of last next element */         \
370 }
371
372 #define DWC_TAILQ_HEAD_INITIALIZER(head)                                \
373         { NULL, &(head).tqh_first }
374
375 #define DWC_TAILQ_ENTRY(type)                                           \
376 struct {                                                                \
377         struct type *tqe_next;  /* next element */                      \
378         struct type **tqe_prev; /* address of previous next element */  \
379 }
380
381 /*
382  * tail queue access methods
383  */
384 #define DWC_TAILQ_FIRST(head)           ((head)->tqh_first)
385 #define DWC_TAILQ_END(head)             NULL
386 #define DWC_TAILQ_NEXT(elm, field)      ((elm)->field.tqe_next)
387 #define DWC_TAILQ_LAST(head, headname)                                  \
388         (*(((struct headname *)((head)->tqh_last))->tqh_last))
389 /* XXX */
390 #define DWC_TAILQ_PREV(elm, headname, field)                            \
391         (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
392 #define DWC_TAILQ_EMPTY(head)                                           \
393         (TAILQ_FIRST(head) == TAILQ_END(head))
394
395 #define DWC_TAILQ_FOREACH(var, head, field)                             \
396         for ((var) = TAILQ_FIRST(head);                                 \
397             (var) != TAILQ_END(head);                                   \
398             (var) = TAILQ_NEXT(var, field))
399
400 #define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field)           \
401         for ((var) = TAILQ_LAST(head, headname);                                \
402             (var) != TAILQ_END(head);                                   \
403             (var) = TAILQ_PREV(var, headname, field))
404
405 /*
406  * Tail queue functions.
407  */
408 #define DWC_TAILQ_INIT(head) do {                                       \
409         (head)->tqh_first = NULL;                                       \
410         (head)->tqh_last = &(head)->tqh_first;                          \
411 } while (0)
412
413 #define DWC_TAILQ_INSERT_HEAD(head, elm, field) do {                    \
414         (elm)->field.tqe_next = (head)->tqh_first;                      \
415         if ((elm)->field.tqe_next != NULL)      \
416                 (head)->tqh_first->field.tqe_prev =                     \
417                     &(elm)->field.tqe_next;                             \
418         else                                                            \
419                 (head)->tqh_last = &(elm)->field.tqe_next;              \
420         (head)->tqh_first = (elm);                                      \
421         (elm)->field.tqe_prev = &(head)->tqh_first;                     \
422 } while (0)
423
424 #define DWC_TAILQ_INSERT_TAIL(head, elm, field) do {                    \
425         (elm)->field.tqe_next = NULL;                                   \
426         (elm)->field.tqe_prev = (head)->tqh_last;                       \
427         *(head)->tqh_last = (elm);                                      \
428         (head)->tqh_last = &(elm)->field.tqe_next;                      \
429 } while (0)
430
431 #define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do {          \
432         (elm)->field.tqe_next = (listelm)->field.tqe_next;              \
433         if ((elm)->field.tqe_next != NULL)                              \
434                 (elm)->field.tqe_next->field.tqe_prev =                 \
435                     &(elm)->field.tqe_next;                             \
436         else                                                            \
437                 (head)->tqh_last = &(elm)->field.tqe_next;              \
438         (listelm)->field.tqe_next = (elm);                              \
439         (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
440 } while (0)
441
442 #define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do {               \
443         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
444         (elm)->field.tqe_next = (listelm);                              \
445         *(listelm)->field.tqe_prev = (elm);                             \
446         (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
447 } while (0)
448
449 #define DWC_TAILQ_REMOVE(head, elm, field) do {                         \
450         if (((elm)->field.tqe_next) != NULL)                            \
451                 (elm)->field.tqe_next->field.tqe_prev =                 \
452                     (elm)->field.tqe_prev;                              \
453         else                                                            \
454                 (head)->tqh_last = (elm)->field.tqe_prev;               \
455         *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
456 } while (0)
457
458 #define DWC_TAILQ_REPLACE(head, elm, elm2, field) do {                  \
459         (elm2)->field.tqe_next = (elm)->field.tqe_next;                 \
460         if ((elm2)->field.tqe_next != NULL)     \
461                 (elm2)->field.tqe_next->field.tqe_prev =                \
462                     &(elm2)->field.tqe_next;                            \
463         else                                                            \
464                 (head)->tqh_last = &(elm2)->field.tqe_next;             \
465         (elm2)->field.tqe_prev = (elm)->field.tqe_prev;                 \
466         *(elm2)->field.tqe_prev = (elm2);                               \
467 } while (0)
468
469 /*
470  * Circular queue definitions.
471  */
472 #define DWC_CIRCLEQ_HEAD(name, type)                                    \
473 struct name {                                                           \
474         struct type *cqh_first;         /* first element */             \
475         struct type *cqh_last;          /* last element */              \
476 }
477
478 #define DWC_CIRCLEQ_HEAD_INITIALIZER(head)                              \
479         { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
480
481 #define DWC_CIRCLEQ_ENTRY(type)                                         \
482 struct {                                                                \
483         struct type *cqe_next;          /* next element */              \
484         struct type *cqe_prev;          /* previous element */          \
485 }
486
487 /*
488  * Circular queue access methods
489  */
490 #define DWC_CIRCLEQ_FIRST(head)         ((head)->cqh_first)
491 #define DWC_CIRCLEQ_LAST(head)          ((head)->cqh_last)
492 #define DWC_CIRCLEQ_END(head)           ((void *)(head))
493 #define DWC_CIRCLEQ_NEXT(elm, field)    ((elm)->field.cqe_next)
494 #define DWC_CIRCLEQ_PREV(elm, field)    ((elm)->field.cqe_prev)
495 #define DWC_CIRCLEQ_EMPTY(head)                                         \
496         (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
497
498 #define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
499
500 #define DWC_CIRCLEQ_FOREACH(var, head, field)                           \
501         for ((var) = DWC_CIRCLEQ_FIRST(head);                           \
502             (var) != DWC_CIRCLEQ_END(head);                             \
503             (var) = DWC_CIRCLEQ_NEXT(var, field))
504
505 #define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field)                        \
506         for ((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
507             (var) != DWC_CIRCLEQ_END(head);                                     \
508             (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
509
510 #define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field)                   \
511         for ((var) = DWC_CIRCLEQ_LAST(head);                            \
512             (var) != DWC_CIRCLEQ_END(head);                             \
513             (var) = DWC_CIRCLEQ_PREV(var, field))
514
515 /*
516  * Circular queue functions.
517  */
518 #define DWC_CIRCLEQ_INIT(head) do {                                     \
519         (head)->cqh_first = DWC_CIRCLEQ_END(head);                      \
520         (head)->cqh_last = DWC_CIRCLEQ_END(head);                       \
521 } while (0)
522
523 #define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do {                         \
524         (elm)->field.cqe_next = NULL;                                   \
525         (elm)->field.cqe_prev = NULL;                                   \
526 } while (0)
527
528 #define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {        \
529         (elm)->field.cqe_next = (listelm)->field.cqe_next;              \
530         (elm)->field.cqe_prev = (listelm);                              \
531         if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head))         \
532                 (head)->cqh_last = (elm);                               \
533         else                                                            \
534                 (listelm)->field.cqe_next->field.cqe_prev = (elm);      \
535         (listelm)->field.cqe_next = (elm);                              \
536 } while (0)
537
538 #define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {       \
539         (elm)->field.cqe_next = (listelm);                              \
540         (elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
541         if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head))         \
542                 (head)->cqh_first = (elm);                              \
543         else                                                            \
544                 (listelm)->field.cqe_prev->field.cqe_next = (elm);      \
545         (listelm)->field.cqe_prev = (elm);                              \
546 } while (0)
547
548 #define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do {                  \
549         (elm)->field.cqe_next = (head)->cqh_first;                      \
550         (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head);                  \
551         if ((head)->cqh_last == DWC_CIRCLEQ_END(head))                  \
552                 (head)->cqh_last = (elm);                               \
553         else                                                            \
554                 (head)->cqh_first->field.cqe_prev = (elm);              \
555         (head)->cqh_first = (elm);                                      \
556 } while (0)
557
558 #define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do {                  \
559         (elm)->field.cqe_next = DWC_CIRCLEQ_END(head);                  \
560         (elm)->field.cqe_prev = (head)->cqh_last;                       \
561         if ((head)->cqh_first == DWC_CIRCLEQ_END(head))                 \
562                 (head)->cqh_first = (elm);                              \
563         else                                                            \
564                 (head)->cqh_last->field.cqe_next = (elm);               \
565         (head)->cqh_last = (elm);                                       \
566 } while (0)
567
568 #define DWC_CIRCLEQ_REMOVE(head, elm, field) do {                       \
569         if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head))             \
570                 (head)->cqh_last = (elm)->field.cqe_prev;               \
571         else                                                            \
572                 (elm)->field.cqe_next->field.cqe_prev =                 \
573                     (elm)->field.cqe_prev;                              \
574         if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head))             \
575                 (head)->cqh_first = (elm)->field.cqe_next;              \
576         else                                                            \
577                 (elm)->field.cqe_prev->field.cqe_next =                 \
578                     (elm)->field.cqe_next;                              \
579 } while (0)
580
581 #define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do {                  \
582         DWC_CIRCLEQ_REMOVE(head, elm, field);                           \
583         DWC_CIRCLEQ_INIT_ENTRY(elm, field);                             \
584 } while (0)
585
586 #define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do {                \
587         (elm2)->field.cqe_next = (elm)->field.cqe_next;                 \
588         if ((elm2)->field.cqe_next ==                                   \
589             DWC_CIRCLEQ_END(head))                                      \
590                 (head).cqh_last = (elm2);                               \
591         else                                                            \
592                 (elm2)->field.cqe_next->field.cqe_prev = (elm2);        \
593         (elm2)->field.cqe_prev = (elm)->field.cqe_prev;                 \
594         if ((elm2)->field.cqe_prev ==                                   \
595             DWC_CIRCLEQ_END(head))                                      \
596                 (head).cqh_first = (elm2);                              \
597         else                                                            \
598                 (elm2)->field.cqe_prev->field.cqe_next = (elm2);        \
599 } while (0)
600
601 #ifdef __cplusplus
602 }
603 #endif
604
605 #endif /* _DWC_LIST_H_ */