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