2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon rights
54 * to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
63 * Type definitions for generic queues.
67 #ifndef _KERN_QUEUE_H_
68 #define _KERN_QUEUE_H_
70 #include <mach/mach_types.h>
71 #include <kern/macro_help.h>
73 #include <sys/cdefs.h>
79 * Queue Management APIs
81 * There are currently two subtly different methods of maintining
82 * a queue of objects. Both APIs are contained in this file, and
83 * unfortunately overlap.
84 * (there is also a third way maintained in bsd/sys/queue.h)
86 * Both methods use a common queue head and linkage pattern:
87 * The head of a queue is declared as:
88 * queue_head_t q_head;
90 * Elements in this queue are chained together using
91 * struct queue_entry objects embedded within a structure:
100 * struct some_data is referred to as the queue "element."
101 * (note that queue_chain_t is typedef'd to struct queue_entry)
103 * IMPORTANT: The two queue iteration methods described below are not
104 * compatible with one another. You must choose one and be careful
105 * to use only the supported APIs for that method.
107 * Method 1: chaining of queue_chain_t (linkage chains)
108 * This method uses the next and prev pointers of the struct queue_entry
109 * linkage object embedded in a queue element to point to the next or
110 * previous queue_entry structure in the chain. The head of the queue
111 * (the queue_head_t object) will point to the first and last
112 * struct queue_entry object, and both the next and prev pointer will
113 * point back to the head if the queue is empty.
115 * This method is the most flexible method of chaining objects together
116 * as it allows multiple chains through a given object, by embedding
117 * multiple queue_chain_t objects in the structure, while simultaneously
118 * providing fast removal and insertion into the queue using only
119 * struct queue_entry object pointers.
121 * ++ Valid APIs for this style queue ++
122 * -------------------------------------
145 * [1] qe_foreach_safe
146 * [1] qe_foreach_element
147 * [1] qe_foreach_element_safe
149 * Method 2: chaining of elements (element chains)
150 * This method uses the next and prev pointers of the struct queue_entry
151 * linkage object embedded in a queue element to point to the next or
152 * previous queue element (not another queue_entry). The head of the
153 * queue will point to the first and last queue element (struct some_data
154 * from the above example) NOT the embedded queue_entry structure. The
155 * first queue element will have a prev pointer that points to the
156 * queue_head_t, and the last queue element will have a next pointer
157 * that points to the queue_head_t.
159 * This method requires knowledge of the queue_head_t of the queue on
160 * which an element resides in order to remove the element. Iterating
161 * through the elements of the queue is also more cumbersome because
162 * a check against the head pointer plus a cast then offset operation
163 * must be performed at each step of the iteration.
165 * ++ Valid APIs for this style queue ++
166 * -------------------------------------
176 * [2] queue_enter_first
177 * [2] queue_insert_before
178 * [2] queue_insert_after
181 * [2] queue_remove_first
182 * [2] queue_remove_last
188 * [C] -> API common to both methods
189 * [1] -> API used only in method 1 (linkage chains)
190 * [2] -> API used only in method 2 (element chains)
194 * A generic doubly-linked list (queue).
198 struct queue_entry
*next
; /* next element */
199 struct queue_entry
*prev
; /* previous element */
201 #if __arm__ && (__BIGGEST_ALIGNMENT__ > 4)
202 /* For the newer ARMv7k ABI where 64-bit types are 64-bit aligned, but pointers
204 * Since this type is so often cast to various 64-bit aligned types
205 * aligning it to 64-bits will avoid -wcast-align without needing
206 * to disable it entirely. The impact on memory footprint should be
209 } __attribute__ ((aligned(8)));
214 typedef struct queue_entry
*queue_t
;
215 typedef struct queue_entry queue_head_t
;
216 typedef struct queue_entry queue_chain_t
;
217 typedef struct queue_entry
*queue_entry_t
;
220 * enqueue puts "elt" on the "queue".
221 * dequeue returns the first element in the "queue".
222 * remqueue removes the specified "elt" from its queue.
225 #define enqueue(queue, elt) enqueue_tail(queue, elt)
226 #define dequeue(queue) dequeue_head(queue)
228 #ifdef XNU_KERNEL_PRIVATE
229 #include <kern/debug.h>
231 __QUEUE_ELT_VALIDATE(queue_entry_t elt
)
233 queue_entry_t elt_next
, elt_prev
;
235 if (__improbable(elt
== (queue_entry_t
)NULL
)) {
236 panic("Invalid queue element %p", elt
);
239 elt_next
= elt
->next
;
240 elt_prev
= elt
->prev
;
242 if (__improbable(elt_next
== (queue_entry_t
)NULL
|| elt_prev
== (queue_entry_t
)NULL
)) {
243 panic("Invalid queue element pointers for %p: next %p prev %p", elt
, elt_next
, elt_prev
);
245 if (__improbable(elt_next
->prev
!= elt
|| elt_prev
->next
!= elt
)) {
246 panic("Invalid queue element linkage for %p: next %p next->prev %p prev %p prev->next %p",
247 elt
, elt_next
, elt_next
->prev
, elt_prev
, elt_prev
->next
);
252 __DEQUEUE_ELT_CLEANUP(queue_entry_t elt
)
254 (elt
)->next
= (queue_entry_t
)NULL
;
255 (elt
)->prev
= (queue_entry_t
)NULL
;
258 #define __QUEUE_ELT_VALIDATE(elt) do { } while (0)
259 #define __DEQUEUE_ELT_CLEANUP(elt) do { } while(0)
260 #endif /* !XNU_KERNEL_PRIVATE */
262 static __inline__
void
267 queue_entry_t old_head
;
269 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
270 old_head
= que
->next
;
271 elt
->next
= old_head
;
273 old_head
->prev
= elt
;
277 static __inline__
void
282 queue_entry_t old_tail
;
284 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
285 old_tail
= que
->prev
;
287 elt
->prev
= old_tail
;
288 old_tail
->next
= elt
;
292 static __inline__ queue_entry_t
296 queue_entry_t elt
= (queue_entry_t
)NULL
;
297 queue_entry_t new_head
;
299 if (que
->next
!= que
) {
301 __QUEUE_ELT_VALIDATE(elt
);
302 new_head
= elt
->next
; /* new_head may point to que if elt was the only element */
303 new_head
->prev
= que
;
304 que
->next
= new_head
;
305 __DEQUEUE_ELT_CLEANUP(elt
);
311 static __inline__ queue_entry_t
315 queue_entry_t elt
= (queue_entry_t
)NULL
;
316 queue_entry_t new_tail
;
318 if (que
->prev
!= que
) {
320 __QUEUE_ELT_VALIDATE(elt
);
321 new_tail
= elt
->prev
; /* new_tail may point to queue if elt was the only element */
322 new_tail
->next
= que
;
323 que
->prev
= new_tail
;
324 __DEQUEUE_ELT_CLEANUP(elt
);
330 static __inline__
void
334 queue_entry_t next_elt
, prev_elt
;
336 __QUEUE_ELT_VALIDATE(elt
);
337 next_elt
= elt
->next
;
338 prev_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
339 next_elt
->prev
= prev_elt
;
340 prev_elt
->next
= next_elt
;
341 __DEQUEUE_ELT_CLEANUP(elt
);
344 static __inline__
void
349 queue_entry_t successor
;
351 __QUEUE_ELT_VALIDATE(pred
);
352 successor
= pred
->next
;
353 entry
->next
= successor
;
355 successor
->prev
= entry
;
359 static __inline__
void
367 * Function: re_queue_head
369 * queue_t que : queue onto which elt will be pre-pended
370 * queue_entry_t elt : element to re-queue
372 * Remove elt from its current queue and put it onto the
373 * head of a new queue
375 * This should only be used with Method 1 queue iteration (linkage chains)
377 static __inline__
void
378 re_queue_head(queue_t que
, queue_entry_t elt
)
380 queue_entry_t n_elt
, p_elt
;
382 __QUEUE_ELT_VALIDATE(elt
);
383 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
387 p_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
400 * Function: re_queue_tail
402 * queue_t que : queue onto which elt will be appended
403 * queue_entry_t elt : element to re-queue
405 * Remove elt from its current queue and put it onto the
408 * This should only be used with Method 1 queue iteration (linkage chains)
410 static __inline__
void
411 re_queue_tail(queue_t que
, queue_entry_t elt
)
413 queue_entry_t n_elt
, p_elt
;
415 __QUEUE_ELT_VALIDATE(elt
);
416 __QUEUE_ELT_VALIDATE((queue_entry_t
)que
);
420 p_elt
= elt
->prev
; /* next_elt may equal prev_elt (and the queue head) if elt was the only element */
435 * Convert a queue_entry_t to a queue element pointer.
436 * Get a pointer to the user-defined element containing
437 * a given queue_entry_t
439 * <type> * qe_element(queue_entry_t qe, <type>, field)
440 * qe - queue entry to convert
441 * <type> - what's in the queue (e.g., struct some_data)
442 * <field> - is the chain field in <type>
444 * Do not use pointer types for <type>
446 #define qe_element(qe, type, field) __container_of(qe, type, field)
451 * Iterate over each queue_entry_t structure.
452 * Generates a 'for' loop, setting 'qe' to
453 * each queue_entry_t in the queue.
455 * qe_foreach(queue_entry_t qe, queue_t head)
456 * qe - iteration variable
457 * head - pointer to queue_head_t (head of queue)
459 * This should only be used with Method 1 queue iteration (linkage chains)
461 #define qe_foreach(qe, head) \
462 for (qe = (head)->next; qe != (head); qe = (qe)->next)
465 * Macro: qe_foreach_safe
467 * Safely iterate over each queue_entry_t structure.
469 * Use this iterator macro if you plan to remove the
470 * queue_entry_t, qe, from the queue during the
473 * qe_foreach_safe(queue_entry_t qe, queue_t head)
474 * qe - iteration variable
475 * head - pointer to queue_head_t (head of queue)
477 * This should only be used with Method 1 queue iteration (linkage chains)
479 #define qe_foreach_safe(qe, head) \
480 for (queue_entry_t _ne = ((head)->next)->next, \
481 __ ## qe ## _unused_shadow __unused = (qe = (head)->next); \
483 qe = _ne, _ne = (qe)->next)
486 * Macro: qe_foreach_element
488 * Iterate over each _element_ in a queue
489 * where each queue_entry_t points to another
490 * queue_entry_t, i.e., managed by the [de|en]queue_head/
491 * [de|en]queue_tail / remqueue / etc. function.
493 * qe_foreach_element(<type> *elt, queue_t head, <field>)
494 * elt - iteration variable
495 * <type> - what's in the queue (e.g., struct some_data)
496 * <field> - is the chain field in <type>
498 * This should only be used with Method 1 queue iteration (linkage chains)
500 #define qe_foreach_element(elt, head, field) \
501 for (elt = qe_element((head)->next, typeof(*(elt)), field); \
502 &((elt)->field) != (head); \
503 elt = qe_element((elt)->field.next, typeof(*(elt)), field))
506 * Macro: qe_foreach_element_safe
508 * Safely iterate over each _element_ in a queue
509 * where each queue_entry_t points to another
510 * queue_entry_t, i.e., managed by the [de|en]queue_head/
511 * [de|en]queue_tail / remqueue / etc. function.
513 * Use this iterator macro if you plan to remove the
514 * element, elt, from the queue during the iteration.
516 * qe_foreach_element_safe(<type> *elt, queue_t head, <field>)
517 * elt - iteration variable
518 * <type> - what's in the queue (e.g., struct some_data)
519 * <field> - is the chain field in <type>
521 * This should only be used with Method 1 queue iteration (linkage chains)
523 #define qe_foreach_element_safe(elt, head, field) \
524 for (typeof(*(elt)) *_nelt = qe_element(((head)->next)->next, typeof(*(elt)), field), \
525 *__ ## elt ## _unused_shadow __unused = \
526 (elt = qe_element((head)->next, typeof(*(elt)), field)); \
527 &((elt)->field) != (head); \
528 elt = _nelt, _nelt = qe_element((elt)->field.next, typeof(*(elt)), field)) \
530 #ifdef XNU_KERNEL_PRIVATE
532 /* Dequeue an element from head, or return NULL if the queue is empty */
533 #define qe_dequeue_head(head, type, field) ({ \
534 queue_entry_t _tmp_entry = dequeue_head((head)); \
535 type *_tmp_element = (type*) NULL; \
536 if (_tmp_entry != (queue_entry_t) NULL) \
537 _tmp_element = qe_element(_tmp_entry, type, field); \
541 /* Dequeue an element from tail, or return NULL if the queue is empty */
542 #define qe_dequeue_tail(head, type, field) ({ \
543 queue_entry_t _tmp_entry = dequeue_tail((head)); \
544 type *_tmp_element = (type*) NULL; \
545 if (_tmp_entry != (queue_entry_t) NULL) \
546 _tmp_element = qe_element(_tmp_entry, type, field); \
550 /* Peek at the first element, or return NULL if the queue is empty */
551 #define qe_queue_first(head, type, field) ({ \
552 queue_entry_t _tmp_entry = queue_first((head)); \
553 type *_tmp_element = (type*) NULL; \
554 if (_tmp_entry != (queue_entry_t) head) \
555 _tmp_element = qe_element(_tmp_entry, type, field); \
559 /* Peek at the last element, or return NULL if the queue is empty */
560 #define qe_queue_last(head, type, field) ({ \
561 queue_entry_t _tmp_entry = queue_last((head)); \
562 type *_tmp_element = (type*) NULL; \
563 if (_tmp_entry != (queue_entry_t) head) \
564 _tmp_element = qe_element(_tmp_entry, type, field); \
568 /* Peek at the next element, or return NULL if the next element is head (indicating queue_end) */
569 #define qe_queue_next(head, element, type, field) ({ \
570 queue_entry_t _tmp_entry = queue_next(&(element)->field); \
571 type *_tmp_element = (type*) NULL; \
572 if (_tmp_entry != (queue_entry_t) head) \
573 _tmp_element = qe_element(_tmp_entry, type, field); \
577 /* Peek at the prev element, or return NULL if the prev element is head (indicating queue_end) */
578 #define qe_queue_prev(head, element, type, field) ({ \
579 queue_entry_t _tmp_entry = queue_prev(&(element)->field); \
580 type *_tmp_element = (type*) NULL; \
581 if (_tmp_entry != (queue_entry_t) head) \
582 _tmp_element = qe_element(_tmp_entry, type, field); \
586 #endif /* XNU_KERNEL_PRIVATE */
589 * Macro: QUEUE_HEAD_INITIALIZER()
591 * Static queue head initializer
593 #define QUEUE_HEAD_INITIALIZER(name) \
599 * Initialize the given queue.
602 * queue_t q; \* MODIFIED *\
604 #define queue_init(q) \
611 * Macro: queue_head_init
613 * Initialize the given queue head
615 * void queue_head_init(q)
616 * queue_head_t q; \* MODIFIED *\
618 #define queue_head_init(q) \
622 * Macro: queue_chain_init
624 * Initialize the given queue chain element
626 * void queue_chain_init(q)
627 * queue_chain_t q; \* MODIFIED *\
629 #define queue_chain_init(q) \
635 * Returns the first entry in the queue,
637 * queue_entry_t queue_first(q)
638 * queue_t q; \* IN *\
640 #define queue_first(q) ((q)->next)
645 * Returns the entry after an item in the queue.
647 * queue_entry_t queue_next(qc)
650 #define queue_next(qc) ((qc)->next)
655 * Returns the last entry in the queue.
657 * queue_entry_t queue_last(q)
658 * queue_t q; \* IN *\
660 #define queue_last(q) ((q)->prev)
665 * Returns the entry before an item in the queue.
667 * queue_entry_t queue_prev(qc)
670 #define queue_prev(qc) ((qc)->prev)
675 * Tests whether a new entry is really the end of
678 * boolean_t queue_end(q, qe)
682 #define queue_end(q, qe) ((q) == (qe))
687 * Tests whether a queue is empty.
689 * boolean_t queue_empty(q)
692 #define queue_empty(q) queue_end((q), queue_first(q))
697 * queue_t _old : head of a queue whose items will be moved
698 * queue_t _new : new queue head onto which items will be moved
700 * Rebase queue items in _old onto _new then re-initialize
701 * the _old object to an empty queue.
702 * Equivalent to the queue_new_head Method 2 macro
704 * Similar to the queue_new_head macro, this macros is intented
705 * to function as an initializer method for '_new' and thus may
706 * leak any list items that happen to be on the '_new' list.
707 * This should only be used with Method 1 queue iteration (linkage chains)
709 static __inline__
void
710 movqueue(queue_t _old
, queue_t _new
)
712 queue_entry_t next_elt
, prev_elt
;
714 __QUEUE_ELT_VALIDATE((queue_entry_t
)_old
);
716 if (queue_empty(_old
)) {
722 * move the queue at _old to _new
723 * and re-initialize _old
725 next_elt
= _old
->next
;
726 prev_elt
= _old
->prev
;
728 _new
->next
= next_elt
;
729 _new
->prev
= prev_elt
;
730 next_elt
->prev
= _new
;
731 prev_elt
->next
= _new
;
736 /*----------------------------------------------------------------*/
738 * Macros that operate on generic structures. The queue
739 * chain may be at any location within the structure, and there
740 * may be more than one chain.
746 * Insert a new element at the tail of the queue.
748 * void queue_enter(q, elt, type, field)
751 * <type> is what's in our queue
752 * <field> is the chain field in (*<type>)
754 * This should only be used with Method 2 queue iteration (element chains)
756 * We insert a compiler barrier after setting the fields in the element
757 * to ensure that the element is updated before being added to the queue,
758 * which is especially important because stackshot, which operates from
759 * debugger context, iterates several queues that use this macro (the tasks
760 * lists and threads lists) without locks. Without this barrier, the
761 * compiler may re-order the instructions for this macro in a way that
762 * could cause stackshot to trip over an inconsistent queue during
765 #define queue_enter(head, elt, type, field) \
767 queue_entry_t __prev; \
769 __prev = (head)->prev; \
770 (elt)->field.prev = __prev; \
771 (elt)->field.next = head; \
772 __compiler_barrier(); \
773 if ((head) == __prev) { \
774 (head)->next = (queue_entry_t) (elt); \
777 ((type)(void *)__prev)->field.next = \
778 (queue_entry_t)(elt); \
780 (head)->prev = (queue_entry_t) elt; \
784 * Macro: queue_enter_first
786 * Insert a new element at the head of the queue.
788 * void queue_enter_first(q, elt, type, field)
791 * <type> is what's in our queue
792 * <field> is the chain field in (*<type>)
794 * This should only be used with Method 2 queue iteration (element chains)
796 #define queue_enter_first(head, elt, type, field) \
798 queue_entry_t __next; \
800 __next = (head)->next; \
801 if ((head) == __next) { \
802 (head)->prev = (queue_entry_t) (elt); \
805 ((type)(void *)__next)->field.prev = \
806 (queue_entry_t)(elt); \
808 (elt)->field.next = __next; \
809 (elt)->field.prev = head; \
810 (head)->next = (queue_entry_t) elt; \
814 * Macro: queue_insert_before
816 * Insert a new element before a given element.
818 * void queue_insert_before(q, elt, cur, type, field)
822 * <type> is what's in our queue
823 * <field> is the chain field in (*<type>)
825 * This should only be used with Method 2 queue iteration (element chains)
827 #define queue_insert_before(head, elt, cur, type, field) \
829 queue_entry_t __prev; \
831 if ((head) == (queue_entry_t)(cur)) { \
832 (elt)->field.next = (head); \
833 if ((head)->next == (head)) { /* only element */ \
834 (elt)->field.prev = (head); \
835 (head)->next = (queue_entry_t)(elt); \
836 } else { /* last element */ \
837 __prev = (elt)->field.prev = (head)->prev; \
838 ((type)(void *)__prev)->field.next = \
839 (queue_entry_t)(elt); \
841 (head)->prev = (queue_entry_t)(elt); \
843 (elt)->field.next = (queue_entry_t)(cur); \
844 if ((head)->next == (queue_entry_t)(cur)) { \
845 /* first element */ \
846 (elt)->field.prev = (head); \
847 (head)->next = (queue_entry_t)(elt); \
848 } else { /* middle element */ \
849 __prev = (elt)->field.prev = (cur)->field.prev; \
850 ((type)(void *)__prev)->field.next = \
851 (queue_entry_t)(elt); \
853 (cur)->field.prev = (queue_entry_t)(elt); \
858 * Macro: queue_insert_after
860 * Insert a new element after a given element.
862 * void queue_insert_after(q, elt, cur, type, field)
866 * <type> is what's in our queue
867 * <field> is the chain field in (*<type>)
869 * This should only be used with Method 2 queue iteration (element chains)
871 #define queue_insert_after(head, elt, cur, type, field) \
873 queue_entry_t __next; \
875 if ((head) == (queue_entry_t)(cur)) { \
876 (elt)->field.prev = (head); \
877 if ((head)->next == (head)) { /* only element */ \
878 (elt)->field.next = (head); \
879 (head)->prev = (queue_entry_t)(elt); \
880 } else { /* first element */ \
881 __next = (elt)->field.next = (head)->next; \
882 ((type)(void *)__next)->field.prev = \
883 (queue_entry_t)(elt); \
885 (head)->next = (queue_entry_t)(elt); \
887 (elt)->field.prev = (queue_entry_t)(cur); \
888 if ((head)->prev == (queue_entry_t)(cur)) { \
890 (elt)->field.next = (head); \
891 (head)->prev = (queue_entry_t)(elt); \
892 } else { /* middle element */ \
893 __next = (elt)->field.next = (cur)->field.next; \
894 ((type)(void *)__next)->field.prev = \
895 (queue_entry_t)(elt); \
897 (cur)->field.next = (queue_entry_t)(elt); \
902 * Macro: queue_field [internal use only]
904 * Find the queue_chain_t (or queue_t) for the
905 * given element (thing) in the given queue (head)
907 * This should only be used with Method 2 queue iteration (element chains)
909 #define queue_field(head, thing, type, field) \
910 (((head) == (thing)) ? (head) : &((type)(void *)(thing))->field)
913 * Macro: queue_remove
915 * Remove an arbitrary item from the queue.
917 * void queue_remove(q, qe, type, field)
918 * arguments as in queue_enter
920 * This should only be used with Method 2 queue iteration (element chains)
922 #define queue_remove(head, elt, type, field) \
924 queue_entry_t __next, __prev; \
926 __next = (elt)->field.next; \
927 __prev = (elt)->field.prev; \
929 if ((head) == __next) \
930 (head)->prev = __prev; \
932 ((type)(void *)__next)->field.prev = __prev; \
934 if ((head) == __prev) \
935 (head)->next = __next; \
937 ((type)(void *)__prev)->field.next = __next; \
939 (elt)->field.next = NULL; \
940 (elt)->field.prev = NULL; \
944 * Macro: queue_remove_first
946 * Remove and return the entry at the head of
949 * queue_remove_first(head, entry, type, field)
950 * entry is returned by reference
952 * This should only be used with Method 2 queue iteration (element chains)
954 #define queue_remove_first(head, entry, type, field) \
956 queue_entry_t __next; \
958 (entry) = (type)(void *) ((head)->next); \
959 __next = (entry)->field.next; \
961 if ((head) == __next) \
962 (head)->prev = (head); \
964 ((type)(void *)(__next))->field.prev = (head); \
965 (head)->next = __next; \
967 (entry)->field.next = NULL; \
968 (entry)->field.prev = NULL; \
972 * Macro: queue_remove_last
974 * Remove and return the entry at the tail of
977 * queue_remove_last(head, entry, type, field)
978 * entry is returned by reference
980 * This should only be used with Method 2 queue iteration (element chains)
982 #define queue_remove_last(head, entry, type, field) \
984 queue_entry_t __prev; \
986 (entry) = (type)(void *) ((head)->prev); \
987 __prev = (entry)->field.prev; \
989 if ((head) == __prev) \
990 (head)->next = (head); \
992 ((type)(void *)(__prev))->field.next = (head); \
993 (head)->prev = __prev; \
995 (entry)->field.next = NULL; \
996 (entry)->field.prev = NULL; \
1000 * Macro: queue_assign
1002 * This should only be used with Method 2 queue iteration (element chains)
1004 #define queue_assign(to, from, type, field) \
1006 ((type)(void *)((from)->prev))->field.next = (to); \
1007 ((type)(void *)((from)->next))->field.prev = (to); \
1012 * Macro: queue_new_head
1014 * rebase old queue to new queue head
1016 * queue_new_head(old, new, type, field)
1019 * <type> is what's in our queue
1020 * <field> is the chain field in (*<type>)
1022 * This should only be used with Method 2 queue iteration (element chains)
1024 #define queue_new_head(old, new, type, field) \
1026 if (!queue_empty(old)) { \
1028 ((type)(void *)((new)->next))->field.prev = \
1030 ((type)(void *)((new)->prev))->field.next = \
1038 * Macro: queue_iterate
1040 * iterate over each item in the queue.
1041 * Generates a 'for' loop, setting elt to
1042 * each item in turn (by reference).
1044 * queue_iterate(q, elt, type, field)
1047 * <type> is what's in our queue
1048 * <field> is the chain field in (*<type>)
1050 * This should only be used with Method 2 queue iteration (element chains)
1052 #define queue_iterate(head, elt, type, field) \
1053 for ((elt) = (type)(void *) queue_first(head); \
1054 !queue_end((head), (queue_entry_t)(elt)); \
1055 (elt) = (type)(void *) queue_next(&(elt)->field))
1060 #endif /* _KERN_QUEUE_H_ */