xref: /illumos-gate/usr/src/cmd/sendmail/include/sm/tailq.h (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1058561cbSjbeck /*	$OpenBSD: queue.h,v 1.30 2005/10/25 06:37:47 otto Exp $	*/
2058561cbSjbeck /*	$NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $	*/
3058561cbSjbeck 
4058561cbSjbeck /*
5058561cbSjbeck  * Copyright (c) 1991, 1993
6058561cbSjbeck  *	The Regents of the University of California.  All rights reserved.
7058561cbSjbeck  *
8058561cbSjbeck  * Redistribution and use in source and binary forms, with or without
9058561cbSjbeck  * modification, are permitted provided that the following conditions
10058561cbSjbeck  * are met:
11058561cbSjbeck  * 1. Redistributions of source code must retain the above copyright
12058561cbSjbeck  *    notice, this list of conditions and the following disclaimer.
13058561cbSjbeck  * 2. Redistributions in binary form must reproduce the above copyright
14058561cbSjbeck  *    notice, this list of conditions and the following disclaimer in the
15058561cbSjbeck  *    documentation and/or other materials provided with the distribution.
16058561cbSjbeck  * 3. Neither the name of the University nor the names of its contributors
17058561cbSjbeck  *    may be used to endorse or promote products derived from this software
18058561cbSjbeck  *    without specific prior written permission.
19058561cbSjbeck  *
20058561cbSjbeck  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21058561cbSjbeck  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22058561cbSjbeck  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23058561cbSjbeck  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24058561cbSjbeck  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25058561cbSjbeck  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26058561cbSjbeck  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27058561cbSjbeck  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28058561cbSjbeck  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29058561cbSjbeck  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30058561cbSjbeck  * SUCH DAMAGE.
31058561cbSjbeck  *
32058561cbSjbeck  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
33058561cbSjbeck  */
34058561cbSjbeck 
35058561cbSjbeck #ifndef	SM_TAILQ_H_
36058561cbSjbeck #define	SM_TAILQ_H_
37058561cbSjbeck 
38058561cbSjbeck /*
39*7800901eSjbeck  * $Id: tailq.h,v 1.2 2007/06/29 23:09:57 ca Exp $
40*7800901eSjbeck  *
41058561cbSjbeck  * This file is a modified copy of queue.h from a BSD system:
42058561cbSjbeck  * we only need tail queues here.
43*7800901eSjbeck  * We do not use queue.h directly because there is a conflict with
44*7800901eSjbeck  * some versions of that file on some OSs.
45058561cbSjbeck  *
46058561cbSjbeck  * A tail queue is headed by a pair of pointers, one to the head of the
47058561cbSjbeck  * list and the other to the tail of the list. The elements are doubly
48058561cbSjbeck  * linked so that an arbitrary element can be removed without a need to
49058561cbSjbeck  * traverse the list. New elements can be added to the list before or
50058561cbSjbeck  * after an existing element, at the head of the list, or at the end of
51058561cbSjbeck  * the list. A tail queue may be traversed in either direction.
52058561cbSjbeck  */
53058561cbSjbeck 
54058561cbSjbeck /*
55058561cbSjbeck  * Tail queue definitions.
56058561cbSjbeck  */
57058561cbSjbeck #define SM_TAILQ_HEAD(name, type)					\
58058561cbSjbeck struct name {								\
59058561cbSjbeck 	struct type *tqh_first;	/* first element */			\
60058561cbSjbeck 	struct type **tqh_last;	/* addr of last next element */		\
61058561cbSjbeck }
62058561cbSjbeck 
63058561cbSjbeck #define SM_TAILQ_HEAD_INITIALIZER(head)					\
64058561cbSjbeck 	{ NULL, &(head).tqh_first }
65058561cbSjbeck 
66058561cbSjbeck #define SM_TAILQ_ENTRY(type)						\
67058561cbSjbeck struct {								\
68058561cbSjbeck 	struct type *tqe_next;	/* next element */			\
69058561cbSjbeck 	struct type **tqe_prev;	/* address of previous next element */	\
70058561cbSjbeck }
71058561cbSjbeck 
72058561cbSjbeck /*
73058561cbSjbeck  * tail queue access methods
74058561cbSjbeck  */
75058561cbSjbeck #define	SM_TAILQ_FIRST(head)		((head)->tqh_first)
76058561cbSjbeck #define	SM_TAILQ_END(head)		NULL
77058561cbSjbeck #define	SM_TAILQ_NEXT(elm, field)	((elm)->field.tqe_next)
78058561cbSjbeck #define SM_TAILQ_LAST(head, headname)					\
79058561cbSjbeck 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
80058561cbSjbeck /* XXX */
81058561cbSjbeck #define SM_TAILQ_PREV(elm, headname, field)				\
82058561cbSjbeck 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
83058561cbSjbeck #define	SM_TAILQ_EMPTY(head)						\
84058561cbSjbeck 	(SM_TAILQ_FIRST(head) == SM_TAILQ_END(head))
85058561cbSjbeck 
86058561cbSjbeck #define SM_TAILQ_FOREACH(var, head, field)				\
87058561cbSjbeck 	for((var) = SM_TAILQ_FIRST(head);				\
88058561cbSjbeck 	    (var) != SM_TAILQ_END(head);				\
89058561cbSjbeck 	    (var) = SM_TAILQ_NEXT(var, field))
90058561cbSjbeck 
91058561cbSjbeck #define SM_TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
92058561cbSjbeck 	for((var) = SM_TAILQ_LAST(head, headname);			\
93058561cbSjbeck 	    (var) != SM_TAILQ_END(head);				\
94058561cbSjbeck 	    (var) = SM_TAILQ_PREV(var, headname, field))
95058561cbSjbeck 
96058561cbSjbeck /*
97058561cbSjbeck  * Tail queue functions.
98058561cbSjbeck  */
99058561cbSjbeck #define	SM_TAILQ_INIT(head) do {					\
100058561cbSjbeck 	(head)->tqh_first = NULL;					\
101058561cbSjbeck 	(head)->tqh_last = &(head)->tqh_first;				\
102058561cbSjbeck } while (0)
103058561cbSjbeck 
104058561cbSjbeck #define SM_TAILQ_INSERT_HEAD(head, elm, field) do {			\
105058561cbSjbeck 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
106058561cbSjbeck 		(head)->tqh_first->field.tqe_prev =			\
107058561cbSjbeck 		    &(elm)->field.tqe_next;				\
108058561cbSjbeck 	else								\
109058561cbSjbeck 		(head)->tqh_last = &(elm)->field.tqe_next;		\
110058561cbSjbeck 	(head)->tqh_first = (elm);					\
111058561cbSjbeck 	(elm)->field.tqe_prev = &(head)->tqh_first;			\
112058561cbSjbeck } while (0)
113058561cbSjbeck 
114058561cbSjbeck #define SM_TAILQ_INSERT_TAIL(head, elm, field) do {			\
115058561cbSjbeck 	(elm)->field.tqe_next = NULL;					\
116058561cbSjbeck 	(elm)->field.tqe_prev = (head)->tqh_last;			\
117058561cbSjbeck 	*(head)->tqh_last = (elm);					\
118058561cbSjbeck 	(head)->tqh_last = &(elm)->field.tqe_next;			\
119058561cbSjbeck } while (0)
120058561cbSjbeck 
121058561cbSjbeck #define SM_TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
122058561cbSjbeck 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
123058561cbSjbeck 		(elm)->field.tqe_next->field.tqe_prev =			\
124058561cbSjbeck 		    &(elm)->field.tqe_next;				\
125058561cbSjbeck 	else								\
126058561cbSjbeck 		(head)->tqh_last = &(elm)->field.tqe_next;		\
127058561cbSjbeck 	(listelm)->field.tqe_next = (elm);				\
128058561cbSjbeck 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
129058561cbSjbeck } while (0)
130058561cbSjbeck 
131058561cbSjbeck #define	SM_TAILQ_INSERT_BEFORE(listelm, elm, field) do {		\
132058561cbSjbeck 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
133058561cbSjbeck 	(elm)->field.tqe_next = (listelm);				\
134058561cbSjbeck 	*(listelm)->field.tqe_prev = (elm);				\
135058561cbSjbeck 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
136058561cbSjbeck } while (0)
137058561cbSjbeck 
138058561cbSjbeck #define SM_TAILQ_REMOVE(head, elm, field) do {				\
139058561cbSjbeck 	if (((elm)->field.tqe_next) != NULL)				\
140058561cbSjbeck 		(elm)->field.tqe_next->field.tqe_prev =			\
141058561cbSjbeck 		    (elm)->field.tqe_prev;				\
142058561cbSjbeck 	else								\
143058561cbSjbeck 		(head)->tqh_last = (elm)->field.tqe_prev;		\
144058561cbSjbeck 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
145058561cbSjbeck } while (0)
146058561cbSjbeck 
147058561cbSjbeck #define SM_TAILQ_REPLACE(head, elm, elm2, field) do {			\
148058561cbSjbeck 	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\
149058561cbSjbeck 		(elm2)->field.tqe_next->field.tqe_prev =		\
150058561cbSjbeck 		    &(elm2)->field.tqe_next;				\
151058561cbSjbeck 	else								\
152058561cbSjbeck 		(head)->tqh_last = &(elm2)->field.tqe_next;		\
153058561cbSjbeck 	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\
154058561cbSjbeck 	*(elm2)->field.tqe_prev = (elm2);				\
155058561cbSjbeck } while (0)
156058561cbSjbeck 
157058561cbSjbeck #endif	/* !SM_TAILQ_H_ */
158