source: branches/libc-0.6/src/emx/include/sys/queue.h

Last change on this file was 958, checked in by bird, 22 years ago

From FreeBSD 5.1

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 16.6 KB
Line 
1/*
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)queue.h 8.5 (Berkeley) 8/20/94
34 * $FreeBSD: src/sys/sys/queue.h,v 1.54 2002/08/05 05:18:43 alfred Exp $
35 */
36
37/** @file
38 * FreeBSD 5.1
39 */
40
41#ifndef _SYS_QUEUE_H_
42#define _SYS_QUEUE_H_
43
44#include <sys/cdefs.h>
45
46/*
47 * This file defines four types of data structures: singly-linked lists,
48 * singly-linked tail queues, lists and tail queues.
49 *
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.
59 *
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.
70 *
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.
77 *
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.
84 *
85 * For details on the use of these macros, see the queue(3) manual page.
86 *
87 *
88 * SLIST LIST STAILQ TAILQ
89 * _HEAD + + + +
90 * _HEAD_INITIALIZER + + + +
91 * _ENTRY + + + +
92 * _INIT + + + +
93 * _EMPTY + + + +
94 * _FIRST + + + +
95 * _NEXT + + + +
96 * _PREV - - - +
97 * _LAST - - + +
98 * _FOREACH + + + +
99 * _FOREACH_REVERSE - - - +
100 * _INSERT_HEAD + + + +
101 * _INSERT_BEFORE - + - +
102 * _INSERT_AFTER + + + +
103 * _INSERT_TAIL - - + +
104 * _CONCAT - - + +
105 * _REMOVE_HEAD + - + -
106 * _REMOVE + + + +
107 *
108 */
109#define QUEUE_MACRO_DEBUG 0
110#if QUEUE_MACRO_DEBUG
111/* Store the last 2 places the queue element or head was altered */
112struct qm_trace {
113 char * lastfile;
114 int lastline;
115 char * prevfile;
116 int prevline;
117};
118
119#define TRACEBUF struct qm_trace trace;
120#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
121
122#define QMD_TRACE_HEAD(head) do { \
123 (head)->trace.prevline = (head)->trace.lastline; \
124 (head)->trace.prevfile = (head)->trace.lastfile; \
125 (head)->trace.lastline = __LINE__; \
126 (head)->trace.lastfile = __FILE__; \
127} while (0)
128
129#define QMD_TRACE_ELEM(elem) do { \
130 (elem)->trace.prevline = (elem)->trace.lastline; \
131 (elem)->trace.prevfile = (elem)->trace.lastfile; \
132 (elem)->trace.lastline = __LINE__; \
133 (elem)->trace.lastfile = __FILE__; \
134} while (0)
135
136#else
137#define QMD_TRACE_ELEM(elem)
138#define QMD_TRACE_HEAD(head)
139#define TRACEBUF
140#define TRASHIT(x)
141#endif /* QUEUE_MACRO_DEBUG */
142
143/*
144 * Singly-linked List declarations.
145 */
146#define SLIST_HEAD(name, type) \
147struct name { \
148 struct type *slh_first; /* first element */ \
149}
150
151#define SLIST_HEAD_INITIALIZER(head) \
152 { NULL }
153
154#define SLIST_ENTRY(type) \
155struct { \
156 struct type *sle_next; /* next element */ \
157}
158
159/*
160 * Singly-linked List functions.
161 */
162#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
163
164#define SLIST_FIRST(head) ((head)->slh_first)
165
166#define SLIST_FOREACH(var, head, field) \
167 for ((var) = SLIST_FIRST((head)); \
168 (var); \
169 (var) = SLIST_NEXT((var), field))
170
171#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
172 for ((varp) = &SLIST_FIRST((head)); \
173 ((var) = *(varp)) != NULL; \
174 (varp) = &SLIST_NEXT((var), field))
175
176#define SLIST_INIT(head) do { \
177 SLIST_FIRST((head)) = NULL; \
178} while (0)
179
180#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
181 SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
182 SLIST_NEXT((slistelm), field) = (elm); \
183} while (0)
184
185#define SLIST_INSERT_HEAD(head, elm, field) do { \
186 SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
187 SLIST_FIRST((head)) = (elm); \
188} while (0)
189
190#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
191
192#define SLIST_REMOVE(head, elm, type, field) do { \
193 if (SLIST_FIRST((head)) == (elm)) { \
194 SLIST_REMOVE_HEAD((head), field); \
195 } \
196 else { \
197 struct type *curelm = SLIST_FIRST((head)); \
198 while (SLIST_NEXT(curelm, field) != (elm)) \
199 curelm = SLIST_NEXT(curelm, field); \
200 SLIST_NEXT(curelm, field) = \
201 SLIST_NEXT(SLIST_NEXT(curelm, field), field); \
202 } \
203} while (0)
204
205#define SLIST_REMOVE_HEAD(head, field) do { \
206 SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
207} while (0)
208
209/*
210 * Singly-linked Tail queue declarations.
211 */
212#define STAILQ_HEAD(name, type) \
213struct name { \
214 struct type *stqh_first;/* first element */ \
215 struct type **stqh_last;/* addr of last next element */ \
216}
217
218#define STAILQ_HEAD_INITIALIZER(head) \
219 { NULL, &(head).stqh_first }
220
221#define STAILQ_ENTRY(type) \
222struct { \
223 struct type *stqe_next; /* next element */ \
224}
225
226/*
227 * Singly-linked Tail queue functions.
228 */
229#define STAILQ_CONCAT(head1, head2) do { \
230 if (!STAILQ_EMPTY((head2))) { \
231 *(head1)->stqh_last = (head2)->stqh_first; \
232 (head1)->stqh_last = (head2)->stqh_last; \
233 STAILQ_INIT((head2)); \
234 } \
235} while (0)
236
237#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
238
239#define STAILQ_FIRST(head) ((head)->stqh_first)
240
241#define STAILQ_FOREACH(var, head, field) \
242 for((var) = STAILQ_FIRST((head)); \
243 (var); \
244 (var) = STAILQ_NEXT((var), field))
245
246#define STAILQ_INIT(head) do { \
247 STAILQ_FIRST((head)) = NULL; \
248 (head)->stqh_last = &STAILQ_FIRST((head)); \
249} while (0)
250
251#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
252 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
253 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
254 STAILQ_NEXT((tqelm), field) = (elm); \
255} while (0)
256
257#define STAILQ_INSERT_HEAD(head, elm, field) do { \
258 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
259 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
260 STAILQ_FIRST((head)) = (elm); \
261} while (0)
262
263#define STAILQ_INSERT_TAIL(head, elm, field) do { \
264 STAILQ_NEXT((elm), field) = NULL; \
265 *(head)->stqh_last = (elm); \
266 (head)->stqh_last = &STAILQ_NEXT((elm), field); \
267} while (0)
268
269#define STAILQ_LAST(head, type, field) \
270 (STAILQ_EMPTY((head)) ? \
271 NULL : \
272 ((struct type *) \
273 ((char *)((head)->stqh_last) - __offsetof(struct type, field))))
274
275#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
276
277#define STAILQ_REMOVE(head, elm, type, field) do { \
278 if (STAILQ_FIRST((head)) == (elm)) { \
279 STAILQ_REMOVE_HEAD((head), field); \
280 } \
281 else { \
282 struct type *curelm = STAILQ_FIRST((head)); \
283 while (STAILQ_NEXT(curelm, field) != (elm)) \
284 curelm = STAILQ_NEXT(curelm, field); \
285 if ((STAILQ_NEXT(curelm, field) = \
286 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\
287 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\
288 } \
289} while (0)
290
291#define STAILQ_REMOVE_HEAD(head, field) do { \
292 if ((STAILQ_FIRST((head)) = \
293 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
294 (head)->stqh_last = &STAILQ_FIRST((head)); \
295} while (0)
296
297#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
298 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \
299 (head)->stqh_last = &STAILQ_FIRST((head)); \
300} while (0)
301
302/*
303 * List declarations.
304 */
305#define LIST_HEAD(name, type) \
306struct name { \
307 struct type *lh_first; /* first element */ \
308}
309
310#define LIST_HEAD_INITIALIZER(head) \
311 { NULL }
312
313#define LIST_ENTRY(type) \
314struct { \
315 struct type *le_next; /* next element */ \
316 struct type **le_prev; /* address of previous next element */ \
317}
318
319/*
320 * List functions.
321 */
322
323#define LIST_EMPTY(head) ((head)->lh_first == NULL)
324
325#define LIST_FIRST(head) ((head)->lh_first)
326
327#define LIST_FOREACH(var, head, field) \
328 for ((var) = LIST_FIRST((head)); \
329 (var); \
330 (var) = LIST_NEXT((var), field))
331
332#define LIST_INIT(head) do { \
333 LIST_FIRST((head)) = NULL; \
334} while (0)
335
336#define LIST_INSERT_AFTER(listelm, elm, field) do { \
337 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
338 LIST_NEXT((listelm), field)->field.le_prev = \
339 &LIST_NEXT((elm), field); \
340 LIST_NEXT((listelm), field) = (elm); \
341 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \
342} while (0)
343
344#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
345 (elm)->field.le_prev = (listelm)->field.le_prev; \
346 LIST_NEXT((elm), field) = (listelm); \
347 *(listelm)->field.le_prev = (elm); \
348 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \
349} while (0)
350
351#define LIST_INSERT_HEAD(head, elm, field) do { \
352 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
353 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
354 LIST_FIRST((head)) = (elm); \
355 (elm)->field.le_prev = &LIST_FIRST((head)); \
356} while (0)
357
358#define LIST_NEXT(elm, field) ((elm)->field.le_next)
359
360#define LIST_REMOVE(elm, field) do { \
361 if (LIST_NEXT((elm), field) != NULL) \
362 LIST_NEXT((elm), field)->field.le_prev = \
363 (elm)->field.le_prev; \
364 *(elm)->field.le_prev = LIST_NEXT((elm), field); \
365} while (0)
366
367/*
368 * Tail queue declarations.
369 */
370#define TAILQ_HEAD(name, type) \
371struct name { \
372 struct type *tqh_first; /* first element */ \
373 struct type **tqh_last; /* addr of last next element */ \
374 TRACEBUF \
375}
376
377#define TAILQ_HEAD_INITIALIZER(head) \
378 { NULL, &(head).tqh_first }
379
380#define TAILQ_ENTRY(type) \
381struct { \
382 struct type *tqe_next; /* next element */ \
383 struct type **tqe_prev; /* address of previous next element */ \
384 TRACEBUF \
385}
386
387/*
388 * Tail queue functions.
389 */
390#define TAILQ_CONCAT(head1, head2, field) do { \
391 if (!TAILQ_EMPTY(head2)) { \
392 *(head1)->tqh_last = (head2)->tqh_first; \
393 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
394 (head1)->tqh_last = (head2)->tqh_last; \
395 TAILQ_INIT((head2)); \
396 QMD_TRACE_HEAD(head); \
397 QMD_TRACE_HEAD(head2); \
398 } \
399} while (0)
400
401#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
402
403#define TAILQ_FIRST(head) ((head)->tqh_first)
404
405#define TAILQ_FOREACH(var, head, field) \
406 for ((var) = TAILQ_FIRST((head)); \
407 (var); \
408 (var) = TAILQ_NEXT((var), field))
409
410#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
411 for ((var) = TAILQ_LAST((head), headname); \
412 (var); \
413 (var) = TAILQ_PREV((var), headname, field))
414
415#define TAILQ_INIT(head) do { \
416 TAILQ_FIRST((head)) = NULL; \
417 (head)->tqh_last = &TAILQ_FIRST((head)); \
418 QMD_TRACE_HEAD(head); \
419} while (0)
420
421#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
422 if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
423 TAILQ_NEXT((elm), field)->field.tqe_prev = \
424 &TAILQ_NEXT((elm), field); \
425 else { \
426 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
427 QMD_TRACE_HEAD(head); \
428 } \
429 TAILQ_NEXT((listelm), field) = (elm); \
430 (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
431 QMD_TRACE_ELEM(&(elm)->field); \
432 QMD_TRACE_ELEM(&listelm->field); \
433} while (0)
434
435#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
436 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
437 TAILQ_NEXT((elm), field) = (listelm); \
438 *(listelm)->field.tqe_prev = (elm); \
439 (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
440 QMD_TRACE_ELEM(&(elm)->field); \
441 QMD_TRACE_ELEM(&listelm->field); \
442} while (0)
443
444#define TAILQ_INSERT_HEAD(head, elm, field) do { \
445 if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
446 TAILQ_FIRST((head))->field.tqe_prev = \
447 &TAILQ_NEXT((elm), field); \
448 else \
449 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
450 TAILQ_FIRST((head)) = (elm); \
451 (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
452 QMD_TRACE_HEAD(head); \
453 QMD_TRACE_ELEM(&(elm)->field); \
454} while (0)
455
456#define TAILQ_INSERT_TAIL(head, elm, field) do { \
457 TAILQ_NEXT((elm), field) = NULL; \
458 (elm)->field.tqe_prev = (head)->tqh_last; \
459 *(head)->tqh_last = (elm); \
460 (head)->tqh_last = &TAILQ_NEXT((elm), field); \
461 QMD_TRACE_HEAD(head); \
462 QMD_TRACE_ELEM(&(elm)->field); \
463} while (0)
464
465#define TAILQ_LAST(head, headname) \
466 (*(((struct headname *)((head)->tqh_last))->tqh_last))
467
468#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
469
470#define TAILQ_PREV(elm, headname, field) \
471 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
472
473#define TAILQ_REMOVE(head, elm, field) do { \
474 if ((TAILQ_NEXT((elm), field)) != NULL) \
475 TAILQ_NEXT((elm), field)->field.tqe_prev = \
476 (elm)->field.tqe_prev; \
477 else { \
478 (head)->tqh_last = (elm)->field.tqe_prev; \
479 QMD_TRACE_HEAD(head); \
480 } \
481 *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
482 TRASHIT((elm)->field.tqe_next); \
483 TRASHIT((elm)->field.tqe_prev); \
484 QMD_TRACE_ELEM(&(elm)->field); \
485} while (0)
486
487
488#ifdef _KERNEL
489
490/*
491 * XXX insque() and remque() are an old way of handling certain queues.
492 * They bogusly assumes that all queue heads look alike.
493 */
494
495struct quehead {
496 struct quehead *qh_link;
497 struct quehead *qh_rlink;
498};
499
500#ifdef __GNUC__
501
502static __inline void
503insque(void *a, void *b)
504{
505 struct quehead *element = (struct quehead *)a,
506 *head = (struct quehead *)b;
507
508 element->qh_link = head->qh_link;
509 element->qh_rlink = head;
510 head->qh_link = element;
511 element->qh_link->qh_rlink = element;
512}
513
514static __inline void
515remque(void *a)
516{
517 struct quehead *element = (struct quehead *)a;
518
519 element->qh_link->qh_rlink = element->qh_rlink;
520 element->qh_rlink->qh_link = element->qh_link;
521 element->qh_rlink = 0;
522}
523
524#else /* !__GNUC__ */
525
526void insque(void *a, void *b);
527void remque(void *a);
528
529#endif /* __GNUC__ */
530
531#endif /* _KERNEL */
532
533#endif /* !_SYS_QUEUE_H_ */
Note: See TracBrowser for help on using the repository browser.