andre@3: /* 
andre@3:  * Copyright (c) 1991, 1993
andre@3:  *	The Regents of the University of California.  All rights reserved.
andre@3:  *
andre@3:  * Redistribution and use in source and binary forms, with or without
andre@3:  * modification, are permitted provided that the following conditions
andre@3:  * are met:
andre@3:  * 1. Redistributions of source code must retain the above copyright
andre@3:  *    notice, this list of conditions and the following disclaimer.
andre@3:  * 2. Redistributions in binary form must reproduce the above copyright
andre@3:  *    notice, this list of conditions and the following disclaimer in the
andre@3:  *    documentation and/or other materials provided with the distribution.
andre@3:  * 3. ***REMOVED*** - see 
andre@3:  *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
andre@3:  * 4. Neither the name of the University nor the names of its contributors
andre@3:  *    may be used to endorse or promote products derived from this software
andre@3:  *    without specific prior written permission.
andre@3:  *
andre@3:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
andre@3:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
andre@3:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
andre@3:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
andre@3:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
andre@3:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
andre@3:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
andre@3:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
andre@3:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
andre@3:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
andre@3:  * SUCH DAMAGE.
andre@3:  *
andre@3:  *	@(#)queue.h	8.3 (Berkeley) 12/13/93
andre@3:  */
andre@3: 
andre@3: #ifndef	_QUEUE_H_
andre@3: #define	_QUEUE_H_
andre@3: 
andre@3: /*
andre@3:  * This file defines three types of data structures: lists, tail queues,
andre@3:  * and circular queues.
andre@3:  *
andre@3:  * A list is headed by a single forward pointer (or an array of forward
andre@3:  * pointers for a hash table header). The elements are doubly linked
andre@3:  * so that an arbitrary element can be removed without a need to
andre@3:  * traverse the list. New elements can be added to the list after
andre@3:  * an existing element or at the head of the list. A list may only be
andre@3:  * traversed in the forward direction.
andre@3:  *
andre@3:  * A tail queue is headed by a pair of pointers, one to the head of the
andre@3:  * list and the other to the tail of the list. The elements are doubly
andre@3:  * linked so that an arbitrary element can be removed without a need to
andre@3:  * traverse the list. New elements can be added to the list after
andre@3:  * an existing element, at the head of the list, or at the end of the
andre@3:  * list. A tail queue may only be traversed in the forward direction.
andre@3:  *
andre@3:  * A circle queue is headed by a pair of pointers, one to the head of the
andre@3:  * list and the other to the tail of the list. The elements are doubly
andre@3:  * linked so that an arbitrary element can be removed without a need to
andre@3:  * traverse the list. New elements can be added to the list before or after
andre@3:  * an existing element, at the head of the list, or at the end of the list.
andre@3:  * A circle queue may be traversed in either direction, but has a more
andre@3:  * complex end of list detection.
andre@3:  *
andre@3:  * For details on the use of these macros, see the queue(3) manual page.
andre@3:  */
andre@3: 
andre@3: /*
andre@3:  * List definitions.
andre@3:  */
andre@3: #define LIST_HEAD(name, type)						\
andre@3: struct name {								\
andre@3: 	struct type *lh_first;	/* first element */			\
andre@3: }
andre@3: 
andre@3: #define LIST_ENTRY(type)						\
andre@3: struct {								\
andre@3: 	struct type *le_next;	/* next element */			\
andre@3: 	struct type **le_prev;	/* address of previous next element */	\
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * List functions.
andre@3:  */
andre@3: #define	LIST_INIT(head) {						\
andre@3: 	(head)->lh_first = NULL;					\
andre@3: }
andre@3: 
andre@3: #define LIST_INSERT_AFTER(listelm, elm, field) {			\
andre@3: 	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\
andre@3: 		(listelm)->field.le_next->field.le_prev =		\
andre@3: 		    &(elm)->field.le_next;				\
andre@3: 	(listelm)->field.le_next = (elm);				\
andre@3: 	(elm)->field.le_prev = &(listelm)->field.le_next;		\
andre@3: }
andre@3: 
andre@3: #define LIST_INSERT_HEAD(head, elm, field) {				\
andre@3: 	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\
andre@3: 		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
andre@3: 	(head)->lh_first = (elm);					\
andre@3: 	(elm)->field.le_prev = &(head)->lh_first;			\
andre@3: }
andre@3: 
andre@3: #define LIST_REMOVE(elm, field) {					\
andre@3: 	if ((elm)->field.le_next != NULL)				\
andre@3: 		(elm)->field.le_next->field.le_prev = 			\
andre@3: 		    (elm)->field.le_prev;				\
andre@3: 	*(elm)->field.le_prev = (elm)->field.le_next;			\
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Tail queue definitions.
andre@3:  */
andre@3: #define TAILQ_HEAD(name, type)						\
andre@3: struct name {								\
andre@3: 	struct type *tqh_first;	/* first element */			\
andre@3: 	struct type **tqh_last;	/* addr of last next element */		\
andre@3: }
andre@3: 
andre@3: #define TAILQ_ENTRY(type)						\
andre@3: struct {								\
andre@3: 	struct type *tqe_next;	/* next element */			\
andre@3: 	struct type **tqe_prev;	/* address of previous next element */	\
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Tail queue functions.
andre@3:  */
andre@3: #define	TAILQ_INIT(head) {						\
andre@3: 	(head)->tqh_first = NULL;					\
andre@3: 	(head)->tqh_last = &(head)->tqh_first;				\
andre@3: }
andre@3: 
andre@3: #define TAILQ_INSERT_HEAD(head, elm, field) {				\
andre@3: 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
andre@3: 		(elm)->field.tqe_next->field.tqe_prev =			\
andre@3: 		    &(elm)->field.tqe_next;				\
andre@3: 	else								\
andre@3: 		(head)->tqh_last = &(elm)->field.tqe_next;		\
andre@3: 	(head)->tqh_first = (elm);					\
andre@3: 	(elm)->field.tqe_prev = &(head)->tqh_first;			\
andre@3: }
andre@3: 
andre@3: #define TAILQ_INSERT_TAIL(head, elm, field) {				\
andre@3: 	(elm)->field.tqe_next = NULL;					\
andre@3: 	(elm)->field.tqe_prev = (head)->tqh_last;			\
andre@3: 	*(head)->tqh_last = (elm);					\
andre@3: 	(head)->tqh_last = &(elm)->field.tqe_next;			\
andre@3: }
andre@3: 
andre@3: #define TAILQ_INSERT_AFTER(head, listelm, elm, field) {			\
andre@3: 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
andre@3: 		(elm)->field.tqe_next->field.tqe_prev = 		\
andre@3: 		    &(elm)->field.tqe_next;				\
andre@3: 	else								\
andre@3: 		(head)->tqh_last = &(elm)->field.tqe_next;		\
andre@3: 	(listelm)->field.tqe_next = (elm);				\
andre@3: 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
andre@3: }
andre@3: 
andre@3: #define TAILQ_REMOVE(head, elm, field) {				\
andre@3: 	if (((elm)->field.tqe_next) != NULL)				\
andre@3: 		(elm)->field.tqe_next->field.tqe_prev = 		\
andre@3: 		    (elm)->field.tqe_prev;				\
andre@3: 	else								\
andre@3: 		(head)->tqh_last = (elm)->field.tqe_prev;		\
andre@3: 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Circular queue definitions.
andre@3:  */
andre@3: #define CIRCLEQ_HEAD(name, type)					\
andre@3: struct name {								\
andre@3: 	struct type *cqh_first;		/* first element */		\
andre@3: 	struct type *cqh_last;		/* last element */		\
andre@3: }
andre@3: 
andre@3: #define CIRCLEQ_ENTRY(type)						\
andre@3: struct {								\
andre@3: 	struct type *cqe_next;		/* next element */		\
andre@3: 	struct type *cqe_prev;		/* previous element */		\
andre@3: }
andre@3: 
andre@3: /*
andre@3:  * Circular queue functions.
andre@3:  */
andre@3: #define	CIRCLEQ_INIT(head) {						\
andre@3: 	(head)->cqh_first = (void *)(head);				\
andre@3: 	(head)->cqh_last = (void *)(head);				\
andre@3: }
andre@3: 
andre@3: #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) {		\
andre@3: 	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
andre@3: 	(elm)->field.cqe_prev = (listelm);				\
andre@3: 	if ((listelm)->field.cqe_next == (void *)(head))		\
andre@3: 		(head)->cqh_last = (elm);				\
andre@3: 	else								\
andre@3: 		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
andre@3: 	(listelm)->field.cqe_next = (elm);				\
andre@3: }
andre@3: 
andre@3: #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) {		\
andre@3: 	(elm)->field.cqe_next = (listelm);				\
andre@3: 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
andre@3: 	if ((listelm)->field.cqe_prev == (void *)(head))		\
andre@3: 		(head)->cqh_first = (elm);				\
andre@3: 	else								\
andre@3: 		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
andre@3: 	(listelm)->field.cqe_prev = (elm);				\
andre@3: }
andre@3: 
andre@3: #define CIRCLEQ_INSERT_HEAD(head, elm, field) {				\
andre@3: 	(elm)->field.cqe_next = (head)->cqh_first;			\
andre@3: 	(elm)->field.cqe_prev = (void *)(head);				\
andre@3: 	if ((head)->cqh_last == (void *)(head))				\
andre@3: 		(head)->cqh_last = (elm);				\
andre@3: 	else								\
andre@3: 		(head)->cqh_first->field.cqe_prev = (elm);		\
andre@3: 	(head)->cqh_first = (elm);					\
andre@3: }
andre@3: 
andre@3: #define CIRCLEQ_INSERT_TAIL(head, elm, field) {				\
andre@3: 	(elm)->field.cqe_next = (void *)(head);				\
andre@3: 	(elm)->field.cqe_prev = (head)->cqh_last;			\
andre@3: 	if ((head)->cqh_first == (void *)(head))			\
andre@3: 		(head)->cqh_first = (elm);				\
andre@3: 	else								\
andre@3: 		(head)->cqh_last->field.cqe_next = (elm);		\
andre@3: 	(head)->cqh_last = (elm);					\
andre@3: }
andre@3: 
andre@3: #define	CIRCLEQ_REMOVE(head, elm, field) {				\
andre@3: 	if ((elm)->field.cqe_next == (void *)(head))			\
andre@3: 		(head)->cqh_last = (elm)->field.cqe_prev;		\
andre@3: 	else								\
andre@3: 		(elm)->field.cqe_next->field.cqe_prev =			\
andre@3: 		    (elm)->field.cqe_prev;				\
andre@3: 	if ((elm)->field.cqe_prev == (void *)(head))			\
andre@3: 		(head)->cqh_first = (elm)->field.cqe_next;		\
andre@3: 	else								\
andre@3: 		(elm)->field.cqe_prev->field.cqe_next =			\
andre@3: 		    (elm)->field.cqe_next;				\
andre@3: }
andre@3: #endif	/* !_QUEUE_H_ */