Blame misc/sys/queue.h

Packit 6c4009
/*
Packit 6c4009
 * Copyright (c) 1991, 1993
Packit 6c4009
 *	The Regents of the University of California.  All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Redistribution and use in source and binary forms, with or without
Packit 6c4009
 * modification, are permitted provided that the following conditions
Packit 6c4009
 * are met:
Packit 6c4009
 * 1. Redistributions of source code must retain the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer.
Packit 6c4009
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 6c4009
 *    notice, this list of conditions and the following disclaimer in the
Packit 6c4009
 *    documentation and/or other materials provided with the distribution.
Packit 6c4009
 * 3. Neither the name of the University nor the names of its contributors
Packit 6c4009
 *    may be used to endorse or promote products derived from this software
Packit 6c4009
 *    without specific prior written permission.
Packit 6c4009
 *
Packit 6c4009
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 6c4009
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 6c4009
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 6c4009
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 6c4009
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 6c4009
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 6c4009
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 6c4009
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 6c4009
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 6c4009
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 6c4009
 * SUCH DAMAGE.
Packit 6c4009
 *
Packit 6c4009
 *	@(#)queue.h	8.5 (Berkeley) 8/20/94
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#ifndef	_SYS_QUEUE_H_
Packit 6c4009
#define	_SYS_QUEUE_H_
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * This file defines five types of data structures: singly-linked lists,
Packit 6c4009
 * lists, simple queues, tail queues, and circular queues.
Packit 6c4009
 *
Packit 6c4009
 * A singly-linked list is headed by a single forward pointer. The
Packit 6c4009
 * elements are singly linked for minimum space and pointer manipulation
Packit 6c4009
 * overhead at the expense of O(n) removal for arbitrary elements. New
Packit 6c4009
 * elements can be added to the list after an existing element or at the
Packit 6c4009
 * head of the list.  Elements being removed from the head of the list
Packit 6c4009
 * should use the explicit macro for this purpose for optimum
Packit 6c4009
 * efficiency. A singly-linked list may only be traversed in the forward
Packit 6c4009
 * direction.  Singly-linked lists are ideal for applications with large
Packit 6c4009
 * datasets and few or no removals or for implementing a LIFO queue.
Packit 6c4009
 *
Packit 6c4009
 * A list is headed by a single forward pointer (or an array of forward
Packit 6c4009
 * pointers for a hash table header). The elements are doubly linked
Packit 6c4009
 * so that an arbitrary element can be removed without a need to
Packit 6c4009
 * traverse the list. New elements can be added to the list before
Packit 6c4009
 * or after an existing element or at the head of the list. A list
Packit 6c4009
 * may only be traversed in the forward direction.
Packit 6c4009
 *
Packit 6c4009
 * A simple queue is headed by a pair of pointers, one the head of the
Packit 6c4009
 * list and the other to the tail of the list. The elements are singly
Packit 6c4009
 * linked to save space, so elements can only be removed from the
Packit 6c4009
 * head of the list. New elements can be added to the list after
Packit 6c4009
 * an existing element, at the head of the list, or at the end of the
Packit 6c4009
 * list. A simple queue may only be traversed in the forward direction.
Packit 6c4009
 *
Packit 6c4009
 * A tail queue is headed by a pair of pointers, one to the head of the
Packit 6c4009
 * list and the other to the tail of the list. The elements are doubly
Packit 6c4009
 * linked so that an arbitrary element can be removed without a need to
Packit 6c4009
 * traverse the list. New elements can be added to the list before or
Packit 6c4009
 * after an existing element, at the head of the list, or at the end of
Packit 6c4009
 * the list. A tail queue may be traversed in either direction.
Packit 6c4009
 *
Packit 6c4009
 * A circle queue is headed by a pair of pointers, one to the head of the
Packit 6c4009
 * list and the other to the tail of the list. The elements are doubly
Packit 6c4009
 * linked so that an arbitrary element can be removed without a need to
Packit 6c4009
 * traverse the list. New elements can be added to the list before or after
Packit 6c4009
 * an existing element, at the head of the list, or at the end of the list.
Packit 6c4009
 * A circle queue may be traversed in either direction, but has a more
Packit 6c4009
 * complex end of list detection.
Packit 6c4009
 *
Packit 6c4009
 * For details on the use of these macros, see the queue(3) manual page.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * List definitions.
Packit 6c4009
 */
