1168515Sgshapiro/*	$OpenBSD: queue.h,v 1.30 2005/10/25 06:37:47 otto Exp $	*/
2168515Sgshapiro/*	$NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $	*/
3168515Sgshapiro
4168515Sgshapiro/*
5168515Sgshapiro * Copyright (c) 1991, 1993
6168515Sgshapiro *	The Regents of the University of California.  All rights reserved.
7168515Sgshapiro *
8168515Sgshapiro * Redistribution and use in source and binary forms, with or without
9168515Sgshapiro * modification, are permitted provided that the following conditions
10168515Sgshapiro * are met:
11168515Sgshapiro * 1. Redistributions of source code must retain the above copyright
12168515Sgshapiro *    notice, this list of conditions and the following disclaimer.
13168515Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
14168515Sgshapiro *    notice, this list of conditions and the following disclaimer in the
15168515Sgshapiro *    documentation and/or other materials provided with the distribution.
16168515Sgshapiro * 3. Neither the name of the University nor the names of its contributors
17168515Sgshapiro *    may be used to endorse or promote products derived from this software
18168515Sgshapiro *    without specific prior written permission.
19168515Sgshapiro *
20168515Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21168515Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22168515Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23168515Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24168515Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25168515Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26168515Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27168515Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28168515Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29168515Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30168515Sgshapiro * SUCH DAMAGE.
31168515Sgshapiro *
32168515Sgshapiro *	@(#)queue.h	8.5 (Berkeley) 8/20/94
33168515Sgshapiro */
34168515Sgshapiro
35168515Sgshapiro#ifndef	SM_TAILQ_H_
36168515Sgshapiro#define	SM_TAILQ_H_
37168515Sgshapiro
38168515Sgshapiro/*
39266527Sgshapiro * $Id: tailq.h,v 1.3 2012-01-21 00:12:14 ashish Exp $
40173340Sgshapiro *
41168515Sgshapiro * This file is a modified copy of queue.h from a BSD system:
42168515Sgshapiro * we only need tail queues here.
43173340Sgshapiro * We do not use queue.h directly because there is a conflict with
44173340Sgshapiro * some versions of that file on some OSs.
45168515Sgshapiro *
46168515Sgshapiro * A tail queue is headed by a pair of pointers, one to the head of the
47168515Sgshapiro * list and the other to the tail of the list. The elements are doubly
48168515Sgshapiro * linked so that an arbitrary element can be removed without a need to
49168515Sgshapiro * traverse the list. New elements can be added to the list before or
50168515Sgshapiro * after an existing element, at the head of the list, or at the end of
51168515Sgshapiro * the list. A tail queue may be traversed in either direction.
52168515Sgshapiro */
53168515Sgshapiro
54168515Sgshapiro/*
55168515Sgshapiro * Tail queue definitions.
56168515Sgshapiro */
57168515Sgshapiro#define SM_TAILQ_HEAD(name, type)					\
58168515Sgshapirostruct name {								\
59168515Sgshapiro	struct type *tqh_first;	/* first element */			\
60168515Sgshapiro	struct type **tqh_last;	/* addr of last next element */		\
61168515Sgshapiro}
62168515Sgshapiro
63168515Sgshapiro#define SM_TAILQ_HEAD_INITIALIZER(head)					\
64168515Sgshapiro	{ NULL, &(head).tqh_first }
65168515Sgshapiro
66168515Sgshapiro#define SM_TAILQ_ENTRY(type)						\
67168515Sgshapirostruct {								\
68168515Sgshapiro	struct type *tqe_next;	/* next element */			\
69168515Sgshapiro	struct type **tqe_prev;	/* address of previous next element */	\
70168515Sgshapiro}
71168515Sgshapiro
72244833Sgshapiro/*
73244833Sgshapiro * tail queue access methods
74168515Sgshapiro */
75168515Sgshapiro#define	SM_TAILQ_FIRST(head)		((head)->tqh_first)
76168515Sgshapiro#define	SM_TAILQ_END(head)		NULL
77168515Sgshapiro#define	SM_TAILQ_NEXT(elm, field)	((elm)->field.tqe_next)
78168515Sgshapiro#define SM_TAILQ_LAST(head, headname)					\
79168515Sgshapiro	(*(((struct headname *)((head)->tqh_last))->tqh_last))
80168515Sgshapiro/* XXX */
81168515Sgshapiro#define SM_TAILQ_PREV(elm, headname, field)				\
82168515Sgshapiro	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
83168515Sgshapiro#define	SM_TAILQ_EMPTY(head)						\
84168515Sgshapiro	(SM_TAILQ_FIRST(head) == SM_TAILQ_END(head))
85168515Sgshapiro
86168515Sgshapiro#define SM_TAILQ_FOREACH(var, head, field)				\
87168515Sgshapiro	for((var) = SM_TAILQ_FIRST(head);				\
88168515Sgshapiro	    (var) != SM_TAILQ_END(head);				\
89168515Sgshapiro	    (var) = SM_TAILQ_NEXT(var, field))
90168515Sgshapiro
91168515Sgshapiro#define SM_TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
92168515Sgshapiro	for((var) = SM_TAILQ_LAST(head, headname);			\
93168515Sgshapiro	    (var) != SM_TAILQ_END(head);				\
94168515Sgshapiro	    (var) = SM_TAILQ_PREV(var, headname, field))
95168515Sgshapiro
96168515Sgshapiro/*
97168515Sgshapiro * Tail queue functions.
98168515Sgshapiro */
99168515Sgshapiro#define	SM_TAILQ_INIT(head) do {					\
100168515Sgshapiro	(head)->tqh_first = NULL;					\
101168515Sgshapiro	(head)->tqh_last = &(head)->tqh_first;				\
102168515Sgshapiro} while (0)
103168515Sgshapiro
104168515Sgshapiro#define SM_TAILQ_INSERT_HEAD(head, elm, field) do {			\
105168515Sgshapiro	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
106168515Sgshapiro		(head)->tqh_first->field.tqe_prev =			\
107168515Sgshapiro		    &(elm)->field.tqe_next;				\
108168515Sgshapiro	else								\
109168515Sgshapiro		(head)->tqh_last = &(elm)->field.tqe_next;		\
110168515Sgshapiro	(head)->tqh_first = (elm);					\
111168515Sgshapiro	(elm)->field.tqe_prev = &(head)->tqh_first;			\
112168515Sgshapiro} while (0)
113168515Sgshapiro
114168515Sgshapiro#define SM_TAILQ_INSERT_TAIL(head, elm, field) do {			\
115168515Sgshapiro	(elm)->field.tqe_next = NULL;					\
116168515Sgshapiro	(elm)->field.tqe_prev = (head)->tqh_last;			\
117168515Sgshapiro	*(head)->tqh_last = (elm);					\
118168515Sgshapiro	(head)->tqh_last = &(elm)->field.tqe_next;			\
119168515Sgshapiro} while (0)
120168515Sgshapiro
121168515Sgshapiro#define SM_TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
122168515Sgshapiro	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
123168515Sgshapiro		(elm)->field.tqe_next->field.tqe_prev =			\
124168515Sgshapiro		    &(elm)->field.tqe_next;				\
125168515Sgshapiro	else								\
126168515Sgshapiro		(head)->tqh_last = &(elm)->field.tqe_next;		\
127168515Sgshapiro	(listelm)->field.tqe_next = (elm);				\
128168515Sgshapiro	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
129168515Sgshapiro} while (0)
130168515Sgshapiro
131168515Sgshapiro#define	SM_TAILQ_INSERT_BEFORE(listelm, elm, field) do {		\
132168515Sgshapiro	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
133168515Sgshapiro	(elm)->field.tqe_next = (listelm);				\
134168515Sgshapiro	*(listelm)->field.tqe_prev = (elm);				\
135168515Sgshapiro	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
136168515Sgshapiro} while (0)
137168515Sgshapiro
138168515Sgshapiro#define SM_TAILQ_REMOVE(head, elm, field) do {				\
139168515Sgshapiro	if (((elm)->field.tqe_next) != NULL)				\
140168515Sgshapiro		(elm)->field.tqe_next->field.tqe_prev =			\
141168515Sgshapiro		    (elm)->field.tqe_prev;				\
142168515Sgshapiro	else								\
143168515Sgshapiro		(head)->tqh_last = (elm)->field.tqe_prev;		\
144168515Sgshapiro	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
145168515Sgshapiro} while (0)
146168515Sgshapiro
147168515Sgshapiro#define SM_TAILQ_REPLACE(head, elm, elm2, field) do {			\
148168515Sgshapiro	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\
149168515Sgshapiro		(elm2)->field.tqe_next->field.tqe_prev =		\
150168515Sgshapiro		    &(elm2)->field.tqe_next;				\
151168515Sgshapiro	else								\
152168515Sgshapiro		(head)->tqh_last = &(elm2)->field.tqe_next;		\
153168515Sgshapiro	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\
154168515Sgshapiro	*(elm2)->field.tqe_prev = (elm2);				\
155168515Sgshapiro} while (0)
156168515Sgshapiro
157168515Sgshapiro#endif	/* !SM_TAILQ_H_ */
158