update madwifi
[linux-2.6/zen-sources.git] / drivers / net / wireless / madwifi / include / sys / queue.h
blob079a1cb10643983cd5338b72c32515324be21b9b
1 /*
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
7 * are met:
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
27 * SUCH DAMAGE.
29 * $Id: queue.h 2392 2007-05-29 21:55:25Z mtaylor $
32 #ifndef _SYS_QUEUE_H_
33 #define _SYS_QUEUE_H_
36 * This file defines four types of data structures: singly-linked lists,
37 * singly-linked tail queues, lists and tail queues.
39 * A singly-linked list is headed by a single forward pointer. The elements
40 * are singly linked for minimum space and pointer manipulation overhead at
41 * the expense of O(n) removal for arbitrary elements. New elements can be
42 * added to the list after an existing element or at the head of the list.
43 * Elements being removed from the head of the list should use the explicit
44 * macro for this purpose for optimum efficiency. A singly-linked list may
45 * only be traversed in the forward direction. Singly-linked lists are ideal
46 * for applications with large datasets and few or no removals or for
47 * implementing a LIFO queue.
49 * A singly-linked tail queue is headed by a pair of pointers, one to the
50 * head of the list and the other to the tail of the list. The elements are
51 * singly linked for minimum space and pointer manipulation overhead at the
52 * expense of O(n) removal for arbitrary elements. New elements can be added
53 * to the list after an existing element, at the head of the list, or at the
54 * end of the list. Elements being removed from the head of the tail queue
55 * should use the explicit macro for this purpose for optimum efficiency.
56 * A singly-linked tail queue may only be traversed in the forward direction.
57 * Singly-linked tail queues are ideal for applications with large datasets
58 * and few or no removals or for implementing a FIFO queue.
60 * A list is headed by a single forward pointer (or an array of forward
61 * pointers for a hash table header). The elements are doubly linked
62 * so that an arbitrary element can be removed without a need to
63 * traverse the list. New elements can be added to the list before
64 * or after an existing element or at the head of the list. A list
65 * may only be traversed in the forward direction.
67 * A tail queue is headed by a pair of pointers, one to the head of the
68 * list and the other to the tail of the list. The elements are doubly
69 * linked so that an arbitrary element can be removed without a need to
70 * traverse the list. New elements can be added to the list before or
71 * after an existing element, at the head of the list, or at the end of
72 * the list. A tail queue may be traversed in either direction.
74 * For details on the use of these macros, see the queue(3) manual page.
77 * SLIST LIST STAILQ TAILQ
78 * _HEAD + + + +
79 * _HEAD_INITIALIZER + + + +
80 * _ENTRY + + + +
81 * _INIT + + + +
82 * _EMPTY + + + +
83 * _FIRST + + + +
84 * _NEXT + + + +
85 * _PREV - - - +
86 * _LAST - - + +
87 * _FOREACH + + + +
88 * _FOREACH_SAFE + + + +
89 * _FOREACH_REVERSE - - - +
90 * _FOREACH_REVERSE_SAFE - - - +
91 * _INSERT_HEAD + + + +
92 * _INSERT_BEFORE - + - +
93 * _INSERT_AFTER + + + +
94 * _INSERT_TAIL - - + +
95 * _CONCAT - - + +
96 * _REMOVE_HEAD + - + -
97 * _REMOVE + + + +
100 #define QUEUE_MACRO_DEBUG 0
101 #if QUEUE_MACRO_DEBUG
102 /* Store the last 2 places the queue element or head was altered */
103 struct qm_trace {
104 char *lastfile;
105 int lastline;
106 char *prevfile;
107 int prevline;
110 #define TRACEBUF struct qm_trace trace;
111 #define TRASHIT(x) do {(x) = (void *)-1;} while (0)
113 #define QMD_TRACE_HEAD(head) do { \
114 (head)->trace.prevline = (head)->trace.lastline; \
115 (head)->trace.prevfile = (head)->trace.lastfile; \
116 (head)->trace.lastline = __LINE__; \
117 (head)->trace.lastfile = __FILE__; \
118 } while (0)
120 #define QMD_TRACE_ELEM(elem) do { \
121 (elem)->trace.prevline = (elem)->trace.lastline; \
122 (elem)->trace.prevfile = (elem)->trace.lastfile; \
123 (elem)->trace.lastline = __LINE__; \
124 (elem)->trace.lastfile = __FILE__; \
125 } while (0)
127 #else
128 #define QMD_TRACE_ELEM(elem)
129 #define QMD_TRACE_HEAD(head)
130 #define TRACEBUF
131 #define TRASHIT(x)
132 #endif /* QUEUE_MACRO_DEBUG */
135 * Singly-linked List declarations.
137 #define SLIST_HEAD(name, type) \
138 struct name { \
139 struct type *slh_first; /* first element */ \
142 #define SLIST_HEAD_INITIALIZER(head) \
143 { NULL }
145 #define SLIST_ENTRY(type) \
146 struct { \
147 struct type *sle_next; /* next element */ \
151 * Singly-linked List functions.
153 #define SLIST_EMPTY(head) ((head)->slh_first == NULL)
155 #define SLIST_FIRST(head) ((head)->slh_first)
157 #define SLIST_FOREACH(var, head, field) \
158 for ((var) = SLIST_FIRST((head)); \
159 (var); \
160 (var) = SLIST_NEXT((var), field))
162 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
163 for ((var) = SLIST_FIRST((head)); \
164 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
165 (var) = (tvar))
167 #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
168 for ((varp) = &SLIST_FIRST((head)); \
169 ((var) = *(varp)) != NULL; \
170 (varp) = &SLIST_NEXT((var), field))
172 #define SLIST_INIT(head) do { \
173 SLIST_FIRST((head)) = NULL; \
174 } while (0)
176 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
177 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
178 SLIST_NEXT((slistelm), field) = (elm); \
179 } while (0)
181 #define SLIST_INSERT_HEAD(head, elm, field) do { \
182 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
183 SLIST_FIRST((head)) = (elm); \
184 } while (0)
186 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
188 #define SLIST_REMOVE(head, elm, type, field) do { \
189 if (SLIST_FIRST((head)) == (elm)) { \
190 SLIST_REMOVE_HEAD((head), field); \
192 else { \
193 struct type *curelm = SLIST_FIRST((head)); \
194 while (SLIST_NEXT(curelm, field) != (elm)) \
195 curelm = SLIST_NEXT(curelm, field); \
196 SLIST_NEXT(curelm, field) = \
197 SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
199 } while (0)
201 #define SLIST_REMOVE_HEAD(head, field) do { \
202 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
203 } while (0)
206 * Singly-linked Tail queue declarations.
208 #define STAILQ_HEAD(name, type) \
209 struct name { \
210 struct type *stqh_first;/* first element */ \
211 struct type **stqh_last;/* addr of last next element */ \
214 #define STAILQ_HEAD_INITIALIZER(head) \
215 { NULL, &(head).stqh_first }
217 #define STAILQ_ENTRY(type) \
218 struct { \
219 struct type *stqe_next; /* next element */ \
223 * Singly-linked Tail queue functions.
225 #define STAILQ_CONCAT(head1, head2) do { \
226 if (!STAILQ_EMPTY((head2))) { \
227 *(head1)->stqh_last = (head2)->stqh_first; \
228 (head1)->stqh_last = (head2)->stqh_last; \
229 STAILQ_INIT((head2)); \
231 } while (0)
233 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
235 #define STAILQ_FIRST(head) ((head)->stqh_first)
237 #define STAILQ_FOREACH(var, head, field) \
238 for ((var) = STAILQ_FIRST((head)); \
239 (var); \
240 (var) = STAILQ_NEXT((var), field))
243 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
244 for ((var) = STAILQ_FIRST((head)); \
245 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
246 (var) = (tvar))
248 #define STAILQ_INIT(head) do { \
249 STAILQ_FIRST((head)) = NULL; \
250 (head)->stqh_last = &STAILQ_FIRST((head)); \
251 } while (0)
253 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
254 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
255 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
256 STAILQ_NEXT((tqelm), field) = (elm); \
257 } while (0)
259 #define STAILQ_INSERT_HEAD(head, elm, field) do { \
260 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
261 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
262 STAILQ_FIRST((head)) = (elm); \
263 } while (0)
265 #define STAILQ_INSERT_TAIL(head, elm, field) do { \
266 STAILQ_NEXT((elm), field) = NULL; \
267 *(head)->stqh_last = (elm); \
268 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
269 } while (0)
271 #define STAILQ_LAST(head, type, field) \
272 (STAILQ_EMPTY((head)) ? \
273 NULL : \
274 ((struct type *) \
275 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
277 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
279 #define STAILQ_REMOVE(head, elm, type, field) do { \
280 if (STAILQ_FIRST((head)) == (elm)) { \
281 STAILQ_REMOVE_HEAD((head), field); \
283 else { \
284 struct type *curelm = STAILQ_FIRST((head)); \
285 while (STAILQ_NEXT(curelm, field) != (elm)) \
286 curelm = STAILQ_NEXT(curelm, field); \
287 if ((STAILQ_NEXT(curelm, field) = \
288 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
289 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
291 } while (0)
294 #define STAILQ_REMOVE_AFTER(head, elm, field) do { \
295 if (STAILQ_NEXT(elm, field)) { \
296 if ((STAILQ_NEXT(elm, field) = \
297 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL)\
298 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
300 } while (0)
303 #define STAILQ_REMOVE_HEAD(head, field) do { \
304 if ((STAILQ_FIRST((head)) = \
305 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
306 (head)->stqh_last = &STAILQ_FIRST((head)); \
307 } while (0)
309 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
310 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
311 (head)->stqh_last = &STAILQ_FIRST((head)); \
312 } while (0)
315 * List declarations.
317 #define ATH_LIST_HEAD(name, type) \
318 struct name { \
319 struct type *lh_first; /* first element */ \
322 #define LIST_HEAD_INITIALIZER(head) \
323 { NULL }
325 #define LIST_ENTRY(type) \
326 struct { \
327 struct type *le_next; /* next element */ \
328 struct type **le_prev; /* address of previous next element */ \
332 * List functions.
335 #define LIST_EMPTY(head) ((head)->lh_first == NULL)
337 #define LIST_FIRST(head) ((head)->lh_first)
339 #define LIST_FOREACH(var, head, field) \
340 for ((var) = LIST_FIRST((head)); \
341 (var); \
342 (var) = LIST_NEXT((var), field))
344 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
345 for ((var) = LIST_FIRST((head)); \
346 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
347 (var) = (tvar))
349 #define LIST_INIT(head) do { \
350 LIST_FIRST((head)) = NULL; \
351 } while (0)
353 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
354 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
355 LIST_NEXT((listelm), field)->field.le_prev = \
356 &LIST_NEXT((elm), field); \
357 LIST_NEXT((listelm), field) = (elm); \
358 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
359 } while (0)
361 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
362 (elm)->field.le_prev = (listelm)->field.le_prev; \
363 LIST_NEXT((elm), field) = (listelm); \
364 *(listelm)->field.le_prev = (elm); \
365 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
366 } while (0)
368 #define LIST_INSERT_HEAD(head, elm, field) do { \
369 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
370 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
371 LIST_FIRST((head)) = (elm); \
372 (elm)->field.le_prev = &LIST_FIRST((head)); \
373 } while (0)
375 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
377 #define LIST_REMOVE(elm, field) do { \
378 if (LIST_NEXT((elm), field) != NULL) \
379 LIST_NEXT((elm), field)->field.le_prev = \
380 (elm)->field.le_prev; \
381 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
382 } while (0)
385 * Tail queue declarations.
387 #define TAILQ_HEAD(name, type) \
388 struct name { \
389 struct type *tqh_first; /* first element */ \
390 struct type **tqh_last; /* addr of last next element */ \
391 TRACEBUF \
394 #define TAILQ_HEAD_INITIALIZER(head) \
395 { NULL, &(head).tqh_first }
397 #define TAILQ_ENTRY(type) \
398 struct { \
399 struct type *tqe_next; /* next element */ \
400 struct type **tqe_prev; /* address of previous next element */ \
401 TRACEBUF \
405 * Tail queue functions.
407 #define TAILQ_CONCAT(head1, head2, field) do { \
408 if (!TAILQ_EMPTY(head2)) { \
409 *(head1)->tqh_last = (head2)->tqh_first; \
410 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
411 (head1)->tqh_last = (head2)->tqh_last; \
412 TAILQ_INIT((head2)); \
413 QMD_TRACE_HEAD(head); \
414 QMD_TRACE_HEAD(head2); \
416 } while (0)
418 #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
420 #define TAILQ_FIRST(head) ((head)->tqh_first)
422 #define TAILQ_FOREACH(var, head, field) \
423 for ((var) = TAILQ_FIRST((head)); \
424 (var); \
425 (var) = TAILQ_NEXT((var), field))
427 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
428 for ((var) = TAILQ_FIRST((head)); \
429 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
430 (var) = (tvar))
432 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
433 for ((var) = TAILQ_LAST((head), headname); \
434 (var); \
435 (var) = TAILQ_PREV((var), headname, field))
437 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
438 for ((var) = TAILQ_LAST((head), headname); \
439 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
440 (var) = (tvar))
442 #define TAILQ_INIT(head) do { \
443 TAILQ_FIRST((head)) = NULL; \
444 (head)->tqh_last = &TAILQ_FIRST((head)); \
445 QMD_TRACE_HEAD(head); \
446 } while (0)
448 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
449 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
450 TAILQ_NEXT((elm), field)->field.tqe_prev = \
451 &TAILQ_NEXT((elm), field); \
452 else { \
453 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
454 QMD_TRACE_HEAD(head); \
456 TAILQ_NEXT((listelm), field) = (elm); \
457 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
458 QMD_TRACE_ELEM(&(elm)->field); \
459 QMD_TRACE_ELEM(&listelm->field); \
460 } while (0)
462 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
463 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
464 TAILQ_NEXT((elm), field) = (listelm); \
465 *(listelm)->field.tqe_prev = (elm); \
466 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
467 QMD_TRACE_ELEM(&(elm)->field); \
468 QMD_TRACE_ELEM(&listelm->field); \
469 } while (0)
471 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
472 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
473 TAILQ_FIRST((head))->field.tqe_prev = \
474 &TAILQ_NEXT((elm), field); \
475 else \
476 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
477 TAILQ_FIRST((head)) = (elm); \
478 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
479 QMD_TRACE_HEAD(head); \
480 QMD_TRACE_ELEM(&(elm)->field); \
481 } while (0)
483 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
484 TAILQ_NEXT((elm), field) = NULL; \
485 (elm)->field.tqe_prev = (head)->tqh_last; \
486 *(head)->tqh_last = (elm); \
487 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
488 QMD_TRACE_HEAD(head); \
489 QMD_TRACE_ELEM(&(elm)->field); \
490 } while (0)
492 #define TAILQ_LAST(head, headname) \
493 (*(((struct headname *)((head)->tqh_last))->tqh_last))
495 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
497 #define TAILQ_PREV(elm, headname, field) \
498 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
500 #define TAILQ_REMOVE(head, elm, field) do { \
501 if ((TAILQ_NEXT((elm), field)) != NULL) \
502 TAILQ_NEXT((elm), field)->field.tqe_prev = \
503 (elm)->field.tqe_prev; \
504 else { \
505 (head)->tqh_last = (elm)->field.tqe_prev; \
506 QMD_TRACE_HEAD(head); \
508 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
509 TRASHIT((elm)->field.tqe_next); \
510 TRASHIT((elm)->field.tqe_prev); \
511 QMD_TRACE_ELEM(&(elm)->field); \
512 } while (0)
515 #ifdef _KERNEL
518 * XXX insque() and remque() are an old way of handling certain queues.
519 * They bogusly assumes that all queue heads look alike.
522 struct quehead {
523 struct quehead *qh_link;
524 struct quehead *qh_rlink;
527 #if defined(__GNUC__) || defined(__INTEL_COMPILER)
529 static __inline void
530 insque(void *a, void *b)
532 struct quehead *element = (struct quehead *)a,
533 *head = (struct quehead *)b;
535 element->qh_link = head->qh_link;
536 element->qh_rlink = head;
537 head->qh_link = element;
538 element->qh_link->qh_rlink = element;
541 static __inline void
542 remque(void *a)
544 struct quehead *element = (struct quehead *)a;
546 element->qh_link->qh_rlink = element->qh_rlink;
547 element->qh_rlink->qh_link = element->qh_link;
548 element->qh_rlink = 0;
551 #else /* !(__GNUC__ || __INTEL_COMPILER) */
553 void insque(void *a, void *b);
554 void remque(void *a);
556 #endif /* __GNUC__ || __INTEL_COMPILER */
558 #endif /* _KERNEL */
560 #endif /* !_SYS_QUEUE_H_ */