1 /*	$OpenBSD: queue.h,v 1.31 2005/11/25 08:06:25 otto 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	_SIMPLE_QUEUE_H_
36 #define	_SIMPLE_QUEUE_H_
37 
38 /*
39  * A simple queue is headed by a pair of pointers, one the head of the
40  * list and the other to the tail of the list. The elements are singly
41  * linked to save space, so elements can only be removed from the
42  * head of the list. New elements can be added to the list before or after
43  * an existing element, at the head of the list, or at the end of the
44  * list. A simple queue may only be traversed in the forward direction.
45  *
46  * For details on the use of these macros, see the queue(3) manual page.
47  */
48 
49 /*
50  * Simple queue definitions.
51  */
52 #define SIMPLEQ_HEAD(name, type)					\
53 struct name {								\
54 	struct type *sqh_first;	/* first element */			\
55 	struct type **sqh_last;	/* addr of last next element */		\
56 }
57 
58 #define SIMPLEQ_HEAD_INITIALIZER(head)					\
59 	{ NULL, &(head).sqh_first }
60 
61 #define SIMPLEQ_ENTRY(type)						\
62 struct {								\
63 	struct type *sqe_next;	/* next element */			\
64 }
65 
66 /*
67  * Simple queue access methods.
68  */
69 #define	SIMPLEQ_FIRST(head)	    ((head)->sqh_first)
70 #define	SIMPLEQ_END(head)	    NULL
71 #define	SIMPLEQ_EMPTY(head)	    (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
72 #define	SIMPLEQ_NEXT(elm, field)    ((elm)->field.sqe_next)
73 
74 #define SIMPLEQ_FOREACH(var, head, field)				\
75 	for((var) = SIMPLEQ_FIRST(head);				\
76 	    (var) != SIMPLEQ_END(head);					\
77 	    (var) = SIMPLEQ_NEXT(var, field))
78 
79 /*
80  * Simple queue functions.
81  */
82 #define	SIMPLEQ_INIT(head) do {						\
83 	(head)->sqh_first = NULL;					\
84 	(head)->sqh_last = &(head)->sqh_first;				\
85 } while (0)
86 
87 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
88 	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
89 		(head)->sqh_last = &(elm)->field.sqe_next;		\
90 	(head)->sqh_first = (elm);					\
91 } while (0)
92 
93 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
94 	(elm)->field.sqe_next = NULL;					\
95 	*(head)->sqh_last = (elm);					\
96 	(head)->sqh_last = &(elm)->field.sqe_next;			\
97 } while (0)
98 
99 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
100 	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
101 		(head)->sqh_last = &(elm)->field.sqe_next;		\
102 	(listelm)->field.sqe_next = (elm);				\
103 } while (0)
104 
105 #define SIMPLEQ_REMOVE_HEAD(head, field) do {			\
106 	if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
107 		(head)->sqh_last = &(head)->sqh_first;			\
108 } while (0)
109 
110 #endif	/* !_SIMPLE_QUEUE_H_ */
111