xref: /titanic_52/usr/src/cmd/sendmail/include/sm/tailq.h (revision a99982a76d4cc12b1e9021e88531cf425d1e7369)
1 /*	$OpenBSD: queue.h,v 1.30 2005/10/25 06:37:47 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	SM_TAILQ_H_
36 #define	SM_TAILQ_H_
37 
38 #pragma ident	"%Z%%M%	%I%	%E% SMI"
39 
40 /*
41  * This file is a modified copy of queue.h from a BSD system:
42  * we only need tail queues here.
43  *
44  * A tail queue is headed by a pair of pointers, one to the head of the
45  * list and the other to the tail of the list. The elements are doubly
46  * linked so that an arbitrary element can be removed without a need to
47  * traverse the list. New elements can be added to the list before or
48  * after an existing element, at the head of the list, or at the end of
49  * the list. A tail queue may be traversed in either direction.
50  */
51 
52 /*
53  * Tail queue definitions.
54  */
55 #define SM_TAILQ_HEAD(name, type)					\
56 struct name {								\
57 	struct type *tqh_first;	/* first element */			\
58 	struct type **tqh_last;	/* addr of last next element */		\
59 }
60 
61 #define SM_TAILQ_HEAD_INITIALIZER(head)					\
62 	{ NULL, &(head).tqh_first }
63 
64 #define SM_TAILQ_ENTRY(type)						\
65 struct {								\
66 	struct type *tqe_next;	/* next element */			\
67 	struct type **tqe_prev;	/* address of previous next element */	\
68 }
69 
70 /*
71  * tail queue access methods
72  */
73 #define	SM_TAILQ_FIRST(head)		((head)->tqh_first)
74 #define	SM_TAILQ_END(head)		NULL
75 #define	SM_TAILQ_NEXT(elm, field)	((elm)->field.tqe_next)
76 #define SM_TAILQ_LAST(head, headname)					\
77 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
78 /* XXX */
79 #define SM_TAILQ_PREV(elm, headname, field)				\
80 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
81 #define	SM_TAILQ_EMPTY(head)						\
82 	(SM_TAILQ_FIRST(head) == SM_TAILQ_END(head))
83 
84 #define SM_TAILQ_FOREACH(var, head, field)				\
85 	for((var) = SM_TAILQ_FIRST(head);				\
86 	    (var) != SM_TAILQ_END(head);				\
87 	    (var) = SM_TAILQ_NEXT(var, field))
88 
89 #define SM_TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
90 	for((var) = SM_TAILQ_LAST(head, headname);			\
91 	    (var) != SM_TAILQ_END(head);				\
92 	    (var) = SM_TAILQ_PREV(var, headname, field))
93 
94 /*
95  * Tail queue functions.
96  */
97 #define	SM_TAILQ_INIT(head) do {					\
98 	(head)->tqh_first = NULL;					\
99 	(head)->tqh_last = &(head)->tqh_first;				\
100 } while (0)
101 
102 #define SM_TAILQ_INSERT_HEAD(head, elm, field) do {			\
103 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
104 		(head)->tqh_first->field.tqe_prev =			\
105 		    &(elm)->field.tqe_next;				\
106 	else								\
107 		(head)->tqh_last = &(elm)->field.tqe_next;		\
108 	(head)->tqh_first = (elm);					\
109 	(elm)->field.tqe_prev = &(head)->tqh_first;			\
110 } while (0)
111 
112 #define SM_TAILQ_INSERT_TAIL(head, elm, field) do {			\
113 	(elm)->field.tqe_next = NULL;					\
114 	(elm)->field.tqe_prev = (head)->tqh_last;			\
115 	*(head)->tqh_last = (elm);					\
116 	(head)->tqh_last = &(elm)->field.tqe_next;			\
117 } while (0)
118 
119 #define SM_TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
120 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
121 		(elm)->field.tqe_next->field.tqe_prev =			\
122 		    &(elm)->field.tqe_next;				\
123 	else								\
124 		(head)->tqh_last = &(elm)->field.tqe_next;		\
125 	(listelm)->field.tqe_next = (elm);				\
126 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
127 } while (0)
128 
129 #define	SM_TAILQ_INSERT_BEFORE(listelm, elm, field) do {		\
130 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
131 	(elm)->field.tqe_next = (listelm);				\
132 	*(listelm)->field.tqe_prev = (elm);				\
133 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
134 } while (0)
135 
136 #define SM_TAILQ_REMOVE(head, elm, field) do {				\
137 	if (((elm)->field.tqe_next) != NULL)				\
138 		(elm)->field.tqe_next->field.tqe_prev =			\
139 		    (elm)->field.tqe_prev;				\
140 	else								\
141 		(head)->tqh_last = (elm)->field.tqe_prev;		\
142 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
143 } while (0)
144 
145 #define SM_TAILQ_REPLACE(head, elm, elm2, field) do {			\
146 	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\
147 		(elm2)->field.tqe_next->field.tqe_prev =		\
148 		    &(elm2)->field.tqe_next;				\
149 	else								\
150 		(head)->tqh_last = &(elm2)->field.tqe_next;		\
151 	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\
152 	*(elm2)->field.tqe_prev = (elm2);				\
153 } while (0)
154 
155 #endif	/* !SM_TAILQ_H_ */
156