1 /*	$OpenBSD: queue.h,v 1.22 2001/06/23 04:39:35 angelos 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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
37  */
38 
39 #ifndef	_SYS_QUEUE_H_
40 #define	_SYS_QUEUE_H_
41 
42 /*
43  * This file defines five types of data structures: singly-linked lists,
44  * lists, simple queues, tail queues, and circular queues.
45  *
46  *
47  * A singly-linked list is headed by a single forward pointer. The elements
48  * are singly linked for minimum space and pointer manipulation overhead at
49  * the expense of O(n) removal for arbitrary elements. New elements can be
50  * added to the list after an existing element or at the head of the list.
51  * Elements being removed from the head of the list should use the explicit
52  * macro for this purpose for optimum efficiency. A singly-linked list may
53  * only be traversed in the forward direction.  Singly-linked lists are ideal
54  * for applications with large datasets and few or no removals or for
55  * implementing a LIFO queue.
56  *
57  * A list is headed by a single forward pointer (or an array of forward
58  * pointers for a hash table header). The elements are doubly linked
59  * so that an arbitrary element can be removed without a need to
60  * traverse the list. New elements can be added to the list before
61  * or after an existing element or at the head of the list. A list
62  * may only be traversed in the forward direction.
63  *
64  * A simple queue is headed by a pair of pointers, one the head of the
65  * list and the other to the tail of the list. The elements are singly
66  * linked to save space, so elements can only be removed from the
67  * head of the list. New elements can be added to the list before or after
68  * an existing element, at the head of the list, or at the end of the
69  * list. A simple queue may only be traversed in the forward direction.
70  *
71  * A tail queue is headed by a pair of pointers, one to the head of the
72  * list and the other to the tail of the list. The elements are doubly
73  * linked 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 or
75  * after an existing element, at the head of the list, or at the end of
76  * the list. A tail queue may be traversed in either direction.
77  *
78  * A circle 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 after
82  * an existing element, at the head of the list, or at the end of the list.
83  * A circle queue may be traversed in either direction, but has a more
84  * complex end of list detection.
85  *
86  * For details on the use of these macros, see the queue(3) manual page.
87  */
88 
89 /*
90  * Singly-linked List definitions.
91  */
92 #define SLIST_HEAD(name, type)						\
93 struct name {								\
94 	struct type *slh_first;	/* first element */			\
95 }
96 
97 #define	SLIST_HEAD_INITIALIZER(head)					\
98 	{ NULL }
99 
100 #define SLIST_ENTRY(type)						\
101 struct {								\
102 	struct type *sle_next;	/* next element */			\
103 }
104 
105 /*
106  * Singly-linked List access methods.
107  */
108 #define	SLIST_FIRST(head)	((head)->slh_first)
109 #define	SLIST_END(head)		NULL
110 #define	SLIST_EMPTY(head)	(SLIST_FIRST(head) == SLIST_END(head))
111 #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
112 
113 #define	SLIST_FOREACH(var, head, field)					\
114 	for((var) = SLIST_FIRST(head);					\
115 	    (var) != SLIST_END(head);					\
116 	    (var) = SLIST_NEXT(var, field))
117 
118 /*
119  * Singly-linked List functions.
120  */
121 #define	SLIST_INIT(head) {						\
122 	SLIST_FIRST(head) = SLIST_END(head);				\
123 }
124 
125 #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
126 	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
127 	(slistelm)->field.sle_next = (elm);				\
128 } while (0)
129 
130 #define	SLIST_INSERT_HEAD(head, elm, field) do {			\
131 	(elm)->field.sle_next = (head)->slh_first;			\
132 	(head)->slh_first = (elm);					\
133 } while (0)
134 
135 #define	SLIST_REMOVE_HEAD(head, field) do {				\
136 	(head)->slh_first = (head)->slh_first->field.sle_next;		\
137 } while (0)
138 
139 #define SLIST_REMOVE(head, elm, type, field) do {			\
140 	if ((head)->slh_first == (elm)) {				\
141 		SLIST_REMOVE_HEAD((head), field);			\
142 	}								\
143 	else {								\
144 		struct type *curelm = (head)->slh_first;		\
145 		while( curelm->field.sle_next != (elm) )		\
146 			curelm = curelm->field.sle_next;		\
147 		curelm->field.sle_next =				\
148 		    curelm->field.sle_next->field.sle_next;		\
149 	}								\
150 } while (0)
151 
152 /*
153  * List definitions.
154  */
155 #define LIST_HEAD(name, type)						\
156 struct name {								\
157 	struct type *lh_first;	/* first element */			\
158 }
159 
160 #define LIST_HEAD_INITIALIZER(head)					\
161 	{ NULL }
162 
163 #define LIST_ENTRY(type)						\
164 struct {								\
165 	struct type *le_next;	/* next element */			\
166 	struct type **le_prev;	/* address of previous next element */	\
167 }
168 
169 /*
170  * List access methods
171  */
172 #define	LIST_FIRST(head)		((head)->lh_first)
173 #define	LIST_END(head)			NULL
174 #define	LIST_EMPTY(head)		(LIST_FIRST(head) == LIST_END(head))
175 #define	LIST_NEXT(elm, field)		((elm)->field.le_next)
176 
177 #define LIST_FOREACH(var, head, field)					\
178 	for((var) = LIST_FIRST(head);					\
179 	    (var)!= LIST_END(head);					\
180 	    (var) = LIST_NEXT(var, field))
181 
182 /*
183  * List functions.
184  */
185 #define	LIST_INIT(head) do {						\
186 	LIST_FIRST(head) = LIST_END(head);				\
187 } while (0)
188 
189 #define LIST_INSERT_AFTER(listelm, elm, field) do {			\
190 	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\
191 		(listelm)->field.le_next->field.le_prev =		\
192 		    &(elm)->field.le_next;				\
193 	(listelm)->field.le_next = (elm);				\
194 	(elm)->field.le_prev = &(listelm)->field.le_next;		\
195 } while (0)
196 
197 #define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
198 	(elm)->field.le_prev = (listelm)->field.le_prev;		\
199 	(elm)->field.le_next = (listelm);				\
200 	*(listelm)->field.le_prev = (elm);				\
201 	(listelm)->field.le_prev = &(elm)->field.le_next;		\
202 } while (0)
203 
204 #define LIST_INSERT_HEAD(head, elm, field) do {				\
205 	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\
206 		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
207 	(head)->lh_first = (elm);					\
208 	(elm)->field.le_prev = &(head)->lh_first;			\
209 } while (0)
210 
211 #define LIST_REMOVE(elm, field) do {					\
212 	if ((elm)->field.le_next != NULL)				\
213 		(elm)->field.le_next->field.le_prev =			\
214 		    (elm)->field.le_prev;				\
215 	*(elm)->field.le_prev = (elm)->field.le_next;			\
216 } while (0)
217 
218 #define LIST_REPLACE(elm, elm2, field) do {				\
219 	if (((elm2)->field.le_next = (elm)->field.le_next) != NULL)	\
220 		(elm2)->field.le_next->field.le_prev =			\
221 		    &(elm2)->field.le_next;				\
222 	(elm2)->field.le_prev = (elm)->field.le_prev;			\
223 	*(elm2)->field.le_prev = (elm2);				\
224 } while (0)
225 
226 /*
227  * Simple queue definitions.
228  */
229 #define SIMPLEQ_HEAD(name, type)					\
230 struct name {								\
231 	struct type *sqh_first;	/* first element */			\
232 	struct type **sqh_last;	/* addr of last next element */		\
233 }
234 
235 #define SIMPLEQ_HEAD_INITIALIZER(head)					\
236 	{ NULL, &(head).sqh_first }
237 
238 #define SIMPLEQ_ENTRY(type)						\
239 struct {								\
240 	struct type *sqe_next;	/* next element */			\
241 }
242 
243 /*
244  * Simple queue access methods.
245  */
246 #define	SIMPLEQ_FIRST(head)	    ((head)->sqh_first)
247 #define	SIMPLEQ_END(head)	    NULL
248 #define	SIMPLEQ_EMPTY(head)	    (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
249 #define	SIMPLEQ_NEXT(elm, field)    ((elm)->field.sqe_next)
250 
251 #define SIMPLEQ_FOREACH(var, head, field)				\
252 	for((var) = SIMPLEQ_FIRST(head);				\
253 	    (var) != SIMPLEQ_END(head);					\
254 	    (var) = SIMPLEQ_NEXT(var, field))
255 
256 /*
257  * Simple queue functions.
258  */
259 #define	SIMPLEQ_INIT(head) do {						\
260 	(head)->sqh_first = NULL;					\
261 	(head)->sqh_last = &(head)->sqh_first;				\
262 } while (0)
263 
264 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
265 	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
266 		(head)->sqh_last = &(elm)->field.sqe_next;		\
267 	(head)->sqh_first = (elm);					\
268 } while (0)
269 
270 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
271 	(elm)->field.sqe_next = NULL;					\
272 	*(head)->sqh_last = (elm);					\
273 	(head)->sqh_last = &(elm)->field.sqe_next;			\
274 } while (0)
275 
276 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
277 	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
278 		(head)->sqh_last = &(elm)->field.sqe_next;		\
279 	(listelm)->field.sqe_next = (elm);				\
280 } while (0)
281 
282 #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do {			\
283 	if (((head)->sqh_first = (elm)->field.sqe_next) == NULL)	\
284 		(head)->sqh_last = &(head)->sqh_first;			\
285 } while (0)
286 
287 /*
288  * Tail queue definitions.
289  */
290 #define TAILQ_HEAD(name, type)						\
291 struct name {								\
292 	struct type *tqh_first;	/* first element */			\
293 	struct type **tqh_last;	/* addr of last next element */		\
294 }
295 
296 #define TAILQ_HEAD_INITIALIZER(head)					\
297 	{ NULL, &(head).tqh_first }
298 
299 #define TAILQ_ENTRY(type)						\
300 struct {								\
301 	struct type *tqe_next;	/* next element */			\
302 	struct type **tqe_prev;	/* address of previous next element */	\
303 }
304 
305 /*
306  * tail queue access methods
307  */
308 #define	TAILQ_FIRST(head)		((head)->tqh_first)
309 #define	TAILQ_END(head)			NULL
310 #define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
311 #define TAILQ_LAST(head, headname)					\
312 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
313 /* XXX */
314 #define TAILQ_PREV(elm, headname, field)				\
315 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
316 #define	TAILQ_EMPTY(head)						\
317 	(TAILQ_FIRST(head) == TAILQ_END(head))
318 
319 #define TAILQ_FOREACH(var, head, field)					\
320 	for((var) = TAILQ_FIRST(head);					\
321 	    (var) != TAILQ_END(head);					\
322 	    (var) = TAILQ_NEXT(var, field))
323 
324 #define TAILQ_FOREACH_REVERSE(var, head, field, headname)		\
325 	for((var) = TAILQ_LAST(head, headname);				\
326 	    (var) != TAILQ_END(head);					\
327 	    (var) = TAILQ_PREV(var, headname, field))
328 
329 /*
330  * Tail queue functions.
331  */
332 #define	TAILQ_INIT(head) do {						\
333 	(head)->tqh_first = NULL;					\
334 	(head)->tqh_last = &(head)->tqh_first;				\
335 } while (0)
336 
337 #define TAILQ_INSERT_HEAD(head, elm, field) do {			\
338 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
339 		(head)->tqh_first->field.tqe_prev =			\
340 		    &(elm)->field.tqe_next;				\
341 	else								\
342 		(head)->tqh_last = &(elm)->field.tqe_next;		\
343 	(head)->tqh_first = (elm);					\
344 	(elm)->field.tqe_prev = &(head)->tqh_first;			\
345 } while (0)
346 
347 #define TAILQ_INSERT_TAIL(head, elm, field) do {			\
348 	(elm)->field.tqe_next = NULL;					\
349 	(elm)->field.tqe_prev = (head)->tqh_last;			\
350 	*(head)->tqh_last = (elm);					\
351 	(head)->tqh_last = &(elm)->field.tqe_next;			\
352 } while (0)
353 
354 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
355 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
356 		(elm)->field.tqe_next->field.tqe_prev =			\
357 		    &(elm)->field.tqe_next;				\
358 	else								\
359 		(head)->tqh_last = &(elm)->field.tqe_next;		\
360 	(listelm)->field.tqe_next = (elm);				\
361 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
362 } while (0)
363 
364 #define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
365 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
366 	(elm)->field.tqe_next = (listelm);				\
367 	*(listelm)->field.tqe_prev = (elm);				\
368 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
369 } while (0)
370 
371 #define TAILQ_REMOVE(head, elm, field) do {				\
372 	if (((elm)->field.tqe_next) != NULL)				\
373 		(elm)->field.tqe_next->field.tqe_prev =			\
374 		    (elm)->field.tqe_prev;				\
375 	else								\
376 		(head)->tqh_last = (elm)->field.tqe_prev;		\
377 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
378 } while (0)
379 
380 #define TAILQ_REPLACE(head, elm, elm2, field) do {			\
381 	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\
382 		(elm2)->field.tqe_next->field.tqe_prev =		\
383 		    &(elm2)->field.tqe_next;				\
384 	else								\
385 		(head)->tqh_last = &(elm2)->field.tqe_next;		\
386 	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\
387 	*(elm2)->field.tqe_prev = (elm2);				\
388 } while (0)
389 
390 /*
391  * Circular queue definitions.
392  */
393 #define CIRCLEQ_HEAD(name, type)					\
394 struct name {								\
395 	struct type *cqh_first;		/* first element */		\
396 	struct type *cqh_last;		/* last element */		\
397 }
398 
399 #define CIRCLEQ_HEAD_INITIALIZER(head)					\
400 	{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
401 
402 #define CIRCLEQ_ENTRY(type)						\
403 struct {								\
404 	struct type *cqe_next;		/* next element */		\
405 	struct type *cqe_prev;		/* previous element */		\
406 }
407 
408 /*
409  * Circular queue access methods
410  */
411 #define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
412 #define	CIRCLEQ_LAST(head)		((head)->cqh_last)
413 #define	CIRCLEQ_END(head)		((void *)(head))
414 #define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
415 #define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
416 #define	CIRCLEQ_EMPTY(head)						\
417 	(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
418 
419 #define CIRCLEQ_FOREACH(var, head, field)				\
420 	for((var) = CIRCLEQ_FIRST(head);				\
421 	    (var) != CIRCLEQ_END(head);					\
422 	    (var) = CIRCLEQ_NEXT(var, field))
423 
424 #define CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
425 	for((var) = CIRCLEQ_LAST(head);					\
426 	    (var) != CIRCLEQ_END(head);					\
427 	    (var) = CIRCLEQ_PREV(var, field))
428 
429 /*
430  * Circular queue functions.
431  */
432 #define	CIRCLEQ_INIT(head) do {						\
433 	(head)->cqh_first = CIRCLEQ_END(head);				\
434 	(head)->cqh_last = CIRCLEQ_END(head);				\
435 } while (0)
436 
437 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
438 	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
439 	(elm)->field.cqe_prev = (listelm);				\
440 	if ((listelm)->field.cqe_next == CIRCLEQ_END(head))		\
441 		(head)->cqh_last = (elm);				\
442 	else								\
443 		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
444 	(listelm)->field.cqe_next = (elm);				\
445 } while (0)
446 
447 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\
448 	(elm)->field.cqe_next = (listelm);				\
449 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
450 	if ((listelm)->field.cqe_prev == CIRCLEQ_END(head))		\
451 		(head)->cqh_first = (elm);				\
452 	else								\
453 		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
454 	(listelm)->field.cqe_prev = (elm);				\
455 } while (0)
456 
457 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
458 	(elm)->field.cqe_next = (head)->cqh_first;			\
459 	(elm)->field.cqe_prev = CIRCLEQ_END(head);			\
460 	if ((head)->cqh_last == CIRCLEQ_END(head))			\
461 		(head)->cqh_last = (elm);				\
462 	else								\
463 		(head)->cqh_first->field.cqe_prev = (elm);		\
464 	(head)->cqh_first = (elm);					\
465 } while (0)
466 
467 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
468 	(elm)->field.cqe_next = CIRCLEQ_END(head);			\
469 	(elm)->field.cqe_prev = (head)->cqh_last;			\
470 	if ((head)->cqh_first == CIRCLEQ_END(head))			\
471 		(head)->cqh_first = (elm);				\
472 	else								\
473 		(head)->cqh_last->field.cqe_next = (elm);		\
474 	(head)->cqh_last = (elm);					\
475 } while (0)
476 
477 #define	CIRCLEQ_REMOVE(head, elm, field) do {				\
478 	if ((elm)->field.cqe_next == CIRCLEQ_END(head))			\
479 		(head)->cqh_last = (elm)->field.cqe_prev;		\
480 	else								\
481 		(elm)->field.cqe_next->field.cqe_prev =			\
482 		    (elm)->field.cqe_prev;				\
483 	if ((elm)->field.cqe_prev == CIRCLEQ_END(head))			\
484 		(head)->cqh_first = (elm)->field.cqe_next;		\
485 	else								\
486 		(elm)->field.cqe_prev->field.cqe_next =			\
487 		    (elm)->field.cqe_next;				\
488 } while (0)
489 
490 #define CIRCLEQ_REPLACE(head, elm, elm2, field) do {			\
491 	if (((elm2)->field.cqe_next = (elm)->field.cqe_next) ==		\
492 	    CIRCLEQ_END(head))						\
493 		(head).cqh_last = (elm2);				\
494 	else								\
495 		(elm2)->field.cqe_next->field.cqe_prev = (elm2);	\
496 	if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) ==		\
497 	    CIRCLEQ_END(head))						\
498 		(head).cqh_first = (elm2);				\
499 	else								\
500 		(elm2)->field.cqe_prev->field.cqe_next = (elm2);	\
501 } while (0)
502 
503 #endif	/* !_SYS_QUEUE_H_ */
504