blob: 73200429eaf77516184b1179a3533efede432dca [file] [log] [blame]
Prakash Dhavali7090c5f2015-11-02 17:55:19 -08001/*
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
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.
16 *
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.
28 *
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 */
32
33#if !defined(__NetBSD__)
34#ifndef _SYS_QUEUE_H_
35#define _SYS_QUEUE_H_
36
37/*
38 * This file defines four types of data structures: singly-linked lists,
39 * singly-linked tail queues, lists and tail queues.
40 *
41 * A singly-linked list is headed by a single forward pointer. The elements
42 * are singly linked for minimum space and pointer manipulation overhead at
43 * the expense of O(n) removal for arbitrary elements. New elements can be
44 * added to the list after an existing element or at the head of the list.
45 * Elements being removed from the head of the list should use the explicit
46 * macro for this purpose for optimum efficiency. A singly-linked list may
47 * only be traversed in the forward direction. Singly-linked lists are ideal
48 * for applications with large datasets and few or no removals or for
49 * implementing a LIFO queue.
50 *
51 * A singly-linked tail queue is headed by a pair of pointers, one to the
52 * head of the list and the other to the tail of the list. The elements are
53 * singly linked for minimum space and pointer manipulation overhead at the
54 * expense of O(n) removal for arbitrary elements. New elements can be added
55 * to the list after an existing element, at the head of the list, or at the
56 * end of the list. Elements being removed from the head of the tail queue
57 * should use the explicit macro for this purpose for optimum efficiency.
58 * A singly-linked tail queue may only be traversed in the forward direction.
59 * Singly-linked tail queues are ideal for applications with large datasets
60 * and few or no removals or for implementing a FIFO queue.
61 *
62 * A list is headed by a single forward pointer (or an array of forward
63 * pointers for a hash table header). The elements are doubly linked
64 * so that an arbitrary element can be removed without a need to
65 * traverse the list. New elements can be added to the list before
66 * or after an existing element or at the head of the list. A list
67 * may only be traversed in the forward direction.
68 *
69 * A tail queue is headed by a pair of pointers, one to the head of the
70 * list and the other to the tail of the list. The elements are doubly
71 * linked so that an arbitrary element can be removed without a need to
72 * traverse the list. New elements can be added to the list before or
73 * after an existing element, at the head of the list, or at the end of
74 * the list. A tail queue may be traversed in either direction.
75 *
76 * For details on the use of these macros, see the queue(3) manual page.
77 *
78 *
79 * SLIST LIST STAILQ TAILQ
80 * _HEAD + + + +
81 * _HEAD_INITIALIZER + + + +
82 * _ENTRY + + + +
83 * _INIT + + + +
84 * _EMPTY + + + +
85 * _FIRST + + + +
86 * _NEXT + + + +
87 * _PREV - - - +
88 * _LAST - - + +
89 * _FOREACH + + + +
90 * _FOREACH_SAFE + + + +
91 * _FOREACH_REVERSE - - - +
92 * _FOREACH_REVERSE_SAFE - - - +
93 * _INSERT_HEAD + + + +
94 * _INSERT_BEFORE - + - +
95 * _INSERT_AFTER + + + +
96 * _INSERT_TAIL - - + +
97 * _CONCAT - - + +
98 * _REMOVE_HEAD + - + -
99 * _REMOVE + + + +
100 *
101 */
102#define QUEUE_MACRO_DEBUG 0
103#if QUEUE_MACRO_DEBUG
104/* Store the last 2 places the queue element or head was altered */
105struct qm_trace {
106 char *lastfile;
107 int lastline;
108 char *prevfile;
109 int prevline;
110};
111
112#define TRACEBUF struct qm_trace trace;
113#define TRASHIT(x) do {(x) = (void *)NULL; } while (0)
114
115#define QMD_TRACE_HEAD(head) do { \
116 (head)->trace.prevline = (head)->trace.lastline; \
117 (head)->trace.prevfile = (head)->trace.lastfile; \
118 (head)->trace.lastline = __LINE__; \
119 (head)->trace.lastfile = __FILE__; \
120} while (0)
121
122#define QMD_TRACE_ELEM(elem) do { \
123 (elem)->trace.prevline = (elem)->trace.lastline; \
124 (elem)->trace.prevfile = (elem)->trace.lastfile; \
125 (elem)->trace.lastline = __LINE__; \
126 (elem)->trace.lastfile = __FILE__; \
127} while (0)
128
129#else
130#define QMD_TRACE_ELEM(elem)
131#define QMD_TRACE_HEAD(head)
132#define TRACEBUF
133#define TRASHIT(x) do {(x) = (void *)0; } while (0)
134#endif /* QUEUE_MACRO_DEBUG */
135
136#ifdef ATHR_RNWF
137/* NDIS contains a defn for SLIST_ENTRY and SINGLE_LIST_ENTRY */
138#endif
139
140/*
141 * Singly-linked List declarations.
142 */
143#define SLIST_HEAD(name, type) \
144 struct name { \
145 struct type *slh_first; /* first element */ \
146 }
147
148#define SLIST_HEAD_INITIALIZER(head) \
149 { NULL }
150
151#define SING_LIST_ENTRY(type) \
152 struct { \
153 struct type *sle_next; /* next element */ \
154 }
155
156/*
157 * Singly-linked List functions.
158 */
159#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
160
161#define SLIST_FIRST(head) ((head)->slh_first)
162
163#define SLIST_FOREACH(var, head, field) \
164 for ((var) = SLIST_FIRST((head)); \
165 (var); \
166 (var) = SLIST_NEXT((var), field))
167
168#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
169 for ((var) = SLIST_FIRST((head)); \
170 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
171 (var) = (tvar))
172
173#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
174 for ((varp) = &SLIST_FIRST((head)); \
175 ((var) = *(varp)) != NULL; \
176 (varp) = &SLIST_NEXT((var), field))
177
178#define SLIST_INIT(head) do { \
179 SLIST_FIRST((head)) = NULL; \
180} while (0)
181
182#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
183 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
184 SLIST_NEXT((slistelm), field) = (elm); \
185} while (0)
186
187#define SLIST_INSERT_HEAD(head, elm, field) do { \
188 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
189 SLIST_FIRST((head)) = (elm); \
190} while (0)
191
192#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
193
194#define SLIST_REMOVE(head, elm, type, field) do { \
195 if (SLIST_FIRST((head)) == (elm)) { \
196 SLIST_REMOVE_HEAD((head), field); \
197 } \
198 else { \
199 struct type *curelm = SLIST_FIRST((head)); \
200 while (SLIST_NEXT(curelm, field) != (elm)) \
201 curelm = SLIST_NEXT(curelm, field); \
202 SLIST_NEXT(curelm, field) = \
203 SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
204 } \
205} while (0)
206
207#define SLIST_REMOVE_HEAD(head, field) do { \
208 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
209} while (0)
210
211/*
212 * Singly-linked Tail queue declarations.
213 */
214#define STAILQ_HEAD(name, type) \
215 struct name { \
216 struct type *stqh_first; /* first element */ \
217 struct type **stqh_last; /* addr of last next element */ \
218 }
219
220#define STAILQ_HEAD_INITIALIZER(head) \
221 { NULL, &(head).stqh_first }
222
223#define STAILQ_ENTRY(type) \
224 struct { \
225 struct type *stqe_next; /* next element */ \
226 }
227
228/*
229 * Singly-linked Tail queue functions.
230 */
231#define STAILQ_CONCAT(head1, head2) do { \
232 if (!STAILQ_EMPTY((head2))) { \
233 *(head1)->stqh_last = (head2)->stqh_first; \
234 (head1)->stqh_last = (head2)->stqh_last; \
235 STAILQ_INIT((head2)); \
236 } \
237} while (0)
238
239#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
240
241#define STAILQ_FIRST(head) ((head)->stqh_first)
242
243#define STAILQ_FOREACH(var, head, field) \
244 for((var) = STAILQ_FIRST((head)); \
245 (var); \
246 (var) = STAILQ_NEXT((var), field))
247
248#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
249 for ((var) = STAILQ_FIRST((head)); \
250 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
251 (var) = (tvar))
252
253#define STAILQ_INIT(head) do { \
254 STAILQ_FIRST((head)) = NULL; \
255 (head)->stqh_last = &STAILQ_FIRST((head)); \
256} while (0)
257
258#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
259 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL) \
260 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
261 STAILQ_NEXT((tqelm), field) = (elm); \
262} while (0)
263
264#define STAILQ_INSERT_HEAD(head, elm, field) do { \
265 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
266 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
267 STAILQ_FIRST((head)) = (elm); \
268} while (0)
269
270#define STAILQ_INSERT_TAIL(head, elm, field) do { \
271 STAILQ_NEXT((elm), field) = NULL; \
272 *(head)->stqh_last = (elm); \
273 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
274} while (0)
275
276#define STAILQ_LAST(head, type, field) \
277 (STAILQ_EMPTY((head)) ? \
278 NULL : \
279 ((struct type *) \
280 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
281
282#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
283
284#define STAILQ_REMOVE(head, elm, type, field) do { \
285 if (STAILQ_FIRST((head)) == (elm)) { \
286 STAILQ_REMOVE_HEAD((head), field); \
287 } \
288 else { \
289 struct type *curelm = STAILQ_FIRST((head)); \
290 while (STAILQ_NEXT(curelm, field) != (elm)) \
291 curelm = STAILQ_NEXT(curelm, field); \
292 if ((STAILQ_NEXT(curelm, field) = \
293 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL) \
294 (head)->stqh_last = &STAILQ_NEXT((curelm), field); \
295 } \
296} while (0)
297
298#define STAILQ_REMOVE_AFTER(head, elm, field) do { \
299 if (STAILQ_NEXT(elm, field)) { \
300 if ((STAILQ_NEXT(elm, field) = \
301 STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
302 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
303 } \
304} while (0)
305
306#define STAILQ_REMOVE_HEAD(head, field) do { \
307 if ((STAILQ_FIRST((head)) = \
308 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
309 (head)->stqh_last = &STAILQ_FIRST((head)); \
310} while (0)
311
312#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
313 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
314 (head)->stqh_last = &STAILQ_FIRST((head)); \
315} while (0)
316
317/*
318 * List declarations.
319 */
320#define ATH_LIST_HEAD(name, type) \
321 struct name { \
322 struct type *lh_first; /* first element */ \
323 }
324
325#ifndef LIST_HEAD
326#define LIST_HEAD ATH_LIST_HEAD
327#endif
328
329#define LIST_HEAD_INITIALIZER(head) \
330 { NULL }
331
332#define LIST_ENTRY(type) \
333 struct { \
334 struct type *le_next; /* next element */ \
335 struct type **le_prev; /* address of previous next element */ \
336 }
337
338/*
339 * List functions.
340 */
341
342#define LIST_EMPTY(head) ((head)->lh_first == NULL)
343
344#define LIST_FIRST(head) ((head)->lh_first)
345
346#define LIST_FOREACH(var, head, field) \
347 for ((var) = LIST_FIRST((head)); \
348 (var); \
349 (var) = LIST_NEXT((var), field))
350
351#define LIST_FOREACH_SAFE(var, head, field, tvar) \
352 for ((var) = LIST_FIRST((head)); \
353 (var) && ((tvar) = LIST_NEXT((var), field), 1); \
354 (var) = (tvar))
355
356#define LIST_INIT(head) do { \
357 LIST_FIRST((head)) = NULL; \
358} while (0)
359
360#define LIST_INSERT_AFTER(listelm, elm, field) do { \
361 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL) \
362 LIST_NEXT((listelm), field)->field.le_prev = \
363 &LIST_NEXT((elm), field); \
364 LIST_NEXT((listelm), field) = (elm); \
365 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
366} while (0)
367
368#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
369 (elm)->field.le_prev = (listelm)->field.le_prev; \
370 LIST_NEXT((elm), field) = (listelm); \
371 *(listelm)->field.le_prev = (elm); \
372 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
373} while (0)
374
375#define LIST_INSERT_HEAD(head, elm, field) do { \
376 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
377 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field); \
378 LIST_FIRST((head)) = (elm); \
379 (elm)->field.le_prev = &LIST_FIRST((head)); \
380} while (0)
381
382#define LIST_NEXT(elm, field) ((elm)->field.le_next)
383
384#define LIST_REMOVE(elm, field) do { \
385 if (LIST_NEXT((elm), field) != NULL) \
386 LIST_NEXT((elm), field)->field.le_prev = \
387 (elm)->field.le_prev; \
388 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
389} while (0)
390
391/*
392 * Tail queue declarations.
393 */
394#define HEADNAME
395#define COPY_HEADNAME(head)
396
397#define TAILQ_HEAD(name, type) \
398 struct name { \
399 struct type *tqh_first; /* first element */ \
400 struct type **tqh_last; /* addr of last next element */ \
401 HEADNAME \
402 TRACEBUF \
403 }
404
405#define TAILQ_HEAD_INITIALIZER(head) \
406 { NULL, &(head).tqh_first }
407
408#define TAILQ_ENTRY(type) \
409 struct { \
410 struct type *tqe_next; /* next element */ \
411 struct type **tqe_prev; /* address of previous next element */ \
412 TRACEBUF \
413 }
414
415/*
416 * Tail queue functions.
417 */
418
419#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
420
421#define TAILQ_FIRST(head) ((head)->tqh_first)
422
423#define TAILQ_FOREACH(var, head, field) \
424 for ((var) = TAILQ_FIRST((head)); \
425 (var); \
426 (var) = TAILQ_NEXT((var), field))
427
428#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
429 for ((var) = TAILQ_FIRST((head)); \
430 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
431 (var) = (tvar))
432
433#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
434 for ((var) = TAILQ_LAST((head), headname); \
435 (var); \
436 (var) = TAILQ_PREV((var), headname, field))
437
438#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
439 for ((var) = TAILQ_LAST((head), headname); \
440 (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
441 (var) = (tvar))
442
443#define TAILQ_INIT(head) do { \
444 TAILQ_FIRST((head)) = NULL; \
445 (head)->tqh_last = &TAILQ_FIRST((head)); \
446 COPY_HEADNAME(head); \
447 QMD_TRACE_HEAD(head); \
448} while (0)
449
450#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
451 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL) \
452 TAILQ_NEXT((elm), field)->field.tqe_prev = \
453 &TAILQ_NEXT((elm), field); \
454 else { \
455 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
456 QMD_TRACE_HEAD(head); \
457 } \
458 TAILQ_NEXT((listelm), field) = (elm); \
459 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
460 QMD_TRACE_ELEM(&(elm)->field); \
461 QMD_TRACE_ELEM(&listelm->field); \
462} while (0)
463
464#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
465 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
466 TAILQ_NEXT((elm), field) = (listelm); \
467 *(listelm)->field.tqe_prev = (elm); \
468 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
469 QMD_TRACE_ELEM(&(elm)->field); \
470 QMD_TRACE_ELEM(&listelm->field); \
471} while (0)
472
473#define TAILQ_INSERT_HEAD(head, elm, field) do { \
474 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
475 TAILQ_FIRST((head))->field.tqe_prev = \
476 &TAILQ_NEXT((elm), field); \
477 else \
478 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
479 TAILQ_FIRST((head)) = (elm); \
480 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
481 QMD_TRACE_HEAD(head); \
482 QMD_TRACE_ELEM(&(elm)->field); \
483} while (0)
484
485#define TAILQ_INSERT_TAIL(head, elm, field) do { \
486 TAILQ_NEXT((elm), field) = NULL; \
487 (elm)->field.tqe_prev = (head)->tqh_last; \
488 *(head)->tqh_last = (elm); \
489 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
490 QMD_TRACE_HEAD(head); \
491 QMD_TRACE_ELEM(&(elm)->field); \
492} while (0)
493
494#define TAILQ_LAST(head, headname) \
495 (*(((struct headname *)((head)->tqh_last))->tqh_last))
496
497#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
498
499#define TAILQ_PREV(elm, headname, field) \
500 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
501
502#define TAILQ_REMOVE(head, elm, field) do { \
503 if ((TAILQ_NEXT((elm), field)) != NULL) \
504 TAILQ_NEXT((elm), field)->field.tqe_prev = \
505 (elm)->field.tqe_prev; \
506 else { \
507 (head)->tqh_last = (elm)->field.tqe_prev; \
508 QMD_TRACE_HEAD(head); \
509 } \
510 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
511 TRASHIT((elm)->field.tqe_next); \
512 TRASHIT((elm)->field.tqe_prev); \
513 QMD_TRACE_ELEM(&(elm)->field); \
514} while (0)
515
516#define TAILQ_CONCAT(head1, head2, field) do { \
517 if (!TAILQ_EMPTY(head2)) { \
518 *(head1)->tqh_last = (head2)->tqh_first; \
519 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
520 (head1)->tqh_last = (head2)->tqh_last; \
521 TAILQ_INIT((head2)); \
522 } \
523} while (0)
524
525#ifdef _KERNEL
526
527/*
528 * XXX insque() and remque() are an old way of handling certain queues.
529 * They bogusly assumes that all queue heads look alike.
530 */
531
532struct quehead {
533 struct quehead *qh_link;
534 struct quehead *qh_rlink;
535};
536
537#if defined(__GNUC__) || defined(__INTEL_COMPILER)
538
539static __inline void insque(void *a, void *b)
540{
541 struct quehead *element = (struct quehead *)a,
542 *head = (struct quehead *)b;
543
544 element->qh_link = head->qh_link;
545 element->qh_rlink = head;
546 head->qh_link = element;
547 element->qh_link->qh_rlink = element;
548}
549
550static __inline void remque(void *a)
551{
552 struct quehead *element = (struct quehead *)a;
553
554 element->qh_link->qh_rlink = element->qh_rlink;
555 element->qh_rlink->qh_link = element->qh_link;
556 element->qh_rlink = 0;
557}
558
559#else /* !(__GNUC__ || __INTEL_COMPILER) */
560
561void insque(void *a, void *b);
562void remque(void *a);
563
564#endif /* __GNUC__ || __INTEL_COMPILER */
565
566#endif /* _KERNEL */
567
568#endif /* !_SYS_QUEUE_H_ */
569#else /* !__NetBSD__ */
570#include_next <sys/queue.h>
571#endif /* __NetBSD__ */