"Initial commit to Gerrit"
[profile/ivi/cogl.git] / cogl / cogl-queue.h
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Copyright (C) 2011 Intel Corporation.
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  * 4. 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  * $FreeBSD$
33  */
34
35 #ifndef _COGL_QUEUE_H_
36 #define _COGL_QUEUE_H_
37
38 /*
39  * This file defines four types of data structures: singly-linked lists,
40  * singly-linked tail queues, lists and tail queues.
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 singly-linked tail queue is headed by a pair of pointers, one to the
53  * head of the list and the other to the tail of the list. The elements are
54  * singly linked for minimum space and pointer manipulation overhead at the
55  * expense of O(n) removal for arbitrary elements. New elements can be added
56  * to the list after an existing element, at the head of the list, or at the
57  * end of the list. Elements being removed from the head of the tail queue
58  * should use the explicit macro for this purpose for optimum efficiency.
59  * A singly-linked tail queue may only be traversed in the forward direction.
60  * Singly-linked tail queues are ideal for applications with large datasets
61  * and few or no removals or for implementing a FIFO 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 tail queue is headed by a pair of pointers, one to the head of the
71  * list and the other to the tail of the list. The elements are doubly
72  * linked so that an arbitrary element can be removed without a need to
73  * traverse the list. New elements can be added to the list before or
74  * after an existing element, at the head of the list, or at the end of
75  * the list. A tail queue may be traversed in either direction.
76  *
77  * For details on the use of these macros, see the queue(3) manual page.
78  *
79  *
80  *                              SLIST   LIST    STAILQ  TAILQ
81  * _HEAD                        +       +       +       +
82  * _HEAD_INITIALIZER            +       +       +       +
83  * _ENTRY                       +       +       +       +
84  * _INIT                        +       +       +       +
85  * _EMPTY                       +       +       +       +
86  * _FIRST                       +       +       +       +
87  * _NEXT                        +       +       +       +
88  * _PREV                        -       -       -       +
89  * _LAST                        -       -       +       +
90  * _FOREACH                     +       +       +       +
91  * _FOREACH_SAFE                +       +       +       +
92  * _FOREACH_REVERSE             -       -       -       +
93  * _FOREACH_REVERSE_SAFE        -       -       -       +
94  * _INSERT_HEAD                 +       +       +       +
95  * _INSERT_BEFORE               -       +       -       +
96  * _INSERT_AFTER                +       +       +       +
97  * _INSERT_TAIL                 -       -       +       +
98  * _CONCAT                      -       -       +       +
99  * _REMOVE_AFTER                +       -       +       -
100  * _REMOVE_HEAD                 +       -       +       -
101  * _REMOVE                      +       +       +       +
102  * _SWAP                        +       +       +       +
103  *
104  */
105 #ifdef COGL_QUEUE_MACRO_DEBUG
106 /* Store the last 2 places the queue element or head was altered */
107 struct cogl_qm_trace {
108         char * lastfile;
109         int lastline;
110         char * prevfile;
111         int prevline;
112 };
113
114 #define COGL_TRACEBUF struct cogl_qm_trace trace;
115 #define COGL_TRASHIT(x) do {(x) = (void *)-1;} while (0)
116 #define COGL_QMD_SAVELINK(name, link) void **name = (void *)&(link)
117
118 #define COGL_QMD_TRACE_HEAD(head) do {                                  \
119         (head)->trace.prevline = (head)->trace.lastline;                \
120         (head)->trace.prevfile = (head)->trace.lastfile;                \
121         (head)->trace.lastline = __LINE__;                              \
122         (head)->trace.lastfile = __FILE__;                              \
123 } while (0)
124
125 #define COGL_QMD_TRACE_ELEM(elem) do {                                  \
126         (elem)->trace.prevline = (elem)->trace.lastline;                \
127         (elem)->trace.prevfile = (elem)->trace.lastfile;                \
128         (elem)->trace.lastline = __LINE__;                              \
129         (elem)->trace.lastfile = __FILE__;                              \
130 } while (0)
131
132 #else
133 #define COGL_QMD_TRACE_ELEM(elem)
134 #define COGL_QMD_TRACE_HEAD(head)
135 #define COGL_QMD_SAVELINK(name, link)
136 #define COGL_TRACEBUF
137 #define COGL_TRASHIT(x)
138 #endif  /* COGL_QUEUE_MACRO_DEBUG */
139
140 /*
141  * Singly-linked List declarations.
142  */
143 #define COGL_SLIST_HEAD(name, type)                                     \
144 typedef struct _ ## name {                                              \
145   type *slh_first; /* first element */                                  \
146 } name
147
148 #define COGL_SLIST_HEAD_INITIALIZER(head)                               \
149         { NULL }
150
151 #define COGL_SLIST_ENTRY(type)                                          \
152 struct {                                                                \
153   type *sle_next;  /* next element */                                   \
154 }
155
156 /*
157  * Singly-linked List functions.
158  */
159 #define COGL_SLIST_EMPTY(head)  ((head)->slh_first == NULL)
160
161 #define COGL_SLIST_FIRST(head)  ((head)->slh_first)
162
163 #define COGL_SLIST_FOREACH(var, head, field)                            \
164         for ((var) = COGL_SLIST_FIRST((head));                          \
165             (var);                                                      \
166             (var) = COGL_SLIST_NEXT((var), field))
167
168 #define COGL_SLIST_FOREACH_SAFE(var, head, field, tvar)                 \
169         for ((var) = COGL_SLIST_FIRST((head));                          \
170             (var) && ((tvar) = COGL_SLIST_NEXT((var), field), 1);       \
171             (var) = (tvar))
172
173 #define COGL_SLIST_FOREACH_PREVPTR(var, varp, head, field)              \
174         for ((varp) = &COGL_SLIST_FIRST((head));                        \
175             ((var) = *(varp)) != NULL;                                  \
176             (varp) = &COGL_SLIST_NEXT((var), field))
177
178 #define COGL_SLIST_INIT(head) do {                                      \
179         COGL_SLIST_FIRST((head)) = NULL;                                \
180 } while (0)
181
182 #define COGL_SLIST_INSERT_AFTER(slistelm, elm, field) do {              \
183         COGL_SLIST_NEXT((elm), field) = COGL_SLIST_NEXT((slistelm), field);  \
184         COGL_SLIST_NEXT((slistelm), field) = (elm);                     \
185 } while (0)
186
187 #define COGL_SLIST_INSERT_HEAD(head, elm, field) do {                   \
188         COGL_SLIST_NEXT((elm), field) = COGL_SLIST_FIRST((head));       \
189         COGL_SLIST_FIRST((head)) = (elm);                               \
190 } while (0)
191
192 #define COGL_SLIST_NEXT(elm, field)  ((elm)->field.sle_next)
193
194 #define COGL_SLIST_REMOVE(head, elm, type, field) do {                  \
195         COGL_QMD_SAVELINK(oldnext, (elm)->field.sle_next);              \
196         if (COGL_SLIST_FIRST((head)) == (elm)) {                        \
197                 COGL_SLIST_REMOVE_HEAD((head), field);                  \
198         }                                                               \
199         else {                                                          \
200                 type *curelm = COGL_SLIST_FIRST((head));                \
201                 while (COGL_SLIST_NEXT(curelm, field) != (elm))         \
202                         curelm = COGL_SLIST_NEXT(curelm, field);        \
203                 COGL_SLIST_REMOVE_AFTER(curelm, field);                 \
204         }                                                               \
205         COGL_TRASHIT(*oldnext);                                         \
206 } while (0)
207
208 #define COGL_SLIST_REMOVE_AFTER(elm, field) do {                        \
209         COGL_SLIST_NEXT(elm, field) =                                   \
210             COGL_SLIST_NEXT(COGL_SLIST_NEXT(elm, field), field);        \
211 } while (0)
212
213 #define COGL_SLIST_REMOVE_HEAD(head, field) do {                        \
214     COGL_SLIST_FIRST((head)) =                                          \
215       COGL_SLIST_NEXT(COGL_SLIST_FIRST((head)), field);                 \
216 } while (0)
217
218 #define COGL_SLIST_SWAP(head1, head2, type) do {                        \
219         type *swap_first = COGL_SLIST_FIRST(head1);                     \
220         COGL_SLIST_FIRST(head1) = COGL_SLIST_FIRST(head2);              \
221         COGL_SLIST_FIRST(head2) = swap_first;                           \
222 } while (0)
223
224 /*
225  * Singly-linked Tail queue declarations.
226  */
227 #define COGL_STAILQ_HEAD(name, type)                                    \
228 typedef struct _ ## name {                                              \
229         type *stqh_first;/* first element */                            \
230         type **stqh_last;/* addr of last next element */                \
231 } name
232
233 #define COGL_STAILQ_HEAD_INITIALIZER(head)                              \
234         { NULL, &(head).stqh_first }
235
236 #define COGL_STAILQ_ENTRY(type)                                         \
237 struct {                                                                \
238         type *stqe_next; /* next element */                             \
239 }
240
241 /*
242  * Singly-linked Tail queue functions.
243  */
244 #define COGL_STAILQ_CONCAT(head1, head2) do {                           \
245         if (!COGL_STAILQ_EMPTY((head2))) {                              \
246                 *(head1)->stqh_last = (head2)->stqh_first;              \
247                 (head1)->stqh_last = (head2)->stqh_last;                \
248                 COGL_STAILQ_INIT((head2));                              \
249         }                                                               \
250 } while (0)
251
252 #define COGL_STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
253
254 #define COGL_STAILQ_FIRST(head) ((head)->stqh_first)
255
256 #define COGL_STAILQ_FOREACH(var, head, field)                           \
257         for((var) = COGL_STAILQ_FIRST((head));                          \
258            (var);                                                       \
259            (var) = COGL_STAILQ_NEXT((var), field))
260
261
262 #define COGL_STAILQ_FOREACH_SAFE(var, head, field, tvar)                \
263         for ((var) = COGL_STAILQ_FIRST((head));                         \
264             (var) && ((tvar) = COGL_STAILQ_NEXT((var), field), 1);      \
265             (var) = (tvar))
266
267 #define COGL_STAILQ_INIT(head) do {                                     \
268         COGL_STAILQ_FIRST((head)) = NULL;                               \
269         (head)->stqh_last = &COGL_STAILQ_FIRST((head));                 \
270 } while (0)
271
272 #define COGL_STAILQ_INSERT_AFTER(head, tqelm, elm, field) do {          \
273     if ((COGL_STAILQ_NEXT((elm), field) =                               \
274          COGL_STAILQ_NEXT((tqelm), field)) == NULL)                     \
275       (head)->stqh_last = &COGL_STAILQ_NEXT((elm), field);              \
276     COGL_STAILQ_NEXT((tqelm), field) = (elm);                           \
277 } while (0)
278
279 #define COGL_STAILQ_INSERT_HEAD(head, elm, field) do {                  \
280     if ((COGL_STAILQ_NEXT((elm), field) =                               \
281          COGL_STAILQ_FIRST((head))) == NULL)                            \
282       (head)->stqh_last = &COGL_STAILQ_NEXT((elm), field);              \
283     COGL_STAILQ_FIRST((head)) = (elm);                                  \
284   } while (0)
285
286 #define COGL_STAILQ_INSERT_TAIL(head, elm, field) do {                  \
287         COGL_STAILQ_NEXT((elm), field) = NULL;                          \
288         *(head)->stqh_last = (elm);                                     \
289         (head)->stqh_last = &COGL_STAILQ_NEXT((elm), field);            \
290 } while (0)
291
292 #define COGL_STAILQ_LAST(head, type, field)                             \
293         (COGL_STAILQ_EMPTY((head)) ?                                    \
294                 NULL :                                                  \
295                 ((type *)(void *)                                       \
296                  ((char *)((head)->stqh_last) -                         \
297                   __offsetof(struct type, field))))
298
299 #define COGL_STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
300
301 #define COGL_STAILQ_REMOVE(head, elm, type, field) do {                 \
302         COGL_QMD_SAVELINK(oldnext, (elm)->field.stqe_next);             \
303         if (COGL_STAILQ_FIRST((head)) == (elm)) {                       \
304                 COGL_STAILQ_REMOVE_HEAD((head), field);                 \
305         }                                                               \
306         else {                                                          \
307                 type *curelm = COGL_STAILQ_FIRST((head));               \
308                 while (COGL_STAILQ_NEXT(curelm, field) != (elm))        \
309                         curelm = COGL_STAILQ_NEXT(curelm, field);       \
310                 COGL_STAILQ_REMOVE_AFTER(head, curelm, field);          \
311         }                                                               \
312         COGL_TRASHIT(*oldnext);                                         \
313 } while (0)
314
315 #define COGL_STAILQ_REMOVE_AFTER(head, elm, field) do {                 \
316         if ((COGL_STAILQ_NEXT(elm, field) =                             \
317              COGL_STAILQ_NEXT(COGL_STAILQ_NEXT(elm, field),             \
318                               field)) == NULL)                          \
319                 (head)->stqh_last = &COGL_STAILQ_NEXT((elm), field);    \
320 } while (0)
321
322 #define COGL_STAILQ_REMOVE_HEAD(head, field) do {                       \
323         if ((COGL_STAILQ_FIRST((head)) =                                \
324              COGL_STAILQ_NEXT(COGL_STAILQ_FIRST((head)), field)) == NULL) \
325                 (head)->stqh_last = &COGL_STAILQ_FIRST((head));         \
326 } while (0)
327
328 #define COGL_STAILQ_SWAP(head1, head2, type) do {                       \
329         type *swap_first = COGL_STAILQ_FIRST(head1);                    \
330         type **swap_last = (head1)->stqh_last;                          \
331         COGL_STAILQ_FIRST(head1) = COGL_STAILQ_FIRST(head2);            \
332         (head1)->stqh_last = (head2)->stqh_last;                        \
333         COGL_STAILQ_FIRST(head2) = swap_first;                          \
334         (head2)->stqh_last = swap_last;                                 \
335         if (COGL_STAILQ_EMPTY(head1))                                   \
336                 (head1)->stqh_last = &COGL_STAILQ_FIRST(head1);         \
337         if (COGL_STAILQ_EMPTY(head2))                                   \
338                 (head2)->stqh_last = &COGL_STAILQ_FIRST(head2);         \
339 } while (0)
340
341
342 /*
343  * List declarations.
344  */
345 #define COGL_LIST_HEAD(name, type)                                      \
346 typedef struct _ ## name {                                              \
347         type *lh_first;  /* first element */                            \
348 } name
349
350 #define COGL_LIST_HEAD_INITIALIZER(head)                                \
351         { NULL }
352
353 #define COGL_LIST_ENTRY(type)                                           \
354 struct {                                                                \
355         type *le_next;   /* next element */                             \
356         type **le_prev;  /* address of previous next element */         \
357 }
358
359 /*
360  * List functions.
361  */
362
363 #if (defined(_KERNEL) && defined(INVARIANTS))
364 #define COGL_QMD_LIST_CHECK_HEAD(head, field) do {                      \
365         if (COGL_LIST_FIRST((head)) != NULL &&                          \
366             COGL_LIST_FIRST((head))->field.le_prev !=                   \
367              &COGL_LIST_FIRST((head)))                                  \
368                 panic("Bad list head %p first->prev != head", (head));  \
369 } while (0)
370
371 #define COGL_QMD_LIST_CHECK_NEXT(elm, field) do {                       \
372         if (COGL_LIST_NEXT((elm), field) != NULL &&                     \
373             COGL_LIST_NEXT((elm), field)->field.le_prev !=              \
374              &((elm)->field.le_next))                                   \
375                 panic("Bad link elm %p next->prev != elm", (elm));      \
376 } while (0)
377
378 #define COGL_QMD_LIST_CHECK_PREV(elm, field) do {                       \
379         if (*(elm)->field.le_prev != (elm))                             \
380                 panic("Bad link elm %p prev->next != elm", (elm));      \
381 } while (0)
382 #else
383 #define COGL_QMD_LIST_CHECK_HEAD(head, field)
384 #define COGL_QMD_LIST_CHECK_NEXT(elm, field)
385 #define COGL_QMD_LIST_CHECK_PREV(elm, field)
386 #endif /* (_KERNEL && INVARIANTS) */
387
388 #define COGL_LIST_EMPTY(head)   ((head)->lh_first == NULL)
389
390 #define COGL_LIST_FIRST(head)   ((head)->lh_first)
391
392 #define COGL_LIST_FOREACH(var, head, field)                             \
393         for ((var) = COGL_LIST_FIRST((head));                           \
394             (var);                                                      \
395             (var) = COGL_LIST_NEXT((var), field))
396
397 #define COGL_LIST_FOREACH_SAFE(var, head, field, tvar)                  \
398         for ((var) = COGL_LIST_FIRST((head));                           \
399             (var) && ((tvar) = COGL_LIST_NEXT((var), field), 1);        \
400             (var) = (tvar))
401
402 #define COGL_LIST_INIT(head) do {                                       \
403         COGL_LIST_FIRST((head)) = NULL;                                 \
404 } while (0)
405
406 #define COGL_LIST_INSERT_AFTER(listelm, elm, field) do {                \
407         COGL_QMD_LIST_CHECK_NEXT(listelm, field);                       \
408         if ((COGL_LIST_NEXT((elm), field) =                             \
409              COGL_LIST_NEXT((listelm), field)) != NULL)                 \
410                 COGL_LIST_NEXT((listelm), field)->field.le_prev =       \
411                     &COGL_LIST_NEXT((elm), field);                      \
412         COGL_LIST_NEXT((listelm), field) = (elm);                       \
413         (elm)->field.le_prev = &COGL_LIST_NEXT((listelm), field);       \
414 } while (0)
415
416 #define COGL_LIST_INSERT_BEFORE(listelm, elm, field) do {               \
417         COGL_QMD_LIST_CHECK_PREV(listelm, field);                       \
418         (elm)->field.le_prev = (listelm)->field.le_prev;                \
419         COGL_LIST_NEXT((elm), field) = (listelm);                       \
420         *(listelm)->field.le_prev = (elm);                              \
421         (listelm)->field.le_prev = &COGL_LIST_NEXT((elm), field);       \
422 } while (0)
423
424 #define COGL_LIST_INSERT_HEAD(head, elm, field) do {                    \
425         COGL_QMD_LIST_CHECK_HEAD((head), field);                        \
426         if ((COGL_LIST_NEXT((elm), field) =                             \
427              COGL_LIST_FIRST((head))) != NULL)                          \
428           COGL_LIST_FIRST((head))->field.le_prev =                      \
429             &COGL_LIST_NEXT((elm), field);                              \
430         COGL_LIST_FIRST((head)) = (elm);                                \
431         (elm)->field.le_prev = &COGL_LIST_FIRST((head));                \
432 } while (0)
433
434 #define COGL_LIST_NEXT(elm, field)   ((elm)->field.le_next)
435
436 #define COGL_LIST_REMOVE(elm, field) do {                               \
437         COGL_QMD_SAVELINK(oldnext, (elm)->field.le_next);               \
438         COGL_QMD_SAVELINK(oldprev, (elm)->field.le_prev);               \
439         COGL_QMD_LIST_CHECK_NEXT(elm, field);                           \
440         COGL_QMD_LIST_CHECK_PREV(elm, field);                           \
441         if (COGL_LIST_NEXT((elm), field) != NULL)                       \
442                 COGL_LIST_NEXT((elm), field)->field.le_prev =           \
443                     (elm)->field.le_prev;                               \
444         *(elm)->field.le_prev = COGL_LIST_NEXT((elm), field);           \
445         COGL_TRASHIT(*oldnext);                                         \
446         COGL_TRASHIT(*oldprev);                                         \
447 } while (0)
448
449 #define COGL_LIST_SWAP(head1, head2, type, field) do {                  \
450         type *swap_tmp = COGL_LIST_FIRST((head1));                      \
451         COGL_LIST_FIRST((head1)) = COGL_LIST_FIRST((head2));            \
452         COGL_LIST_FIRST((head2)) = swap_tmp;                            \
453         if ((swap_tmp = COGL_LIST_FIRST((head1))) != NULL)              \
454                 swap_tmp->field.le_prev = &COGL_LIST_FIRST((head1));    \
455         if ((swap_tmp = COGL_LIST_FIRST((head2))) != NULL)              \
456                 swap_tmp->field.le_prev = &COGL_LIST_FIRST((head2));    \
457 } while (0)
458
459 /*
460  * Tail queue declarations.
461  */
462 #define COGL_TAILQ_HEAD(name, type)                                     \
463 typedef struct _ ## name {                                              \
464         type *tqh_first; /* first element */                            \
465         type **tqh_last; /* addr of last next element */                \
466         COGL_TRACEBUF                                                   \
467 } name
468
469 #define COGL_TAILQ_HEAD_INITIALIZER(head)                               \
470         { NULL, &(head).tqh_first }
471
472 #define COGL_TAILQ_ENTRY(type)                                          \
473 struct {                                                                \
474         type *tqe_next;  /* next element */                             \
475         type **tqe_prev; /* address of previous next element */         \
476         COGL_TRACEBUF                                                   \
477 }
478
479 /*
480  * Tail queue functions.
481  */
482 #if (defined(_KERNEL) && defined(INVARIANTS))
483 #define COGL_QMD_TAILQ_CHECK_HEAD(head, field) do {                     \
484         if (!COGL_TAILQ_EMPTY(head) &&                                  \
485             COGL_TAILQ_FIRST((head))->field.tqe_prev !=                 \
486              &COGL_TAILQ_FIRST((head)))                                 \
487                 panic("Bad tailq head %p first->prev != head", (head)); \
488 } while (0)
489
490 #define COGL_QMD_TAILQ_CHECK_TAIL(head, field) do {                     \
491         if (*(head)->tqh_last != NULL)                                  \
492                 panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head));  \
493 } while (0)
494
495 #define COGL_QMD_TAILQ_CHECK_NEXT(elm, field) do {                      \
496         if (COGL_TAILQ_NEXT((elm), field) != NULL &&                    \
497             COGL_TAILQ_NEXT((elm), field)->field.tqe_prev !=            \
498              &((elm)->field.tqe_next))                                  \
499                 panic("Bad link elm %p next->prev != elm", (elm));      \
500 } while (0)
501
502 #define COGL_QMD_TAILQ_CHECK_PREV(elm, field) do {                      \
503         if (*(elm)->field.tqe_prev != (elm))                            \
504                 panic("Bad link elm %p prev->next != elm", (elm));      \
505 } while (0)
506 #else
507 #define COGL_QMD_TAILQ_CHECK_HEAD(head, field)
508 #define COGL_QMD_TAILQ_CHECK_TAIL(head, headname)
509 #define COGL_QMD_TAILQ_CHECK_NEXT(elm, field)
510 #define COGL_QMD_TAILQ_CHECK_PREV(elm, field)
511 #endif /* (_KERNEL && INVARIANTS) */
512
513 #define COGL_TAILQ_CONCAT(head1, head2, field) do {                     \
514         if (!COGL_TAILQ_EMPTY(head2)) {                                 \
515                 *(head1)->tqh_last = (head2)->tqh_first;                \
516                 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
517                 (head1)->tqh_last = (head2)->tqh_last;                  \
518                 COGL_TAILQ_INIT((head2));                               \
519                 COGL_QMD_TRACE_HEAD(head1);                             \
520                 COGL_QMD_TRACE_HEAD(head2);                             \
521         }                                                               \
522 } while (0)
523
524 #define COGL_TAILQ_EMPTY(head)  ((head)->tqh_first == NULL)
525
526 #define COGL_TAILQ_FIRST(head)  ((head)->tqh_first)
527
528 #define COGL_TAILQ_FOREACH(var, head, field)                            \
529         for ((var) = COGL_TAILQ_FIRST((head));                          \
530             (var);                                                      \
531             (var) = COGL_TAILQ_NEXT((var), field))
532
533 #define COGL_TAILQ_FOREACH_SAFE(var, head, field, tvar)                 \
534         for ((var) = COGL_TAILQ_FIRST((head));                          \
535             (var) && ((tvar) = COGL_TAILQ_NEXT((var), field), 1);       \
536             (var) = (tvar))
537
538 #define COGL_TAILQ_FOREACH_REVERSE(var, head, headname, field)          \
539         for ((var) = COGL_TAILQ_LAST((head), headname);                 \
540             (var);                                                      \
541             (var) = COGL_TAILQ_PREV((var), headname, field))
542
543 #define COGL_TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
544         for ((var) = COGL_TAILQ_LAST((head), headname);                 \
545             (var) && ((tvar) = COGL_TAILQ_PREV((var), headname, field), 1); \
546             (var) = (tvar))
547
548 #define COGL_TAILQ_INIT(head) do {                                      \
549         COGL_TAILQ_FIRST((head)) = NULL;                                \
550         (head)->tqh_last = &COGL_TAILQ_FIRST((head));                   \
551         COGL_QMD_TRACE_HEAD(head);                                      \
552 } while (0)
553
554 #define COGL_TAILQ_INSERT_AFTER(head, listelm, elm, field) do {         \
555         COGL_QMD_TAILQ_CHECK_NEXT(listelm, field);                      \
556         if ((COGL_TAILQ_NEXT((elm), field) =                            \
557              COGL_TAILQ_NEXT((listelm), field)) != NULL)                \
558                 COGL_TAILQ_NEXT((elm), field)->field.tqe_prev =         \
559                     &COGL_TAILQ_NEXT((elm), field);                     \
560         else {                                                          \
561                 (head)->tqh_last = &COGL_TAILQ_NEXT((elm), field);      \
562                 COGL_QMD_TRACE_HEAD(head);                              \
563         }                                                               \
564         COGL_TAILQ_NEXT((listelm), field) = (elm);                      \
565         (elm)->field.tqe_prev = &COGL_TAILQ_NEXT((listelm), field);     \
566         COGL_QMD_TRACE_ELEM(&(elm)->field);                             \
567         COGL_QMD_TRACE_ELEM(&listelm->field);                           \
568 } while (0)
569
570 #define COGL_TAILQ_INSERT_BEFORE(listelm, elm, field) do {              \
571         COGL_QMD_TAILQ_CHECK_PREV(listelm, field);                      \
572         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
573         COGL_TAILQ_NEXT((elm), field) = (listelm);                      \
574         *(listelm)->field.tqe_prev = (elm);                             \
575         (listelm)->field.tqe_prev = &COGL_TAILQ_NEXT((elm), field);     \
576         COGL_QMD_TRACE_ELEM(&(elm)->field);                             \
577         COGL_QMD_TRACE_ELEM(&listelm->field);                           \
578 } while (0)
579
580 #define COGL_TAILQ_INSERT_HEAD(head, elm, field) do {                   \
581         COGL_QMD_TAILQ_CHECK_HEAD(head, field);                         \
582         if ((COGL_TAILQ_NEXT((elm), field) =                            \
583              COGL_TAILQ_FIRST((head))) != NULL)                         \
584                 COGL_TAILQ_FIRST((head))->field.tqe_prev =              \
585                     &COGL_TAILQ_NEXT((elm), field);                     \
586         else                                                            \
587                 (head)->tqh_last = &COGL_TAILQ_NEXT((elm), field);      \
588         COGL_TAILQ_FIRST((head)) = (elm);                               \
589         (elm)->field.tqe_prev = &COGL_TAILQ_FIRST((head));              \
590         COGL_QMD_TRACE_HEAD(head);                                      \
591         COGL_QMD_TRACE_ELEM(&(elm)->field);                             \
592 } while (0)
593
594 #define COGL_TAILQ_INSERT_TAIL(head, elm, field) do {                   \
595         COGL_QMD_TAILQ_CHECK_TAIL(head, field);                         \
596         COGL_TAILQ_NEXT((elm), field) = NULL;                           \
597         (elm)->field.tqe_prev = (head)->tqh_last;                       \
598         *(head)->tqh_last = (elm);                                      \
599         (head)->tqh_last = &COGL_TAILQ_NEXT((elm), field);              \
600         COGL_QMD_TRACE_HEAD(head);                                      \
601         COGL_QMD_TRACE_ELEM(&(elm)->field);                             \
602 } while (0)
603
604 #define COGL_TAILQ_LAST(head, headname)                                 \
605         (*(((headname *)((head)->tqh_last))->tqh_last))
606
607 #define COGL_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
608
609 #define COGL_TAILQ_PREV(elm, headname, field)                           \
610         (*(((headname *)((elm)->field.tqe_prev))->tqh_last))
611
612 #define COGL_TAILQ_REMOVE(head, elm, field) do {                        \
613         COGL_QMD_SAVELINK(oldnext, (elm)->field.tqe_next);              \
614         COGL_QMD_SAVELINK(oldprev, (elm)->field.tqe_prev);              \
615         COGL_QMD_TAILQ_CHECK_NEXT(elm, field);                          \
616         COGL_QMD_TAILQ_CHECK_PREV(elm, field);                          \
617         if ((COGL_TAILQ_NEXT((elm), field)) != NULL)                    \
618                 COGL_TAILQ_NEXT((elm), field)->field.tqe_prev =         \
619                     (elm)->field.tqe_prev;                              \
620         else {                                                          \
621                 (head)->tqh_last = (elm)->field.tqe_prev;               \
622                 COGL_QMD_TRACE_HEAD(head);                              \
623         }                                                               \
624         *(elm)->field.tqe_prev = COGL_TAILQ_NEXT((elm), field);         \
625         COGL_TRASHIT(*oldnext);                                         \
626         COGL_TRASHIT(*oldprev);                                         \
627         COGL_QMD_TRACE_ELEM(&(elm)->field);                             \
628 } while (0)
629
630 #define COGL_TAILQ_SWAP(head1, head2, type, field) do {                 \
631         type *swap_first = (head1)->tqh_first;                          \
632         type **swap_last = (head1)->tqh_last;                           \
633         (head1)->tqh_first = (head2)->tqh_first;                        \
634         (head1)->tqh_last = (head2)->tqh_last;                          \
635         (head2)->tqh_first = swap_first;                                \
636         (head2)->tqh_last = swap_last;                                  \
637         if ((swap_first = (head1)->tqh_first) != NULL)                  \
638                 swap_first->field.tqe_prev = &(head1)->tqh_first;       \
639         else                                                            \
640                 (head1)->tqh_last = &(head1)->tqh_first;                \
641         if ((swap_first = (head2)->tqh_first) != NULL)                  \
642                 swap_first->field.tqe_prev = &(head2)->tqh_first;       \
643         else                                                            \
644                 (head2)->tqh_last = &(head2)->tqh_first;                \
645 } while (0)
646
647 #endif /* !_COGL_QUEUE_H_ */