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