2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (C) 2011 Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 * @(#)queue.h 8.5 (Berkeley) 8/20/94
35 #ifndef _COGL_QUEUE_H_
36 #define _COGL_QUEUE_H_
39 * This file defines four types of data structures: singly-linked lists,
40 * singly-linked tail queues, lists and tail queues.
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.
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.
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.
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.
77 * For details on the use of these macros, see the queue(3) manual page.
80 * SLIST LIST STAILQ TAILQ
82 * _HEAD_INITIALIZER + + + +
91 * _FOREACH_SAFE + + + +
92 * _FOREACH_REVERSE - - - +
93 * _FOREACH_REVERSE_SAFE - - - +
94 * _INSERT_HEAD + + + +
95 * _INSERT_BEFORE - + - +
96 * _INSERT_AFTER + + + +
97 * _INSERT_TAIL - - + +
99 * _REMOVE_AFTER + - + -
100 * _REMOVE_HEAD + - + -
105 #ifdef COGL_QUEUE_MACRO_DEBUG
106 /* Store the last 2 places the queue element or head was altered */
107 struct cogl_qm_trace {
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)
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__; \
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__; \
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 */
141 * Singly-linked List declarations.
143 #define COGL_SLIST_HEAD(name, type) \
144 typedef struct _ ## name { \
145 type *slh_first; /* first element */ \
148 #define COGL_SLIST_HEAD_INITIALIZER(head) \
151 #define COGL_SLIST_ENTRY(type) \
153 type *sle_next; /* next element */ \
157 * Singly-linked List functions.
159 #define COGL_SLIST_EMPTY(head) ((head)->slh_first == NULL)
161 #define COGL_SLIST_FIRST(head) ((head)->slh_first)
163 #define COGL_SLIST_FOREACH(var, head, field) \
164 for ((var) = COGL_SLIST_FIRST((head)); \
166 (var) = COGL_SLIST_NEXT((var), field))
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); \
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))
178 #define COGL_SLIST_INIT(head) do { \
179 COGL_SLIST_FIRST((head)) = NULL; \
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); \
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); \
192 #define COGL_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
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); \
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); \
205 COGL_TRASHIT(*oldnext); \
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); \
213 #define COGL_SLIST_REMOVE_HEAD(head, field) do { \
214 COGL_SLIST_FIRST((head)) = \
215 COGL_SLIST_NEXT(COGL_SLIST_FIRST((head)), field); \
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; \
225 * Singly-linked Tail queue declarations.
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 */ \
233 #define COGL_STAILQ_HEAD_INITIALIZER(head) \
234 { NULL, &(head).stqh_first }
236 #define COGL_STAILQ_ENTRY(type) \
238 type *stqe_next; /* next element */ \
242 * Singly-linked Tail queue functions.
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)); \
252 #define COGL_STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
254 #define COGL_STAILQ_FIRST(head) ((head)->stqh_first)
256 #define COGL_STAILQ_FOREACH(var, head, field) \
257 for((var) = COGL_STAILQ_FIRST((head)); \
259 (var) = COGL_STAILQ_NEXT((var), field))
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); \
267 #define COGL_STAILQ_INIT(head) do { \
268 COGL_STAILQ_FIRST((head)) = NULL; \
269 (head)->stqh_last = &COGL_STAILQ_FIRST((head)); \
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); \
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); \
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); \
292 #define COGL_STAILQ_LAST(head, type, field) \
293 (COGL_STAILQ_EMPTY((head)) ? \
296 ((char *)((head)->stqh_last) - \
297 __offsetof(struct type, field))))
299 #define COGL_STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
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); \
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); \
312 COGL_TRASHIT(*oldnext); \
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), \
319 (head)->stqh_last = &COGL_STAILQ_NEXT((elm), field); \
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)); \
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); \
345 #define COGL_LIST_HEAD(name, type) \
346 typedef struct _ ## name { \
347 type *lh_first; /* first element */ \
350 #define COGL_LIST_HEAD_INITIALIZER(head) \
353 #define COGL_LIST_ENTRY(type) \
355 type *le_next; /* next element */ \
356 type **le_prev; /* address of previous next element */ \
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)); \
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)); \
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)); \
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) */
388 #define COGL_LIST_EMPTY(head) ((head)->lh_first == NULL)
390 #define COGL_LIST_FIRST(head) ((head)->lh_first)
392 #define COGL_LIST_FOREACH(var, head, field) \
393 for ((var) = COGL_LIST_FIRST((head)); \
395 (var) = COGL_LIST_NEXT((var), field))
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); \
402 #define COGL_LIST_INIT(head) do { \
403 COGL_LIST_FIRST((head)) = NULL; \
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); \
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); \
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)); \
434 #define COGL_LIST_NEXT(elm, field) ((elm)->field.le_next)
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); \
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)); \
460 * Tail queue declarations.
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 */ \
469 #define COGL_TAILQ_HEAD_INITIALIZER(head) \
470 { NULL, &(head).tqh_first }
472 #define COGL_TAILQ_ENTRY(type) \
474 type *tqe_next; /* next element */ \
475 type **tqe_prev; /* address of previous next element */ \
480 * Tail queue functions.
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)); \
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)); \
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)); \
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)); \
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) */
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); \
524 #define COGL_TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
526 #define COGL_TAILQ_FIRST(head) ((head)->tqh_first)
528 #define COGL_TAILQ_FOREACH(var, head, field) \
529 for ((var) = COGL_TAILQ_FIRST((head)); \
531 (var) = COGL_TAILQ_NEXT((var), field))
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); \
538 #define COGL_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
539 for ((var) = COGL_TAILQ_LAST((head), headname); \
541 (var) = COGL_TAILQ_PREV((var), headname, field))
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); \
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); \
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); \
561 (head)->tqh_last = &COGL_TAILQ_NEXT((elm), field); \
562 COGL_QMD_TRACE_HEAD(head); \
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); \
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); \
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); \
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); \
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); \
604 #define COGL_TAILQ_LAST(head, headname) \
605 (*(((headname *)((head)->tqh_last))->tqh_last))
607 #define COGL_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
609 #define COGL_TAILQ_PREV(elm, headname, field) \
610 (*(((headname *)((elm)->field.tqe_prev))->tqh_last))
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; \
621 (head)->tqh_last = (elm)->field.tqe_prev; \
622 COGL_QMD_TRACE_HEAD(head); \
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); \
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; \
640 (head1)->tqh_last = &(head1)->tqh_first; \
641 if ((swap_first = (head2)->tqh_first) != NULL) \
642 swap_first->field.tqe_prev = &(head2)->tqh_first; \
644 (head2)->tqh_last = &(head2)->tqh_first; \
647 #endif /* !_COGL_QUEUE_H_ */