comparison compat/queue.h @ 17:a08ef0674d8e

Page long output in interactive mode
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Sat, 12 Aug 2017 10:41:52 +0200
parents
children
comparison
equal deleted inserted replaced
16:a07665727c19 17:a08ef0674d8e
1 /* $OpenBSD: queue.h,v 1.44 2016/09/09 20:31:46 millert Exp $ */
2 /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
3
4 /*
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)queue.h 8.5 (Berkeley) 8/20/94
33 */
34
35 #ifndef _SYS_QUEUE_H_
36 #define _SYS_QUEUE_H_
37
38 #include <stddef.h>
39
40 /*
41 * This file defines five types of data structures: singly-linked lists,
42 * lists, simple queues, tail queues and XOR simple queues.
43 *
44 *
45 * A singly-linked list is headed by a single forward pointer. The elements
46 * are singly linked for minimum space and pointer manipulation overhead at
47 * the expense of O(n) removal for arbitrary elements. New elements can be
48 * added to the list after an existing element or at the head of the list.
49 * Elements being removed from the head of the list should use the explicit
50 * macro for this purpose for optimum efficiency. A singly-linked list may
51 * only be traversed in the forward direction. Singly-linked lists are ideal
52 * for applications with large datasets and few or no removals or for
53 * implementing a LIFO queue.
54 *
55 * A list is headed by a single forward pointer (or an array of forward
56 * pointers for a hash table header). The elements are doubly linked
57 * so that an arbitrary element can be removed without a need to
58 * traverse the list. New elements can be added to the list before
59 * or after an existing element or at the head of the list. A list
60 * may only be traversed in the forward direction.
61 *
62 * A simple queue is headed by a pair of pointers, one to the head of the
63 * list and the other to the tail of the list. The elements are singly
64 * linked to save space, so elements can only be removed from the
65 * head of the list. New elements can be added to the list before or after
66 * an existing element, at the head of the list, or at the end of the
67 * list. A simple queue 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 * An XOR simple queue is used in the same way as a regular simple queue.
77 * The difference is that the head structure also includes a "cookie" that
78 * is XOR'd with the queue pointer (first, last or next) to generate the
79 * real pointer value.
80 *
81 * For details on the use of these macros, see the queue(3) manual page.
82 */
83
84 #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
85 #define _Q_INVALIDATE(a) (a) = ((void *)-1)
86 #else
87 #define _Q_INVALIDATE(a)
88 #endif
89
90 /*
91 * Singly-linked List definitions.
92 */
93 #define SLIST_HEAD(name, type) \
94 struct name { \
95 struct type *slh_first; /* first element */ \
96 }
97
98 #define SLIST_HEAD_INITIALIZER(head) \
99 { NULL }
100
101 #define SLIST_ENTRY(type) \
102 struct { \
103 struct type *sle_next; /* next element */ \
104 }
105
106 /*
107 * Singly-linked List access methods.
108 */
109 #define SLIST_FIRST(head) ((head)->slh_first)
110 #define SLIST_END(head) NULL
111 #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
112 #define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
113
114 #define SLIST_FOREACH(var, head, field) \
115 for((var) = SLIST_FIRST(head); \
116 (var) != SLIST_END(head); \
117 (var) = SLIST_NEXT(var, field))
118
119 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
120 for ((var) = SLIST_FIRST(head); \
121 (var) && ((tvar) = SLIST_NEXT(var, field), 1); \
122 (var) = (tvar))
123
124 /*
125 * Singly-linked List functions.
126 */
127 #define SLIST_INIT(head) { \
128 SLIST_FIRST(head) = SLIST_END(head); \
129 }
130
131 #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
132 (elm)->field.sle_next = (slistelm)->field.sle_next; \
133 (slistelm)->field.sle_next = (elm); \
134 } while (0)
135
136 #define SLIST_INSERT_HEAD(head, elm, field) do { \
137 (elm)->field.sle_next = (head)->slh_first; \
138 (head)->slh_first = (elm); \
139 } while (0)
140
141 #define SLIST_REMOVE_AFTER(elm, field) do { \
142 (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
143 } while (0)
144
145 #define SLIST_REMOVE_HEAD(head, field) do { \
146 (head)->slh_first = (head)->slh_first->field.sle_next; \
147 } while (0)
148
149 #define SLIST_REMOVE(head, elm, type, field) do { \
150 if ((head)->slh_first == (elm)) { \
151 SLIST_REMOVE_HEAD((head), field); \
152 } else { \
153 struct type *curelm = (head)->slh_first; \
154 \
155 while (curelm->field.sle_next != (elm)) \
156 curelm = curelm->field.sle_next; \
157 curelm->field.sle_next = \
158 curelm->field.sle_next->field.sle_next; \
159 } \
160 _Q_INVALIDATE((elm)->field.sle_next); \
161 } while (0)
162
163 /*
164 * List definitions.
165 */
166 #define LIST_HEAD(name, type) \
167 struct name { \
168 struct type *lh_first; /* first element */ \
169 }
170
171 #define LIST_HEAD_INITIALIZER(head) \
172 { NULL }
173
174 #define LIST_ENTRY(type) \
175 struct { \
176 struct type *le_next; /* next element */ \
177 struct type **le_prev; /* address of previous next element */ \
178 }
179
180 /*
181 * List access methods.
182 */
183 #define LIST_FIRST(head) ((head)->lh_first)
184 #define LIST_END(head) NULL
185 #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head))
186 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
187
188 #define LIST_FOREACH(var, head, field) \
189 for((var) = LIST_FIRST(head); \
190 (var)!= LIST_END(head); \
191 (var) = LIST_NEXT(var, field))
192
193 #define LIST_FOREACH_SAFE(var, head, field, tvar) \
194 for ((var) = LIST_FIRST(head); \
195 (var) && ((tvar) = LIST_NEXT(var, field), 1); \
196 (var) = (tvar))
197
198 /*
199 * List functions.
200 */
201 #define LIST_INIT(head) do { \
202 LIST_FIRST(head) = LIST_END(head); \
203 } while (0)
204
205 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
206 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
207 (listelm)->field.le_next->field.le_prev = \
208 &(elm)->field.le_next; \
209 (listelm)->field.le_next = (elm); \
210 (elm)->field.le_prev = &(listelm)->field.le_next; \
211 } while (0)
212
213 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
214 (elm)->field.le_prev = (listelm)->field.le_prev; \
215 (elm)->field.le_next = (listelm); \
216 *(listelm)->field.le_prev = (elm); \
217 (listelm)->field.le_prev = &(elm)->field.le_next; \
218 } while (0)
219
220 #define LIST_INSERT_HEAD(head, elm, field) do { \
221 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
222 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
223 (head)->lh_first = (elm); \
224 (elm)->field.le_prev = &(head)->lh_first; \
225 } while (0)
226
227 #define LIST_REMOVE(elm, field) do { \
228 if ((elm)->field.le_next != NULL) \
229 (elm)->field.le_next->field.le_prev = \
230 (elm)->field.le_prev; \
231 *(elm)->field.le_prev = (elm)->field.le_next; \
232 _Q_INVALIDATE((elm)->field.le_prev); \
233 _Q_INVALIDATE((elm)->field.le_next); \
234 } while (0)
235
236 #define LIST_REPLACE(elm, elm2, field) do { \
237 if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \
238 (elm2)->field.le_next->field.le_prev = \
239 &(elm2)->field.le_next; \
240 (elm2)->field.le_prev = (elm)->field.le_prev; \
241 *(elm2)->field.le_prev = (elm2); \
242 _Q_INVALIDATE((elm)->field.le_prev); \
243 _Q_INVALIDATE((elm)->field.le_next); \
244 } while (0)
245
246 /*
247 * Simple queue definitions.
248 */
249 #define SIMPLEQ_HEAD(name, type) \
250 struct name { \
251 struct type *sqh_first; /* first element */ \
252 struct type **sqh_last; /* addr of last next element */ \
253 }
254
255 #define SIMPLEQ_HEAD_INITIALIZER(head) \
256 { NULL, &(head).sqh_first }
257
258 #define SIMPLEQ_ENTRY(type) \
259 struct { \
260 struct type *sqe_next; /* next element */ \
261 }
262
263 /*
264 * Simple queue access methods.
265 */
266 #define SIMPLEQ_FIRST(head) ((head)->sqh_first)
267 #define SIMPLEQ_END(head) NULL
268 #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
269 #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
270
271 #define SIMPLEQ_FOREACH(var, head, field) \
272 for((var) = SIMPLEQ_FIRST(head); \
273 (var) != SIMPLEQ_END(head); \
274 (var) = SIMPLEQ_NEXT(var, field))
275
276 #define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
277 for ((var) = SIMPLEQ_FIRST(head); \
278 (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \
279 (var) = (tvar))
280
281 /*
282 * Simple queue functions.
283 */
284 #define SIMPLEQ_INIT(head) do { \
285 (head)->sqh_first = NULL; \
286 (head)->sqh_last = &(head)->sqh_first; \
287 } while (0)
288
289 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
290 if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
291 (head)->sqh_last = &(elm)->field.sqe_next; \
292 (head)->sqh_first = (elm); \
293 } while (0)
294
295 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
296 (elm)->field.sqe_next = NULL; \
297 *(head)->sqh_last = (elm); \
298 (head)->sqh_last = &(elm)->field.sqe_next; \
299 } while (0)
300
301 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
302 if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
303 (head)->sqh_last = &(elm)->field.sqe_next; \
304 (listelm)->field.sqe_next = (elm); \
305 } while (0)
306
307 #define SIMPLEQ_REMOVE_HEAD(head, field) do { \
308 if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
309 (head)->sqh_last = &(head)->sqh_first; \
310 } while (0)
311
312 #define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
313 if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \
314 == NULL) \
315 (head)->sqh_last = &(elm)->field.sqe_next; \
316 } while (0)
317
318 #define SIMPLEQ_CONCAT(head1, head2) do { \
319 if (!SIMPLEQ_EMPTY((head2))) { \
320 *(head1)->sqh_last = (head2)->sqh_first; \
321 (head1)->sqh_last = (head2)->sqh_last; \
322 SIMPLEQ_INIT((head2)); \
323 } \
324 } while (0)
325
326 /*
327 * XOR Simple queue definitions.
328 */
329 #define XSIMPLEQ_HEAD(name, type) \
330 struct name { \
331 struct type *sqx_first; /* first element */ \
332 struct type **sqx_last; /* addr of last next element */ \
333 unsigned long sqx_cookie; \
334 }
335
336 #define XSIMPLEQ_ENTRY(type) \
337 struct { \
338 struct type *sqx_next; /* next element */ \
339 }
340
341 /*
342 * XOR Simple queue access methods.
343 */
344 #define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \
345 (unsigned long)(ptr)))
346 #define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first))
347 #define XSIMPLEQ_END(head) NULL
348 #define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head))
349 #define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next))
350
351
352 #define XSIMPLEQ_FOREACH(var, head, field) \
353 for ((var) = XSIMPLEQ_FIRST(head); \
354 (var) != XSIMPLEQ_END(head); \
355 (var) = XSIMPLEQ_NEXT(head, var, field))
356
357 #define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \
358 for ((var) = XSIMPLEQ_FIRST(head); \
359 (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \
360 (var) = (tvar))
361
362 /*
363 * XOR Simple queue functions.
364 */
365 #define XSIMPLEQ_INIT(head) do { \
366 arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \
367 (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \
368 (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
369 } while (0)
370
371 #define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \
372 if (((elm)->field.sqx_next = (head)->sqx_first) == \
373 XSIMPLEQ_XOR(head, NULL)) \
374 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
375 (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \
376 } while (0)
377
378 #define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \
379 (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \
380 *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \
381 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
382 } while (0)
383
384 #define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
385 if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \
386 XSIMPLEQ_XOR(head, NULL)) \
387 (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
388 (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \
389 } while (0)
390
391 #define XSIMPLEQ_REMOVE_HEAD(head, field) do { \
392 if (((head)->sqx_first = XSIMPLEQ_XOR(head, \
393 (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \
394 (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \
395 } while (0)
396
397 #define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \
398 if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \
399 (elm)->field.sqx_next)->field.sqx_next) \
400 == XSIMPLEQ_XOR(head, NULL)) \
401 (head)->sqx_last = \
402 XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \
403 } while (0)
404
405
406 /*
407 * Tail queue definitions.
408 */
409 #define TAILQ_HEAD(name, type) \
410 struct name { \
411 struct type *tqh_first; /* first element */ \
412 struct type **tqh_last; /* addr of last next element */ \
413 }
414
415 #define TAILQ_HEAD_INITIALIZER(head) \
416 { NULL, &(head).tqh_first }
417
418 #define TAILQ_ENTRY(type) \
419 struct { \
420 struct type *tqe_next; /* next element */ \
421 struct type **tqe_prev; /* address of previous next element */ \
422 }
423
424 /*
425 * Tail queue access methods.
426 */
427 #define TAILQ_FIRST(head) ((head)->tqh_first)
428 #define TAILQ_END(head) NULL
429 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
430 #define TAILQ_LAST(head, headname) \
431 (*(((struct headname *)((head)->tqh_last))->tqh_last))
432 /* XXX */
433 #define TAILQ_PREV(elm, headname, field) \
434 (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
435 #define TAILQ_EMPTY(head) \
436 (TAILQ_FIRST(head) == TAILQ_END(head))
437
438 #define TAILQ_FOREACH(var, head, field) \
439 for((var) = TAILQ_FIRST(head); \
440 (var) != TAILQ_END(head); \
441 (var) = TAILQ_NEXT(var, field))
442
443 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
444 for ((var) = TAILQ_FIRST(head); \
445 (var) != TAILQ_END(head) && \
446 ((tvar) = TAILQ_NEXT(var, field), 1); \
447 (var) = (tvar))
448
449
450 #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
451 for((var) = TAILQ_LAST(head, headname); \
452 (var) != TAILQ_END(head); \
453 (var) = TAILQ_PREV(var, headname, field))
454
455 #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
456 for ((var) = TAILQ_LAST(head, headname); \
457 (var) != TAILQ_END(head) && \
458 ((tvar) = TAILQ_PREV(var, headname, field), 1); \
459 (var) = (tvar))
460
461 /*
462 * Tail queue functions.
463 */
464 #define TAILQ_INIT(head) do { \
465 (head)->tqh_first = NULL; \
466 (head)->tqh_last = &(head)->tqh_first; \
467 } while (0)
468
469 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
470 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
471 (head)->tqh_first->field.tqe_prev = \
472 &(elm)->field.tqe_next; \
473 else \
474 (head)->tqh_last = &(elm)->field.tqe_next; \
475 (head)->tqh_first = (elm); \
476 (elm)->field.tqe_prev = &(head)->tqh_first; \
477 } while (0)
478
479 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
480 (elm)->field.tqe_next = NULL; \
481 (elm)->field.tqe_prev = (head)->tqh_last; \
482 *(head)->tqh_last = (elm); \
483 (head)->tqh_last = &(elm)->field.tqe_next; \
484 } while (0)
485
486 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
487 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
488 (elm)->field.tqe_next->field.tqe_prev = \
489 &(elm)->field.tqe_next; \
490 else \
491 (head)->tqh_last = &(elm)->field.tqe_next; \
492 (listelm)->field.tqe_next = (elm); \
493 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
494 } while (0)
495
496 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
497 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
498 (elm)->field.tqe_next = (listelm); \
499 *(listelm)->field.tqe_prev = (elm); \
500 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
501 } while (0)
502
503 #define TAILQ_REMOVE(head, elm, field) do { \
504 if (((elm)->field.tqe_next) != NULL) \
505 (elm)->field.tqe_next->field.tqe_prev = \
506 (elm)->field.tqe_prev; \
507 else \
508 (head)->tqh_last = (elm)->field.tqe_prev; \
509 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
510 _Q_INVALIDATE((elm)->field.tqe_prev); \
511 _Q_INVALIDATE((elm)->field.tqe_next); \
512 } while (0)
513
514 #define TAILQ_REPLACE(head, elm, elm2, field) do { \
515 if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
516 (elm2)->field.tqe_next->field.tqe_prev = \
517 &(elm2)->field.tqe_next; \
518 else \
519 (head)->tqh_last = &(elm2)->field.tqe_next; \
520 (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
521 *(elm2)->field.tqe_prev = (elm2); \
522 _Q_INVALIDATE((elm)->field.tqe_prev); \
523 _Q_INVALIDATE((elm)->field.tqe_next); \
524 } while (0)
525
526 #define TAILQ_CONCAT(head1, head2, field) do { \
527 if (!TAILQ_EMPTY(head2)) { \
528 *(head1)->tqh_last = (head2)->tqh_first; \
529 (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
530 (head1)->tqh_last = (head2)->tqh_last; \
531 TAILQ_INIT((head2)); \
532 } \
533 } while (0)
534
535 #endif /* !_SYS_QUEUE_H_ */