Packit 6c4009
#define	LIST_HEAD(name, type)						\
Packit 6c4009
struct name {								\
Packit 6c4009
	struct type *lh_first;	/* first element */			\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define	LIST_HEAD_INITIALIZER(head)					\
Packit 6c4009
	{ NULL }
Packit 6c4009
Packit 6c4009
#define	LIST_ENTRY(type)						\
Packit 6c4009
struct {								\
Packit 6c4009
	struct type *le_next;	/* next element */			\
Packit 6c4009
	struct type **le_prev;	/* address of previous next element */	\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * List functions.
Packit 6c4009
 */
Packit 6c4009
#define	LIST_INIT(head) do {						\
Packit 6c4009
	(head)->lh_first = NULL;					\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	LIST_INSERT_AFTER(listelm, elm, field) do {			\
Packit 6c4009
	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\
Packit 6c4009
		(listelm)->field.le_next->field.le_prev =		\
Packit 6c4009
		    &(elm)->field.le_next;				\
Packit 6c4009
	(listelm)->field.le_next = (elm);				\
Packit 6c4009
	(elm)->field.le_prev = &(listelm)->field.le_next;		\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
Packit 6c4009
	(elm)->field.le_prev = (listelm)->field.le_prev;		\
Packit 6c4009
	(elm)->field.le_next = (listelm);				\
Packit 6c4009
	*(listelm)->field.le_prev = (elm);				\
Packit 6c4009
	(listelm)->field.le_prev = &(elm)->field.le_next;		\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	LIST_INSERT_HEAD(head, elm, field) do {				\
Packit 6c4009
	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\
Packit 6c4009
		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
Packit 6c4009
	(head)->lh_first = (elm);					\
Packit 6c4009
	(elm)->field.le_prev = &(head)->lh_first;			\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	LIST_REMOVE(elm, field) do {					\
Packit 6c4009
	if ((elm)->field.le_next != NULL)				\
Packit 6c4009
		(elm)->field.le_next->field.le_prev = 			\
Packit 6c4009
		    (elm)->field.le_prev;				\
Packit 6c4009
	*(elm)->field.le_prev = (elm)->field.le_next;			\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	LIST_FOREACH(var, head, field)					\
Packit 6c4009
	for ((var) = ((head)->lh_first);				\
Packit 6c4009
		(var);							\
Packit 6c4009
		(var) = ((var)->field.le_next))
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * List access methods.
Packit 6c4009
 */
Packit 6c4009
#define	LIST_EMPTY(head)		((head)->lh_first == NULL)
Packit 6c4009
#define	LIST_FIRST(head)		((head)->lh_first)
Packit 6c4009
#define	LIST_NEXT(elm, field)		((elm)->field.le_next)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Singly-linked List definitions.
Packit 6c4009
 */
Packit 6c4009
#define	SLIST_HEAD(name, type)						\
Packit 6c4009
struct name {								\
Packit 6c4009
	struct type *slh_first;	/* first element */			\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define	SLIST_HEAD_INITIALIZER(head)					\
Packit 6c4009
	{ NULL }
Packit 6c4009
Packit 6c4009
#define	SLIST_ENTRY(type)						\
Packit 6c4009
struct {								\
Packit 6c4009
	struct type *sle_next;	/* next element */			\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Singly-linked List functions.
Packit 6c4009
 */
Packit 6c4009
#define	SLIST_INIT(head) do {						\
Packit 6c4009
	(head)->slh_first = NULL;					\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
Packit 6c4009
	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
Packit 6c4009
	(slistelm)->field.sle_next = (elm);				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SLIST_INSERT_HEAD(head, elm, field) do {			\
Packit 6c4009
	(elm)->field.sle_next = (head)->slh_first;			\
Packit 6c4009
	(head)->slh_first = (elm);					\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SLIST_REMOVE_HEAD(head, field) do {				\
Packit 6c4009
	(head)->slh_first = (head)->slh_first->field.sle_next;		\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SLIST_REMOVE(head, elm, type, field) do {			\
Packit 6c4009
	if ((head)->slh_first == (elm)) {				\
Packit 6c4009
		SLIST_REMOVE_HEAD((head), field);			\
Packit 6c4009
	}								\
Packit 6c4009
	else {								\
Packit 6c4009
		struct type *curelm = (head)->slh_first;		\
Packit 6c4009
		while(curelm->field.sle_next != (elm))			\
Packit 6c4009
			curelm = curelm->field.sle_next;		\
Packit 6c4009
		curelm->field.sle_next =				\
Packit 6c4009
		    curelm->field.sle_next->field.sle_next;		\
Packit 6c4009
	}								\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SLIST_FOREACH(var, head, field)					\
Packit 6c4009
	for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Singly-linked List access methods.
Packit 6c4009
 */
Packit 6c4009
#define	SLIST_EMPTY(head)	((head)->slh_first == NULL)
Packit 6c4009
#define	SLIST_FIRST(head)	((head)->slh_first)
Packit 6c4009
#define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Singly-linked Tail queue declarations.
Packit 6c4009
 */
Packit 6c4009
#define	STAILQ_HEAD(name, type)					\
Packit 6c4009
struct name {								\
Packit 6c4009
	struct type *stqh_first;	/* first element */			\
Packit 6c4009
	struct type **stqh_last;	/* addr of last next element */		\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define	STAILQ_HEAD_INITIALIZER(head)					\
Packit 6c4009
	{ NULL, &(head).stqh_first }
Packit 6c4009
Packit 6c4009
#define	STAILQ_ENTRY(type)						\
Packit 6c4009
struct {								\
Packit 6c4009
	struct type *stqe_next;	/* next element */			\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Singly-linked Tail queue functions.
Packit 6c4009
 */
Packit 6c4009
#define	STAILQ_INIT(head) do {						\
Packit 6c4009
	(head)->stqh_first = NULL;					\
Packit 6c4009
	(head)->stqh_last = &(head)->stqh_first;				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
Packit 6c4009
	if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)	\
Packit 6c4009
		(head)->stqh_last = &(elm)->field.stqe_next;		\
Packit 6c4009
	(head)->stqh_first = (elm);					\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
Packit 6c4009
	(elm)->field.stqe_next = NULL;					\
Packit 6c4009
	*(head)->stqh_last = (elm);					\
Packit 6c4009
	(head)->stqh_last = &(elm)->field.stqe_next;			\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
Packit 6c4009
	if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
Packit 6c4009
		(head)->stqh_last = &(elm)->field.stqe_next;		\
Packit 6c4009
	(listelm)->field.stqe_next = (elm);				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	STAILQ_REMOVE_HEAD(head, field) do {				\
Packit 6c4009
	if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) \
Packit 6c4009
		(head)->stqh_last = &(head)->stqh_first;			\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	STAILQ_REMOVE(head, elm, type, field) do {			\
Packit 6c4009
	if ((head)->stqh_first == (elm)) {				\
Packit 6c4009
		STAILQ_REMOVE_HEAD((head), field);			\
Packit 6c4009
	} else {							\
Packit 6c4009
		struct type *curelm = (head)->stqh_first;		\
Packit 6c4009
		while (curelm->field.stqe_next != (elm))			\
Packit 6c4009
			curelm = curelm->field.stqe_next;		\
Packit 6c4009
		if ((curelm->field.stqe_next =				\
Packit 6c4009
			curelm->field.stqe_next->field.stqe_next) == NULL) \
Packit 6c4009
			    (head)->stqh_last = &(curelm)->field.stqe_next; \
Packit 6c4009
	}								\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	STAILQ_FOREACH(var, head, field)				\
Packit 6c4009
	for ((var) = ((head)->stqh_first);				\
Packit 6c4009
		(var);							\
Packit 6c4009
		(var) = ((var)->field.stqe_next))
Packit 6c4009
Packit 6c4009
#define	STAILQ_CONCAT(head1, head2) do {				\
Packit 6c4009
	if (!STAILQ_EMPTY((head2))) {					\
Packit 6c4009
		*(head1)->stqh_last = (head2)->stqh_first;		\
Packit 6c4009
		(head1)->stqh_last = (head2)->stqh_last;		\
Packit 6c4009
		STAILQ_INIT((head2));					\
Packit 6c4009
	}								\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Singly-linked Tail queue access methods.
Packit 6c4009
 */
Packit 6c4009
#define	STAILQ_EMPTY(head)	((head)->stqh_first == NULL)
Packit 6c4009
#define	STAILQ_FIRST(head)	((head)->stqh_first)
Packit 6c4009
#define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Simple queue definitions.
Packit 6c4009
 */
Packit 6c4009
#define	SIMPLEQ_HEAD(name, type)					\
Packit 6c4009
struct name {								\
Packit 6c4009
	struct type *sqh_first;	/* first element */			\
Packit 6c4009
	struct type **sqh_last;	/* addr of last next element */		\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define	SIMPLEQ_HEAD_INITIALIZER(head)					\
Packit 6c4009
	{ NULL, &(head).sqh_first }
Packit 6c4009
Packit 6c4009
#define	SIMPLEQ_ENTRY(type)						\
Packit 6c4009
struct {								\
Packit 6c4009
	struct type *sqe_next;	/* next element */			\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Simple queue functions.
Packit 6c4009
 */
Packit 6c4009
#define	SIMPLEQ_INIT(head) do {						\
Packit 6c4009
	(head)->sqh_first = NULL;					\
Packit 6c4009
	(head)->sqh_last = &(head)->sqh_first;				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
Packit 6c4009
	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
Packit 6c4009
		(head)->sqh_last = &(elm)->field.sqe_next;		\
Packit 6c4009
	(head)->sqh_first = (elm);					\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
Packit 6c4009
	(elm)->field.sqe_next = NULL;					\
Packit 6c4009
	*(head)->sqh_last = (elm);					\
Packit 6c4009
	(head)->sqh_last = &(elm)->field.sqe_next;			\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
Packit 6c4009
	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
Packit 6c4009
		(head)->sqh_last = &(elm)->field.sqe_next;		\
Packit 6c4009
	(listelm)->field.sqe_next = (elm);				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SIMPLEQ_REMOVE_HEAD(head, field) do {				\
Packit 6c4009
	if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
Packit 6c4009
		(head)->sqh_last = &(head)->sqh_first;			\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SIMPLEQ_REMOVE(head, elm, type, field) do {			\
Packit 6c4009
	if ((head)->sqh_first == (elm)) {				\
Packit 6c4009
		SIMPLEQ_REMOVE_HEAD((head), field);			\
Packit 6c4009
	} else {							\
Packit 6c4009
		struct type *curelm = (head)->sqh_first;		\
Packit 6c4009
		while (curelm->field.sqe_next != (elm))			\
Packit 6c4009
			curelm = curelm->field.sqe_next;		\
Packit 6c4009
		if ((curelm->field.sqe_next =				\
Packit 6c4009
			curelm->field.sqe_next->field.sqe_next) == NULL) \
Packit 6c4009
			    (head)->sqh_last = &(curelm)->field.sqe_next; \
Packit 6c4009
	}								\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	SIMPLEQ_FOREACH(var, head, field)				\
Packit 6c4009
	for ((var) = ((head)->sqh_first);				\
Packit 6c4009
		(var);							\
Packit 6c4009
		(var) = ((var)->field.sqe_next))
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Simple queue access methods.
Packit 6c4009
 */
Packit 6c4009
#define	SIMPLEQ_EMPTY(head)		((head)->sqh_first == NULL)
Packit 6c4009
#define	SIMPLEQ_FIRST(head)		((head)->sqh_first)
Packit 6c4009
#define	SIMPLEQ_NEXT(elm, field)	((elm)->field.sqe_next)
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Tail queue definitions.
Packit 6c4009
 */
Packit 6c4009
#define	_TAILQ_HEAD(name, type, qual)					\
Packit 6c4009
struct name {								\
Packit 6c4009
	qual type *tqh_first;		/* first element */		\
Packit 6c4009
	qual type *qual *tqh_last;	/* addr of last next element */	\
Packit 6c4009
}
Packit 6c4009
#define TAILQ_HEAD(name, type)	_TAILQ_HEAD(name, struct type,)
Packit 6c4009
Packit 6c4009
#define	TAILQ_HEAD_INITIALIZER(head)					\
Packit 6c4009
	{ NULL, &(head).tqh_first }
Packit 6c4009
Packit 6c4009
#define	_TAILQ_ENTRY(type, qual)					\
Packit 6c4009
struct {								\
Packit 6c4009
	qual type *tqe_next;		/* next element */		\
Packit 6c4009
	qual type *qual *tqe_prev;	/* address of previous next element */\
Packit 6c4009
}
Packit 6c4009
#define TAILQ_ENTRY(type)	_TAILQ_ENTRY(struct type,)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Tail queue functions.
Packit 6c4009
 */
Packit 6c4009
#define	TAILQ_INIT(head) do {						\
Packit 6c4009
	(head)->tqh_first = NULL;					\
Packit 6c4009
	(head)->tqh_last = &(head)->tqh_first;				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	TAILQ_INSERT_HEAD(head, elm, field) do {			\
Packit 6c4009
	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\
Packit 6c4009
		(head)->tqh_first->field.tqe_prev =			\
Packit 6c4009
		    &(elm)->field.tqe_next;				\
Packit 6c4009
	else								\
Packit 6c4009
		(head)->tqh_last = &(elm)->field.tqe_next;		\
Packit 6c4009
	(head)->tqh_first = (elm);					\
Packit 6c4009
	(elm)->field.tqe_prev = &(head)->tqh_first;			\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	TAILQ_INSERT_TAIL(head, elm, field) do {			\
Packit 6c4009
	(elm)->field.tqe_next = NULL;					\
Packit 6c4009
	(elm)->field.tqe_prev = (head)->tqh_last;			\
Packit 6c4009
	*(head)->tqh_last = (elm);					\
Packit 6c4009
	(head)->tqh_last = &(elm)->field.tqe_next;			\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
Packit 6c4009
	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
Packit 6c4009
		(elm)->field.tqe_next->field.tqe_prev = 		\
Packit 6c4009
		    &(elm)->field.tqe_next;				\
Packit 6c4009
	else								\
Packit 6c4009
		(head)->tqh_last = &(elm)->field.tqe_next;		\
Packit 6c4009
	(listelm)->field.tqe_next = (elm);				\
Packit 6c4009
	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
Packit 6c4009
	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
Packit 6c4009
	(elm)->field.tqe_next = (listelm);				\
Packit 6c4009
	*(listelm)->field.tqe_prev = (elm);				\
Packit 6c4009
	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	TAILQ_REMOVE(head, elm, field) do {				\
Packit 6c4009
	if (((elm)->field.tqe_next) != NULL)				\
Packit 6c4009
		(elm)->field.tqe_next->field.tqe_prev = 		\
Packit 6c4009
		    (elm)->field.tqe_prev;				\
Packit 6c4009
	else								\
Packit 6c4009
		(head)->tqh_last = (elm)->field.tqe_prev;		\
Packit 6c4009
	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	TAILQ_FOREACH(var, head, field)					\
Packit 6c4009
	for ((var) = ((head)->tqh_first);				\
Packit 6c4009
		(var);							\
Packit 6c4009
		(var) = ((var)->field.tqe_next))
Packit 6c4009
Packit 6c4009
#define	TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
Packit 6c4009
	for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));	\
Packit 6c4009
		(var);							\
Packit 6c4009
		(var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
Packit 6c4009
Packit 6c4009
#define	TAILQ_CONCAT(head1, head2, field) do {				\
Packit 6c4009
	if (!TAILQ_EMPTY(head2)) {					\
Packit 6c4009
		*(head1)->tqh_last = (head2)->tqh_first;		\
Packit 6c4009
		(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last;	\
Packit 6c4009
		(head1)->tqh_last = (head2)->tqh_last;			\
Packit 6c4009
		TAILQ_INIT((head2));					\
Packit 6c4009
	}								\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Tail queue access methods.
Packit 6c4009
 */
Packit 6c4009
#define	TAILQ_EMPTY(head)		((head)->tqh_first == NULL)
Packit 6c4009
#define	TAILQ_FIRST(head)		((head)->tqh_first)
Packit 6c4009
#define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
Packit 6c4009
Packit 6c4009
#define	TAILQ_LAST(head, headname) \
Packit 6c4009
	(*(((struct headname *)((head)->tqh_last))->tqh_last))
Packit 6c4009
#define	TAILQ_PREV(elm, headname, field) \
Packit 6c4009
	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
Packit 6c4009
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Circular queue definitions.
Packit 6c4009
 */
Packit 6c4009
#define	CIRCLEQ_HEAD(name, type)					\
Packit 6c4009
struct name {								\
Packit 6c4009
	struct type *cqh_first;		/* first element */		\
Packit 6c4009
	struct type *cqh_last;		/* last element */		\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define	CIRCLEQ_HEAD_INITIALIZER(head)					\
Packit 6c4009
	{ (void *)&head, (void *)&head }
Packit 6c4009
Packit 6c4009
#define	CIRCLEQ_ENTRY(type)						\
Packit 6c4009
struct {								\
Packit 6c4009
	struct type *cqe_next;		/* next element */		\
Packit 6c4009
	struct type *cqe_prev;		/* previous element */		\
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Circular queue functions.
Packit 6c4009
 */
Packit 6c4009
#define	CIRCLEQ_INIT(head) do {						\
Packit 6c4009
	(head)->cqh_first = (void *)(head);				\
Packit 6c4009
	(head)->cqh_last = (void *)(head);				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
Packit 6c4009
	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
Packit 6c4009
	(elm)->field.cqe_prev = (listelm);				\
Packit 6c4009
	if ((listelm)->field.cqe_next == (void *)(head))		\
Packit 6c4009
		(head)->cqh_last = (elm);				\
Packit 6c4009
	else								\
Packit 6c4009
		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
Packit 6c4009
	(listelm)->field.cqe_next = (elm);				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\
Packit 6c4009
	(elm)->field.cqe_next = (listelm);				\
Packit 6c4009
	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
Packit 6c4009
	if ((listelm)->field.cqe_prev == (void *)(head))		\
Packit 6c4009
		(head)->cqh_first = (elm);				\
Packit 6c4009
	else								\
Packit 6c4009
		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
Packit 6c4009
	(listelm)->field.cqe_prev = (elm);				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
Packit 6c4009
	(elm)->field.cqe_next = (head)->cqh_first;			\
Packit 6c4009
	(elm)->field.cqe_prev = (void *)(head);				\
Packit 6c4009
	if ((head)->cqh_last == (void *)(head))				\
Packit 6c4009
		(head)->cqh_last = (elm);				\
Packit 6c4009
	else								\
Packit 6c4009
		(head)->cqh_first->field.cqe_prev = (elm);		\
Packit 6c4009
	(head)->cqh_first = (elm);					\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
Packit 6c4009
	(elm)->field.cqe_next = (void *)(head);				\
Packit 6c4009
	(elm)->field.cqe_prev = (head)->cqh_last;			\
Packit 6c4009
	if ((head)->cqh_first == (void *)(head))			\
Packit 6c4009
		(head)->cqh_first = (elm);				\
Packit 6c4009
	else								\
Packit 6c4009
		(head)->cqh_last->field.cqe_next = (elm);		\
Packit 6c4009
	(head)->cqh_last = (elm);					\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	CIRCLEQ_REMOVE(head, elm, field) do {				\
Packit 6c4009
	if ((elm)->field.cqe_next == (void *)(head))			\
Packit 6c4009
		(head)->cqh_last = (elm)->field.cqe_prev;		\
Packit 6c4009
	else								\
Packit 6c4009
		(elm)->field.cqe_next->field.cqe_prev =			\
Packit 6c4009
		    (elm)->field.cqe_prev;				\
Packit 6c4009
	if ((elm)->field.cqe_prev == (void *)(head))			\
Packit 6c4009
		(head)->cqh_first = (elm)->field.cqe_next;		\
Packit 6c4009
	else								\
Packit 6c4009
		(elm)->field.cqe_prev->field.cqe_next =			\
Packit 6c4009
		    (elm)->field.cqe_next;				\
Packit 6c4009
} while (/*CONSTCOND*/0)
Packit 6c4009
Packit 6c4009
#define	CIRCLEQ_FOREACH(var, head, field)				\
Packit 6c4009
	for ((var) = ((head)->cqh_first);				\
Packit 6c4009
		(var) != (const void *)(head);				\
Packit 6c4009
		(var) = ((var)->field.cqe_next))
Packit 6c4009
Packit 6c4009
#define	CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
Packit 6c4009
	for ((var) = ((head)->cqh_last);				\
Packit 6c4009
		(var) != (const void *)(head);				\
Packit 6c4009
		(var) = ((var)->field.cqe_prev))
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * Circular queue access methods.
Packit 6c4009
 */
Packit 6c4009
#define	CIRCLEQ_EMPTY(head)		((head)->cqh_first == (void *)(head))
Packit 6c4009
#define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
Packit 6c4009
#define	CIRCLEQ_LAST(head)		((head)->cqh_last)
Packit 6c4009
#define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
Packit 6c4009
#define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
Packit 6c4009
Packit 6c4009
#define CIRCLEQ_LOOP_NEXT(head, elm, field)				\
Packit 6c4009
	(((elm)->field.cqe_next == (void *)(head))			\
Packit 6c4009
	    ? ((head)->cqh_first)					\
Packit 6c4009
	    : (elm->field.cqe_next))
Packit 6c4009
#define CIRCLEQ_LOOP_PREV(head, elm, field)				\
Packit 6c4009
	(((elm)->field.cqe_prev == (void *)(head))			\
Packit 6c4009
	    ? ((head)->cqh_last)					\
Packit 6c4009
	    : (elm->field.cqe_prev))
Packit 6c4009
Packit 6c4009
#endif	/* sys/queue.h */