2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)queue.h 8.5 (Berkeley) 8/20/94
30 * $FreeBSD: src/sys/sys/queue.h,v 1.58 2004/04/07 04:19:49 imp Exp $
31 * $Id: //depot/sw/branches/fusion_usb/target_firmware/magpie_fw_dev/asf/include/asf_queue.h#1 $
38 * General purpose routines
40 #define asf_offsetof(type, member) ((adf_os_size_t) &((type *)0)->member)
42 #define asf_containerof(ptr, type, member) ({ \
43 const typeof( ((type *)0)->member ) *__lptr = (ptr); \
44 (type *)( (char *)__mptr - asf_offsetof(type,member) );})
47 * This file defines four types of data structures: singly-linked lists,
48 * singly-linked tail queues, lists and tail queues.
50 * A singly-linked list is headed by a single forward pointer. The elements
51 * are singly linked for minimum space and pointer manipulation overhead at
52 * the expense of O(n) removal for arbitrary elements. New elements can be
53 * added to the list after an existing element or at the head of the list.
54 * Elements being removed from the head of the list should use the explicit
55 * macro for this purpose for optimum efficiency. A singly-linked list may
56 * only be traversed in the forward direction. Singly-linked lists are ideal
57 * for applications with large datasets and few or no removals or for
58 * implementing a LIFO queue.
60 * A singly-linked tail queue is headed by a pair of pointers, one to the
61 * head of the list and the other to the tail of the list. The elements are
62 * singly linked for minimum space and pointer manipulation overhead at the
63 * expense of O(n) removal for arbitrary elements. New elements can be added
64 * to the list after an existing element, at the head of the list, or at the
65 * end of the list. Elements being removed from the head of the tail queue
66 * should use the explicit macro for this purpose for optimum efficiency.
67 * A singly-linked tail queue may only be traversed in the forward direction.
68 * Singly-linked tail queues are ideal for applications with large datasets
69 * and few or no removals or for implementing a FIFO queue.
71 * A list is headed by a single forward pointer (or an array of forward
72 * pointers for a hash table header). The elements are doubly linked
73 * so that an arbitrary element can be removed without a need to
74 * traverse the list. New elements can be added to the list before
75 * or after an existing element or at the head of the list. A list
76 * may only be traversed in the forward direction.
78 * A tail queue is headed by a pair of pointers, one to the head of the
79 * list and the other to the tail of the list. The elements are doubly
80 * linked so that an arbitrary element can be removed without a need to
81 * traverse the list. New elements can be added to the list before or
82 * after an existing element, at the head of the list, or at the end of
83 * the list. A tail queue may be traversed in either direction.
85 * For details on the use of these macros, see the queue(3) manual page.
88 * asf_slist asf_list asf_stailq asf_tailq
90 * _head_initializer + + + +
99 * _foreach_safe + + + +
100 * _foreach_reverse - - - +
101 * _foreach_reverse_safe - - - +
102 * _insert_head + + + +
103 * _insert_before - + - +
104 * _insert_after + + + +
105 * _insert_tail - - + +
107 * _remove_head + - + -
111 #define QUEUE_MACRO_DEBUG 0
112 #if QUEUE_MACRO_DEBUG
113 /* Store the last 2 places the queue element or head was altered */
114 struct asf_qm_trace {
121 #define TRACEBUF struct asf_qm_trace trace
122 #define trashit(x) do {(x) = (void *)-1;} while (0)
124 #define qmd_trace_head(head) do { \
125 (head)->trace.prevline = (head)->trace.lastline; \
126 (head)->trace.prevfile = (head)->trace.lastfile; \
127 (head)->trace.lastline = __LINE__; \
128 (head)->trace.lastfile = __FILE__; \
131 #define qmd_trace_elem(elem) do { \
132 (elem)->trace.prevline = (elem)->trace.lastline; \
133 (elem)->trace.prevfile = (elem)->trace.lastfile; \
134 (elem)->trace.lastline = __LINE__; \
135 (elem)->trace.lastfile = __FILE__; \
139 #define qmd_trace_elem(elem)
140 #define qmd_trace_head(head)
143 #endif /* QUEUE_MACRO_DEBUG */
146 * Singly-linked List declarations.
148 #define asf_slist_head(name, type) \
150 struct type *slh_first; /* first element */ \
153 #define asf_slist_head_initializer(head) \
156 #define asf_slist_entry(type) \
158 struct type *sle_next; /* next element */ \
162 * Singly-linked List functions.
164 #define asf_slist_empty(head) ((head)->slh_first == NULL)
166 #define asf_slist_first(head) ((head)->slh_first)
168 #define asf_slist_foreach(var, head, field) \
169 for ((var) = asf_slist_first((head)); \
171 (var) = asf_slist_next((var), field))
173 #define asf_slist_foreach_safe(var, head, field, tvar) \
174 for ((var) = asf_slist_first((head)); \
175 (var) && ((tvar) = asf_slist_next((var), field), 1); \
178 #define asf_slist_foreach_prevptr(var, varp, head, field) \
179 for ((varp) = &asf_slist_first((head)); \
180 ((var) = *(varp)) != NULL; \
181 (varp) = &asf_slist_next((var), field))
183 #define asf_slist_init(head) do { \
184 asf_slist_first((head)) = NULL; \
187 #define asf_slist_insert_after(slistelm, elm, field) do { \
188 asf_slist_next((elm), field) = asf_slist_next((slistelm), field); \
189 asf_slist_next((slistelm), field) = (elm); \
192 #define asf_slist_insert_head(head, elm, field) do { \
193 asf_slist_next((elm), field) = asf_slist_first((head)); \
194 asf_slist_first((head)) = (elm); \
197 #define asf_slist_next(elm, field) ((elm)->field.sle_next)
199 #define asf_slist_remove(head, elm, type, field) do { \
200 if (asf_slist_first((head)) == (elm)) { \
201 asf_slist_remove_head((head), field); \
204 struct type *curelm = asf_slist_first((head)); \
205 while (asf_slist_next(curelm, field) != (elm)) \
206 curelm = asf_slist_next(curelm, field); \
207 asf_slist_next(curelm, field) = \
208 asf_slist_next(asf_slist_next(curelm, field), field); \
212 #define asf_slist_remove_head(head, field) do { \
213 asf_slist_first((head)) = asf_slist_next(asf_slist_first((head)), field); \
217 * Singly-linked Tail queue declarations.
219 #define asf_stailq_head(name, type) \
221 struct type *stqh_first;/* first element */ \
222 struct type **stqh_last;/* addr of last next element */ \
225 #define asf_stailq_head_initializer(head) \
226 { NULL, &(head).stqh_first }
228 #define asf_stailq_entry(type) \
230 struct type *stqe_next; /* next element */ \
234 * Singly-linked Tail queue functions.
236 #define asf_stailq_concat(head1, head2) do { \
237 if (!asf_stailq_empty((head2))) { \
238 *(head1)->stqh_last = (head2)->stqh_first; \
239 (head1)->stqh_last = (head2)->stqh_last; \
240 asf_stailq_init((head2)); \
244 #define asf_stailq_empty(head) ((head)->stqh_first == NULL)
246 #define asf_stailq_first(head) ((head)->stqh_first)
248 #define asf_stailq_foreach(var, head, field) \
249 for((var) = asf_stailq_first((head)); \
251 (var) = asf_stailq_next((var), field))
254 #define asf_stailq_foreach_safe(var, head, field, tvar) \
255 for ((var) = asf_stailq_first((head)); \
256 (var) && ((tvar) = asf_stailq_next((var), field), 1); \
259 #define asf_stailq_init(head) do { \
260 asf_stailq_first((head)) = NULL; \
261 (head)->stqh_last = &asf_stailq_first((head)); \
264 #define asf_stailq_insert_after(head, tqelm, elm, field) do { \
265 if ((asf_stailq_next((elm), field) = asf_stailq_next((tqelm), field)) == NULL)\
266 (head)->stqh_last = &asf_stailq_next((elm), field); \
267 asf_stailq_next((tqelm), field) = (elm); \
270 #define asf_stailq_insert_head(head, elm, field) do { \
271 if ((asf_stailq_next((elm), field) = asf_stailq_first((head))) == NULL) \
272 (head)->stqh_last = &asf_stailq_next((elm), field); \
273 asf_stailq_first((head)) = (elm); \
276 #define asf_stailq_insert_tail(head, elm, field) do { \
277 asf_stailq_next((elm), field) = NULL; \
278 *(head)->stqh_last = (elm); \
279 (head)->stqh_last = &asf_stailq_next((elm), field); \
282 #define asf_stailq_last(head, type, field) \
283 (asf_stailq_empty((head)) ? \
286 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
288 #define asf_stailq_next(elm, field) ((elm)->field.stqe_next)
290 #define asf_stailq_remove(head, elm, type, field) do { \
291 if (asf_stailq_first((head)) == (elm)) { \
292 asf_stailq_remove_head((head), field); \
295 struct type *curelm = asf_stailq_first((head)); \
296 while (asf_stailq_next(curelm, field) != (elm)) \
297 curelm = asf_stailq_next(curelm, field); \
298 if ((asf_stailq_next(curelm, field) = \
299 asf_stailq_next(asf_stailq_next(curelm, field), field)) == NULL)\
300 (head)->stqh_last = &asf_stailq_next((curelm), field);\
305 #define asf_stailq_remove_after(head, elm, field) do { \
306 if (asf_stailq_next(elm, field)) { \
307 if ((asf_stailq_next(elm, field) = \
308 asf_stailq_next(asf_stailq_next(elm, field), field)) == NULL)\
309 (head)->stqh_last = &asf_stailq_next((elm), field); \
314 #define asf_stailq_remove_head(head, field) do { \
315 if ((asf_stailq_first((head)) = \
316 asf_stailq_next(asf_stailq_first((head)), field)) == NULL) \
317 (head)->stqh_last = &asf_stailq_first((head)); \
320 #define asf_stailq_remove_head_until(head, elm, field) do { \
321 if ((asf_stailq_first((head)) = asf_stailq_next((elm), field)) == NULL) \
322 (head)->stqh_last = &asf_stailq_first((head)); \
328 #define asf_list_head(name, type) \
330 struct type *lh_first; /* first element */ \
333 #define asf_list_head_initializer(head) \
336 #define asf_list_entry(type) \
338 struct type *le_next; /* next element */ \
339 struct type **le_prev; /* address of previous next element */ \
346 #define asf_list_empty(head) ((head)->lh_first == NULL)
348 #define asf_list_first(head) ((head)->lh_first)
350 #define asf_list_foreach(var, head, field) \
351 for ((var) = asf_list_first((head)); \
353 (var) = asf_list_next((var), field))
355 #define asf_list_foreach_safe(var, head, field, tvar) \
356 for ((var) = asf_list_first((head)); \
357 (var) && ((tvar) = asf_list_next((var), field), 1); \
360 #define asf_list_init(head) do { \
361 asf_list_first((head)) = NULL; \
364 #define asf_list_insert_after(listelm, elm, field) do { \
365 if ((asf_list_next((elm), field) = asf_list_next((listelm), field)) != NULL)\
366 asf_list_next((listelm), field)->field.le_prev = \
367 &asf_list_next((elm), field); \
368 asf_list_next((listelm), field) = (elm); \
369 (elm)->field.le_prev = &asf_list_next((listelm), field); \
372 #define asf_list_insert_before(listelm, elm, field) do { \
373 (elm)->field.le_prev = (listelm)->field.le_prev; \
374 asf_list_next((elm), field) = (listelm); \
375 *(listelm)->field.le_prev = (elm); \
376 (listelm)->field.le_prev = &asf_list_next((elm), field); \
379 #define asf_list_insert_head(head, elm, field) do { \
380 if ((asf_list_next((elm), field) = asf_list_first((head))) != NULL) \
381 asf_list_first((head))->field.le_prev = &asf_list_next((elm), field);\
382 asf_list_first((head)) = (elm); \
383 (elm)->field.le_prev = &asf_list_first((head)); \
386 #define asf_list_next(elm, field) ((elm)->field.le_next)
388 #define asf_list_remove(elm, field) do { \
389 if (asf_list_next((elm), field) != NULL) \
390 asf_list_next((elm), field)->field.le_prev = \
391 (elm)->field.le_prev; \
392 *(elm)->field.le_prev = asf_list_next((elm), field); \
396 * Tail queue declarations.
398 #define asf_tailq_head(name, type) \
400 struct type *tqh_first; /* first element */ \
401 struct type **tqh_last; /* addr of last next element */ \
405 #define asf_tailq_head_initializer(head) \
406 { NULL, &(head).tqh_first }
408 #define asf_tailq_entry(type) \
410 struct type *tqe_next; /* next element */ \
411 struct type **tqe_prev; /* address of previous next element */ \
416 * Tail queue functions.
418 #define asf_tailq_concat(head1, head2, field) do { \
419 if (!asf_tailq_empty(head2)) { \
420 *(head1)->tqh_last = (head2)->tqh_first; \
421 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
422 (head1)->tqh_last = (head2)->tqh_last; \
423 asf_tailq_init((head2)); \
424 qmd_trace_head(head); \
425 qmd_trace_head(head2); \
429 #define asf_tailq_empty(head) ((head)->tqh_first == NULL)
431 #define asf_tailq_first(head) ((head)->tqh_first)
433 #define asf_tailq_foreach(var, head, field) \
434 for ((var) = asf_tailq_first((head)); \
436 (var) = asf_tailq_next((var), field))
438 #define asf_tailq_foreach_safe(var, head, field, tvar) \
439 for ((var) = asf_tailq_first((head)); \
440 (var) && ((tvar) = asf_tailq_next((var), field), 1); \
443 #define asf_tailq_foreach_reverse(var, head, headname, field) \
444 for ((var) = asf_tailq_last((head), headname); \
446 (var) = asf_tailq_prev((var), headname, field))
448 #define asf_tailq_foreach_reverse_safe(var, head, headname, field, tvar) \
449 for ((var) = asf_tailq_last((head), headname); \
450 (var) && ((tvar) = asf_tailq_prev((var), headname, field), 1); \
453 #define asf_tailq_init(head) do { \
454 asf_tailq_first((head)) = NULL; \
455 (head)->tqh_last = &asf_tailq_first((head)); \
456 qmd_trace_head(head); \
459 #define asf_tailq_insert_after(head, listelm, elm, field) do { \
460 if ((asf_tailq_next((elm), field) = asf_tailq_next((listelm), field)) != NULL)\
461 asf_tailq_next((elm), field)->field.tqe_prev = \
462 &asf_tailq_next((elm), field); \
464 (head)->tqh_last = &asf_tailq_next((elm), field); \
465 qmd_trace_head(head); \
467 asf_tailq_next((listelm), field) = (elm); \
468 (elm)->field.tqe_prev = &asf_tailq_next((listelm), field); \
469 qmd_trace_elem(&(elm)->field); \
470 qmd_trace_elem(&listelm->field); \
473 #define asf_tailq_insert_before(listelm, elm, field) do { \
474 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
475 asf_tailq_next((elm), field) = (listelm); \
476 *(listelm)->field.tqe_prev = (elm); \
477 (listelm)->field.tqe_prev = &asf_tailq_next((elm), field); \
478 qmd_trace_elem(&(elm)->field); \
479 qmd_trace_elem(&listelm->field); \
482 #define asf_tailq_insert_head(head, elm, field) do { \
483 if ((asf_tailq_next((elm), field) = asf_tailq_first((head))) != NULL) \
484 asf_tailq_first((head))->field.tqe_prev = \
485 &asf_tailq_next((elm), field); \
487 (head)->tqh_last = &asf_tailq_next((elm), field); \
488 asf_tailq_first((head)) = (elm); \
489 (elm)->field.tqe_prev = &asf_tailq_first((head)); \
490 qmd_trace_head(head); \
491 qmd_trace_elem(&(elm)->field); \
494 #define asf_tailq_insert_tail(head, elm, field) do { \
495 asf_tailq_next((elm), field) = NULL; \
496 (elm)->field.tqe_prev = (head)->tqh_last; \
497 *(head)->tqh_last = (elm); \
498 (head)->tqh_last = &asf_tailq_next((elm), field); \
499 qmd_trace_head(head); \
500 qmd_trace_elem(&(elm)->field); \
503 #define asf_tailq_last(head, headname) \
504 (*(((struct headname *)((head)->tqh_last))->tqh_last))
506 #define asf_tailq_next(elm, field) ((elm)->field.tqe_next)
508 #define asf_tailq_prev(elm, headname, field) \
509 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
511 #define asf_tailq_remove(head, elm, field) do { \
512 if ((asf_tailq_next((elm), field)) != NULL) \
513 asf_tailq_next((elm), field)->field.tqe_prev = \
514 (elm)->field.tqe_prev; \
516 (head)->tqh_last = (elm)->field.tqe_prev; \
517 qmd_trace_head(head); \
519 *(elm)->field.tqe_prev = asf_tailq_next((elm), field); \
520 trashit((elm)->field.tqe_next); \
521 trashit((elm)->field.tqe_prev); \
522 qmd_trace_elem(&(elm)->field); \
529 * XXX asf_insque() and remque() are an old way of handling certain queues.
530 * They bogusly assumes that all queue heads look alike.
534 struct asf_qhead *qh_link;
535 struct asf_qhead *qh_rlink;
538 #if defined(__GNUC__) || defined(__INTEL_COMPILER)
541 asf_insque(void *a, void *b)
543 struct asf_qhead *element = (struct asf_qhead *)a,
544 *head = (struct asf_qhead *)b;
546 element->qh_link = head->qh_link;
547 element->qh_rlink = head;
548 head->qh_link = element;
549 element->qh_link->qh_rlink = element;
555 struct asf_qhead *element = (struct asf_qhead *)a;
557 element->qh_link->qh_rlink = element->qh_rlink;
558 element->qh_rlink->qh_link = element->qh_link;
559 element->qh_rlink = 0;
562 #else /* !(__GNUC__ || __INTEL_COMPILER) */
564 void asf_insque(void *a, void *b);
565 void asf_remque(void *a);
567 #endif /* __GNUC__ || __INTEL_COMPILER */
571 #endif /* !_ASF_QUEUE_H_ */