Blame include/sys/queue.h

Kylie McClain b61f59
/*	$NetBSD: queue.h,v 1.68 2014/11/19 08:10:01 uebayasi Exp $	*/
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Copyright (c) 1991, 1993
Kylie McClain b61f59
 *	The Regents of the University of California.  All rights reserved.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * Redistribution and use in source and binary forms, with or without
Kylie McClain b61f59
 * modification, are permitted provided that the following conditions
Kylie McClain b61f59
 * are met:
Kylie McClain b61f59
 * 1. Redistributions of source code must retain the above copyright
Kylie McClain b61f59
 *    notice, this list of conditions and the following disclaimer.
Kylie McClain b61f59
 * 2. Redistributions in binary form must reproduce the above copyright
Kylie McClain b61f59
 *    notice, this list of conditions and the following disclaimer in the
Kylie McClain b61f59
 *    documentation and/or other materials provided with the distribution.
Kylie McClain b61f59
 * 3. Neither the name of the University nor the names of its contributors
Kylie McClain b61f59
 *    may be used to endorse or promote products derived from this software
Kylie McClain b61f59
 *    without specific prior written permission.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Kylie McClain b61f59
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Kylie McClain b61f59
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Kylie McClain b61f59
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Kylie McClain b61f59
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Kylie McClain b61f59
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Kylie McClain b61f59
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Kylie McClain b61f59
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Kylie McClain b61f59
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Kylie McClain b61f59
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Kylie McClain b61f59
 * SUCH DAMAGE.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 *	@(#)queue.h	8.5 (Berkeley) 8/20/94
Kylie McClain b61f59
 */
Kylie McClain b61f59
Kylie McClain b61f59
#ifndef	_SYS_QUEUE_H_
Kylie McClain b61f59
#define	_SYS_QUEUE_H_
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * This file defines five types of data structures: singly-linked lists,
Kylie McClain b61f59
 * lists, simple queues, tail queues, and circular queues.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * A singly-linked list is headed by a single forward pointer. The
Kylie McClain b61f59
 * elements are singly linked for minimum space and pointer manipulation
Kylie McClain b61f59
 * overhead at the expense of O(n) removal for arbitrary elements. New
Kylie McClain b61f59
 * elements can be added to the list after an existing element or at the
Kylie McClain b61f59
 * head of the list.  Elements being removed from the head of the list
Kylie McClain b61f59
 * should use the explicit macro for this purpose for optimum
Kylie McClain b61f59
 * efficiency. A singly-linked list may only be traversed in the forward
Kylie McClain b61f59
 * direction.  Singly-linked lists are ideal for applications with large
Kylie McClain b61f59
 * datasets and few or no removals or for implementing a LIFO queue.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * A list is headed by a single forward pointer (or an array of forward
Kylie McClain b61f59
 * pointers for a hash table header). The elements are doubly linked
Kylie McClain b61f59
 * so that an arbitrary element can be removed without a need to
Kylie McClain b61f59
 * traverse the list. New elements can be added to the list before
Kylie McClain b61f59
 * or after an existing element or at the head of the list. A list
Kylie McClain b61f59
 * may only be traversed in the forward direction.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * A simple queue is headed by a pair of pointers, one the head of the
Kylie McClain b61f59
 * list and the other to the tail of the list. The elements are singly
Kylie McClain b61f59
 * linked to save space, so elements can only be removed from the
Kylie McClain b61f59
 * head of the list. New elements can be added to the list after
Kylie McClain b61f59
 * an existing element, at the head of the list, or at the end of the
Kylie McClain b61f59
 * list. A simple queue may only be traversed in the forward direction.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * A tail queue is headed by a pair of pointers, one to the head of the
Kylie McClain b61f59
 * list and the other to the tail of the list. The elements are doubly
Kylie McClain b61f59
 * linked so that an arbitrary element can be removed without a need to
Kylie McClain b61f59
 * traverse the list. New elements can be added to the list before or
Kylie McClain b61f59
 * after an existing element, at the head of the list, or at the end of
Kylie McClain b61f59
 * the list. A tail queue may be traversed in either direction.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * A circle queue is headed by a pair of pointers, one to the head of the
Kylie McClain b61f59
 * list and the other to the tail of the list. The elements are doubly
Kylie McClain b61f59
 * linked so that an arbitrary element can be removed without a need to
Kylie McClain b61f59
 * traverse the list. New elements can be added to the list before or after
Kylie McClain b61f59
 * an existing element, at the head of the list, or at the end of the list.
Kylie McClain b61f59
 * A circle queue may be traversed in either direction, but has a more
Kylie McClain b61f59
 * complex end of list detection.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * For details on the use of these macros, see the queue(3) manual page.
Kylie McClain b61f59
 */
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Include the definition of NULL only on NetBSD because sys/null.h
Kylie McClain b61f59
 * is not available elsewhere.  This conditional makes the header
Kylie McClain b61f59
 * portable and it can simply be dropped verbatim into any system.
Kylie McClain b61f59
 * The caveat is that on other systems some other header
Kylie McClain b61f59
 * must provide NULL before the macros can be used.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#ifdef __NetBSD__
Kylie McClain b61f59
#include <sys/null.h>
Kylie McClain b61f59
#endif
Kylie McClain b61f59
Kylie McClain b61f59
#if defined(QUEUEDEBUG)
Kylie McClain b61f59
# if defined(_KERNEL)
Kylie McClain b61f59
#  define QUEUEDEBUG_ABORT(...) panic(__VA_ARGS__)
Kylie McClain b61f59
# else
Kylie McClain b61f59
#  include <err.h>
Kylie McClain b61f59
#  define QUEUEDEBUG_ABORT(...) err(1, __VA_ARGS__)
Kylie McClain b61f59
# endif
Kylie McClain b61f59
#endif
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Singly-linked List definitions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	SLIST_HEAD(name, type)						\
Kylie McClain b61f59
struct name {								\
Kylie McClain b61f59
	struct type *slh_first;	/* first element */			\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define	SLIST_HEAD_INITIALIZER(head)					\
Kylie McClain b61f59
	{ NULL }
Kylie McClain b61f59
Kylie McClain b61f59
#define	SLIST_ENTRY(type)						\
Kylie McClain b61f59
struct {								\
Kylie McClain b61f59
	struct type *sle_next;	/* next element */			\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Singly-linked List access methods.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	SLIST_FIRST(head)	((head)->slh_first)
Kylie McClain b61f59
#define	SLIST_END(head)		NULL
Kylie McClain b61f59
#define	SLIST_EMPTY(head)	((head)->slh_first == NULL)
Kylie McClain b61f59
#define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SLIST_FOREACH(var, head, field)					\
Kylie McClain b61f59
	for((var) = (head)->slh_first;					\
Kylie McClain b61f59
	    (var) != SLIST_END(head);					\
Kylie McClain b61f59
	    (var) = (var)->field.sle_next)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SLIST_FOREACH_SAFE(var, head, field, tvar)			\
Kylie McClain b61f59
	for ((var) = SLIST_FIRST((head));				\
Kylie McClain b61f59
	    (var) != SLIST_END(head) &&					\
Kylie McClain b61f59
	    ((tvar) = SLIST_NEXT((var), field), 1);			\
Kylie McClain b61f59
	    (var) = (tvar))
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Singly-linked List functions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	SLIST_INIT(head) do {						\
Kylie McClain b61f59
	(head)->slh_first = SLIST_END(head);				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\
Kylie McClain b61f59
	(elm)->field.sle_next = (slistelm)->field.sle_next;		\
Kylie McClain b61f59
	(slistelm)->field.sle_next = (elm);				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SLIST_INSERT_HEAD(head, elm, field) do {			\
Kylie McClain b61f59
	(elm)->field.sle_next = (head)->slh_first;			\
Kylie McClain b61f59
	(head)->slh_first = (elm);					\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SLIST_REMOVE_AFTER(slistelm, field) do {			\
Kylie McClain b61f59
	(slistelm)->field.sle_next =					\
Kylie McClain b61f59
	    SLIST_NEXT(SLIST_NEXT((slistelm), field), field);		\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SLIST_REMOVE_HEAD(head, field) do {				\
Kylie McClain b61f59
	(head)->slh_first = (head)->slh_first->field.sle_next;		\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SLIST_REMOVE(head, elm, type, field) do {			\
Kylie McClain b61f59
	if ((head)->slh_first == (elm)) {				\
Kylie McClain b61f59
		SLIST_REMOVE_HEAD((head), field);			\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	else {								\
Kylie McClain b61f59
		struct type *curelm = (head)->slh_first;		\
Kylie McClain b61f59
		while(curelm->field.sle_next != (elm))			\
Kylie McClain b61f59
			curelm = curelm->field.sle_next;		\
Kylie McClain b61f59
		curelm->field.sle_next =				\
Kylie McClain b61f59
		    curelm->field.sle_next->field.sle_next;		\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * List definitions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	LIST_HEAD(name, type)						\
Kylie McClain b61f59
struct name {								\
Kylie McClain b61f59
	struct type *lh_first;	/* first element */			\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_HEAD_INITIALIZER(head)					\
Kylie McClain b61f59
	{ NULL }
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_ENTRY(type)						\
Kylie McClain b61f59
struct {								\
Kylie McClain b61f59
	struct type *le_next;	/* next element */			\
Kylie McClain b61f59
	struct type **le_prev;	/* address of previous next element */	\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * List access methods.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	LIST_FIRST(head)		((head)->lh_first)
Kylie McClain b61f59
#define	LIST_END(head)			NULL
Kylie McClain b61f59
#define	LIST_EMPTY(head)		((head)->lh_first == LIST_END(head))
Kylie McClain b61f59
#define	LIST_NEXT(elm, field)		((elm)->field.le_next)
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_FOREACH(var, head, field)					\
Kylie McClain b61f59
	for ((var) = ((head)->lh_first);				\
Kylie McClain b61f59
	    (var) != LIST_END(head);					\
Kylie McClain b61f59
	    (var) = ((var)->field.le_next))
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_FOREACH_SAFE(var, head, field, tvar)			\
Kylie McClain b61f59
	for ((var) = LIST_FIRST((head));				\
Kylie McClain b61f59
	    (var) != LIST_END(head) &&					\
Kylie McClain b61f59
	    ((tvar) = LIST_NEXT((var), field), 1);			\
Kylie McClain b61f59
	    (var) = (tvar))
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_MOVE(head1, head2) do {					\
Kylie McClain b61f59
	LIST_INIT((head2));						\
Kylie McClain b61f59
	if (!LIST_EMPTY((head1))) {					\
Kylie McClain b61f59
		(head2)->lh_first = (head1)->lh_first;			\
Kylie McClain b61f59
		LIST_INIT((head1));					\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * List functions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#if defined(QUEUEDEBUG)
Kylie McClain b61f59
#define	QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field)			\
Kylie McClain b61f59
	if ((head)->lh_first &&						\
Kylie McClain b61f59
	    (head)->lh_first->field.le_prev != &(head)->lh_first)	\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("LIST_INSERT_HEAD %p %s:%d", (head),	\
Kylie McClain b61f59
		    __FILE__, __LINE__);
Kylie McClain b61f59
#define	QUEUEDEBUG_LIST_OP(elm, field)					\
Kylie McClain b61f59
	if ((elm)->field.le_next &&					\
Kylie McClain b61f59
	    (elm)->field.le_next->field.le_prev !=			\
Kylie McClain b61f59
	    &(elm)->field.le_next)					\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("LIST_* forw %p %s:%d", (elm),		\
Kylie McClain b61f59
		    __FILE__, __LINE__);				\
Kylie McClain b61f59
	if (*(elm)->field.le_prev != (elm))				\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("LIST_* back %p %s:%d", (elm),		\
Kylie McClain b61f59
		    __FILE__, __LINE__);
Kylie McClain b61f59
#define	QUEUEDEBUG_LIST_POSTREMOVE(elm, field)				\
Kylie McClain b61f59
	(elm)->field.le_next = (void *)1L;				\
Kylie McClain b61f59
	(elm)->field.le_prev = (void *)1L;
Kylie McClain b61f59
#else
Kylie McClain b61f59
#define	QUEUEDEBUG_LIST_INSERT_HEAD(head, elm, field)
Kylie McClain b61f59
#define	QUEUEDEBUG_LIST_OP(elm, field)
Kylie McClain b61f59
#define	QUEUEDEBUG_LIST_POSTREMOVE(elm, field)
Kylie McClain b61f59
#endif
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_INIT(head) do {						\
Kylie McClain b61f59
	(head)->lh_first = LIST_END(head);				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_INSERT_AFTER(listelm, elm, field) do {			\
Kylie McClain b61f59
	QUEUEDEBUG_LIST_OP((listelm), field)				\
Kylie McClain b61f59
	if (((elm)->field.le_next = (listelm)->field.le_next) != 	\
Kylie McClain b61f59
	    LIST_END(head))						\
Kylie McClain b61f59
		(listelm)->field.le_next->field.le_prev =		\
Kylie McClain b61f59
		    &(elm)->field.le_next;				\
Kylie McClain b61f59
	(listelm)->field.le_next = (elm);				\
Kylie McClain b61f59
	(elm)->field.le_prev = &(listelm)->field.le_next;		\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\
Kylie McClain b61f59
	QUEUEDEBUG_LIST_OP((listelm), field)				\
Kylie McClain b61f59
	(elm)->field.le_prev = (listelm)->field.le_prev;		\
Kylie McClain b61f59
	(elm)->field.le_next = (listelm);				\
Kylie McClain b61f59
	*(listelm)->field.le_prev = (elm);				\
Kylie McClain b61f59
	(listelm)->field.le_prev = &(elm)->field.le_next;		\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_INSERT_HEAD(head, elm, field) do {				\
Kylie McClain b61f59
	QUEUEDEBUG_LIST_INSERT_HEAD((head), (elm), field)		\
Kylie McClain b61f59
	if (((elm)->field.le_next = (head)->lh_first) != LIST_END(head))\
Kylie McClain b61f59
		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
Kylie McClain b61f59
	(head)->lh_first = (elm);					\
Kylie McClain b61f59
	(elm)->field.le_prev = &(head)->lh_first;			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	LIST_REMOVE(elm, field) do {					\
Kylie McClain b61f59
	QUEUEDEBUG_LIST_OP((elm), field)				\
Kylie McClain b61f59
	if ((elm)->field.le_next != NULL)				\
Kylie McClain b61f59
		(elm)->field.le_next->field.le_prev = 			\
Kylie McClain b61f59
		    (elm)->field.le_prev;				\
Kylie McClain b61f59
	*(elm)->field.le_prev = (elm)->field.le_next;			\
Kylie McClain b61f59
	QUEUEDEBUG_LIST_POSTREMOVE((elm), field)			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define LIST_REPLACE(elm, elm2, field) do {				\
Kylie McClain b61f59
	if (((elm2)->field.le_next = (elm)->field.le_next) != NULL)	\
Kylie McClain b61f59
		(elm2)->field.le_next->field.le_prev =			\
Kylie McClain b61f59
		    &(elm2)->field.le_next;				\
Kylie McClain b61f59
	(elm2)->field.le_prev = (elm)->field.le_prev;			\
Kylie McClain b61f59
	*(elm2)->field.le_prev = (elm2);				\
Kylie McClain b61f59
	QUEUEDEBUG_LIST_POSTREMOVE((elm), field)			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Simple queue definitions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	SIMPLEQ_HEAD(name, type)					\
Kylie McClain b61f59
struct name {								\
Kylie McClain b61f59
	struct type *sqh_first;	/* first element */			\
Kylie McClain b61f59
	struct type **sqh_last;	/* addr of last next element */		\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_HEAD_INITIALIZER(head)					\
Kylie McClain b61f59
	{ NULL, &(head).sqh_first }
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_ENTRY(type)						\
Kylie McClain b61f59
struct {								\
Kylie McClain b61f59
	struct type *sqe_next;	/* next element */			\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Simple queue access methods.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	SIMPLEQ_FIRST(head)		((head)->sqh_first)
Kylie McClain b61f59
#define	SIMPLEQ_END(head)		NULL
Kylie McClain b61f59
#define	SIMPLEQ_EMPTY(head)		((head)->sqh_first == SIMPLEQ_END(head))
Kylie McClain b61f59
#define	SIMPLEQ_NEXT(elm, field)	((elm)->field.sqe_next)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_FOREACH(var, head, field)				\
Kylie McClain b61f59
	for ((var) = ((head)->sqh_first);				\
Kylie McClain b61f59
	    (var) != SIMPLEQ_END(head);					\
Kylie McClain b61f59
	    (var) = ((var)->field.sqe_next))
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_FOREACH_SAFE(var, head, field, next)			\
Kylie McClain b61f59
	for ((var) = ((head)->sqh_first);				\
Kylie McClain b61f59
	    (var) != SIMPLEQ_END(head) &&				\
Kylie McClain b61f59
	    ((next = ((var)->field.sqe_next)), 1);			\
Kylie McClain b61f59
	    (var) = (next))
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Simple queue functions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	SIMPLEQ_INIT(head) do {						\
Kylie McClain b61f59
	(head)->sqh_first = NULL;					\
Kylie McClain b61f59
	(head)->sqh_last = &(head)->sqh_first;				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\
Kylie McClain b61f59
	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\
Kylie McClain b61f59
		(head)->sqh_last = &(elm)->field.sqe_next;		\
Kylie McClain b61f59
	(head)->sqh_first = (elm);					\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\
Kylie McClain b61f59
	(elm)->field.sqe_next = NULL;					\
Kylie McClain b61f59
	*(head)->sqh_last = (elm);					\
Kylie McClain b61f59
	(head)->sqh_last = &(elm)->field.sqe_next;			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
Kylie McClain b61f59
	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
Kylie McClain b61f59
		(head)->sqh_last = &(elm)->field.sqe_next;		\
Kylie McClain b61f59
	(listelm)->field.sqe_next = (elm);				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_REMOVE_HEAD(head, field) do {				\
Kylie McClain b61f59
	if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
Kylie McClain b61f59
		(head)->sqh_last = &(head)->sqh_first;			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define SIMPLEQ_REMOVE_AFTER(head, elm, field) do {			\
Kylie McClain b61f59
	if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \
Kylie McClain b61f59
	    == NULL)							\
Kylie McClain b61f59
		(head)->sqh_last = &(elm)->field.sqe_next;		\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_REMOVE(head, elm, type, field) do {			\
Kylie McClain b61f59
	if ((head)->sqh_first == (elm)) {				\
Kylie McClain b61f59
		SIMPLEQ_REMOVE_HEAD((head), field);			\
Kylie McClain b61f59
	} else {							\
Kylie McClain b61f59
		struct type *curelm = (head)->sqh_first;		\
Kylie McClain b61f59
		while (curelm->field.sqe_next != (elm))			\
Kylie McClain b61f59
			curelm = curelm->field.sqe_next;		\
Kylie McClain b61f59
		if ((curelm->field.sqe_next =				\
Kylie McClain b61f59
			curelm->field.sqe_next->field.sqe_next) == NULL) \
Kylie McClain b61f59
			    (head)->sqh_last = &(curelm)->field.sqe_next; \
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_CONCAT(head1, head2) do {				\
Kylie McClain b61f59
	if (!SIMPLEQ_EMPTY((head2))) {					\
Kylie McClain b61f59
		*(head1)->sqh_last = (head2)->sqh_first;		\
Kylie McClain b61f59
		(head1)->sqh_last = (head2)->sqh_last;		\
Kylie McClain b61f59
		SIMPLEQ_INIT((head2));					\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	SIMPLEQ_LAST(head, type, field)					\
Kylie McClain b61f59
	(SIMPLEQ_EMPTY((head)) ?						\
Kylie McClain b61f59
		NULL :							\
Kylie McClain b61f59
	        ((struct type *)(void *)				\
Kylie McClain b61f59
		((char *)((head)->sqh_last) - offsetof(struct type, field))))
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Tail queue definitions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	_TAILQ_HEAD(name, type, qual)					\
Kylie McClain b61f59
struct name {								\
Kylie McClain b61f59
	qual type *tqh_first;		/* first element */		\
Kylie McClain b61f59
	qual type *qual *tqh_last;	/* addr of last next element */	\
Kylie McClain b61f59
}
Kylie McClain b61f59
#define TAILQ_HEAD(name, type)	_TAILQ_HEAD(name, struct type,)
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_HEAD_INITIALIZER(head)					\
Kylie McClain b61f59
	{ TAILQ_END(head), &(head).tqh_first }
Kylie McClain b61f59
Kylie McClain b61f59
#define	_TAILQ_ENTRY(type, qual)					\
Kylie McClain b61f59
struct {								\
Kylie McClain b61f59
	qual type *tqe_next;		/* next element */		\
Kylie McClain b61f59
	qual type *qual *tqe_prev;	/* address of previous next element */\
Kylie McClain b61f59
}
Kylie McClain b61f59
#define TAILQ_ENTRY(type)	_TAILQ_ENTRY(struct type,)
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Tail queue access methods.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	TAILQ_FIRST(head)		((head)->tqh_first)
Kylie McClain b61f59
#define	TAILQ_END(head)			(NULL)
Kylie McClain b61f59
#define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
Kylie McClain b61f59
#define	TAILQ_LAST(head, headname) \
Kylie McClain b61f59
	(*(((struct headname *)((head)->tqh_last))->tqh_last))
Kylie McClain b61f59
#define	TAILQ_PREV(elm, headname, field) \
Kylie McClain b61f59
	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
Kylie McClain b61f59
#define	TAILQ_EMPTY(head)		(TAILQ_FIRST(head) == TAILQ_END(head))
Kylie McClain b61f59
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_FOREACH(var, head, field)					\
Kylie McClain b61f59
	for ((var) = ((head)->tqh_first);				\
Kylie McClain b61f59
	    (var) != TAILQ_END(head);					\
Kylie McClain b61f59
	    (var) = ((var)->field.tqe_next))
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_FOREACH_SAFE(var, head, field, next)			\
Kylie McClain b61f59
	for ((var) = ((head)->tqh_first);				\
Kylie McClain b61f59
	    (var) != TAILQ_END(head) &&					\
Kylie McClain b61f59
	    ((next) = TAILQ_NEXT(var, field), 1); (var) = (next))
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_FOREACH_REVERSE(var, head, headname, field)		\
Kylie McClain b61f59
	for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last));\
Kylie McClain b61f59
	    (var) != TAILQ_END(head);					\
Kylie McClain b61f59
	    (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last)))
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, prev)	\
Kylie McClain b61f59
	for ((var) = TAILQ_LAST((head), headname);			\
Kylie McClain b61f59
	    (var) != TAILQ_END(head) && 				\
Kylie McClain b61f59
	    ((prev) = TAILQ_PREV((var), headname, field), 1); (var) = (prev))
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Tail queue functions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#if defined(QUEUEDEBUG)
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field)			\
Kylie McClain b61f59
	if ((head)->tqh_first &&					\
Kylie McClain b61f59
	    (head)->tqh_first->field.tqe_prev != &(head)->tqh_first)	\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("TAILQ_INSERT_HEAD %p %s:%d", (head),	\
Kylie McClain b61f59
		    __FILE__, __LINE__);
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field)			\
Kylie McClain b61f59
	if (*(head)->tqh_last != NULL)					\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("TAILQ_INSERT_TAIL %p %s:%d", (head),	\
Kylie McClain b61f59
		    __FILE__, __LINE__);
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_OP(elm, field)					\
Kylie McClain b61f59
	if ((elm)->field.tqe_next &&					\
Kylie McClain b61f59
	    (elm)->field.tqe_next->field.tqe_prev !=			\
Kylie McClain b61f59
	    &(elm)->field.tqe_next)					\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("TAILQ_* forw %p %s:%d", (elm),	\
Kylie McClain b61f59
		    __FILE__, __LINE__);				\
Kylie McClain b61f59
	if (*(elm)->field.tqe_prev != (elm))				\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("TAILQ_* back %p %s:%d", (elm),	\
Kylie McClain b61f59
		    __FILE__, __LINE__);
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field)			\
Kylie McClain b61f59
	if ((elm)->field.tqe_next == NULL &&				\
Kylie McClain b61f59
	    (head)->tqh_last != &(elm)->field.tqe_next)			\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("TAILQ_PREREMOVE head %p elm %p %s:%d",\
Kylie McClain b61f59
		    (head), (elm), __FILE__, __LINE__);
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field)				\
Kylie McClain b61f59
	(elm)->field.tqe_next = (void *)1L;				\
Kylie McClain b61f59
	(elm)->field.tqe_prev = (void *)1L;
Kylie McClain b61f59
#else
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_INSERT_HEAD(head, elm, field)
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_INSERT_TAIL(head, elm, field)
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_OP(elm, field)
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_PREREMOVE(head, elm, field)
Kylie McClain b61f59
#define	QUEUEDEBUG_TAILQ_POSTREMOVE(elm, field)
Kylie McClain b61f59
#endif
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_INIT(head) do {						\
Kylie McClain b61f59
	(head)->tqh_first = TAILQ_END(head);				\
Kylie McClain b61f59
	(head)->tqh_last = &(head)->tqh_first;				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_INSERT_HEAD(head, elm, field) do {			\
Kylie McClain b61f59
	QUEUEDEBUG_TAILQ_INSERT_HEAD((head), (elm), field)		\
Kylie McClain b61f59
	if (((elm)->field.tqe_next = (head)->tqh_first) != TAILQ_END(head))\
Kylie McClain b61f59
		(head)->tqh_first->field.tqe_prev =			\
Kylie McClain b61f59
		    &(elm)->field.tqe_next;				\
Kylie McClain b61f59
	else								\
Kylie McClain b61f59
		(head)->tqh_last = &(elm)->field.tqe_next;		\
Kylie McClain b61f59
	(head)->tqh_first = (elm);					\
Kylie McClain b61f59
	(elm)->field.tqe_prev = &(head)->tqh_first;			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_INSERT_TAIL(head, elm, field) do {			\
Kylie McClain b61f59
	QUEUEDEBUG_TAILQ_INSERT_TAIL((head), (elm), field)		\
Kylie McClain b61f59
	(elm)->field.tqe_next = TAILQ_END(head);			\
Kylie McClain b61f59
	(elm)->field.tqe_prev = (head)->tqh_last;			\
Kylie McClain b61f59
	*(head)->tqh_last = (elm);					\
Kylie McClain b61f59
	(head)->tqh_last = &(elm)->field.tqe_next;			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
Kylie McClain b61f59
	QUEUEDEBUG_TAILQ_OP((listelm), field)				\
Kylie McClain b61f59
	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != 	\
Kylie McClain b61f59
	    TAILQ_END(head))						\
Kylie McClain b61f59
		(elm)->field.tqe_next->field.tqe_prev = 		\
Kylie McClain b61f59
		    &(elm)->field.tqe_next;				\
Kylie McClain b61f59
	else								\
Kylie McClain b61f59
		(head)->tqh_last = &(elm)->field.tqe_next;		\
Kylie McClain b61f59
	(listelm)->field.tqe_next = (elm);				\
Kylie McClain b61f59
	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\
Kylie McClain b61f59
	QUEUEDEBUG_TAILQ_OP((listelm), field)				\
Kylie McClain b61f59
	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\
Kylie McClain b61f59
	(elm)->field.tqe_next = (listelm);				\
Kylie McClain b61f59
	*(listelm)->field.tqe_prev = (elm);				\
Kylie McClain b61f59
	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_REMOVE(head, elm, field) do {				\
Kylie McClain b61f59
	QUEUEDEBUG_TAILQ_PREREMOVE((head), (elm), field)		\
Kylie McClain b61f59
	QUEUEDEBUG_TAILQ_OP((elm), field)				\
Kylie McClain b61f59
	if (((elm)->field.tqe_next) != TAILQ_END(head))			\
Kylie McClain b61f59
		(elm)->field.tqe_next->field.tqe_prev = 		\
Kylie McClain b61f59
		    (elm)->field.tqe_prev;				\
Kylie McClain b61f59
	else								\
Kylie McClain b61f59
		(head)->tqh_last = (elm)->field.tqe_prev;		\
Kylie McClain b61f59
	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\
Kylie McClain b61f59
	QUEUEDEBUG_TAILQ_POSTREMOVE((elm), field);			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define TAILQ_REPLACE(head, elm, elm2, field) do {			\
Kylie McClain b61f59
        if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != 	\
Kylie McClain b61f59
	    TAILQ_END(head))   						\
Kylie McClain b61f59
                (elm2)->field.tqe_next->field.tqe_prev =		\
Kylie McClain b61f59
                    &(elm2)->field.tqe_next;				\
Kylie McClain b61f59
        else								\
Kylie McClain b61f59
                (head)->tqh_last = &(elm2)->field.tqe_next;		\
Kylie McClain b61f59
        (elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\
Kylie McClain b61f59
        *(elm2)->field.tqe_prev = (elm2);				\
Kylie McClain b61f59
	QUEUEDEBUG_TAILQ_POSTREMOVE((elm), field);			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	TAILQ_CONCAT(head1, head2, field) do {				\
Kylie McClain b61f59
	if (!TAILQ_EMPTY(head2)) {					\
Kylie McClain b61f59
		*(head1)->tqh_last = (head2)->tqh_first;		\
Kylie McClain b61f59
		(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last;	\
Kylie McClain b61f59
		(head1)->tqh_last = (head2)->tqh_last;			\
Kylie McClain b61f59
		TAILQ_INIT((head2));					\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Singly-linked Tail queue declarations.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	STAILQ_HEAD(name, type)						\
Kylie McClain b61f59
struct name {								\
Kylie McClain b61f59
	struct type *stqh_first;	/* first element */		\
Kylie McClain b61f59
	struct type **stqh_last;	/* addr of last next element */	\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_HEAD_INITIALIZER(head)					\
Kylie McClain b61f59
	{ NULL, &(head).stqh_first }
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_ENTRY(type)						\
Kylie McClain b61f59
struct {								\
Kylie McClain b61f59
	struct type *stqe_next;	/* next element */			\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Singly-linked Tail queue access methods.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	STAILQ_FIRST(head)	((head)->stqh_first)
Kylie McClain b61f59
#define	STAILQ_END(head)	NULL
Kylie McClain b61f59
#define	STAILQ_NEXT(elm, field)	((elm)->field.stqe_next)
Kylie McClain b61f59
#define	STAILQ_EMPTY(head)	(STAILQ_FIRST(head) == STAILQ_END(head))
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Singly-linked Tail queue functions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	STAILQ_INIT(head) do {						\
Kylie McClain b61f59
	(head)->stqh_first = NULL;					\
Kylie McClain b61f59
	(head)->stqh_last = &(head)->stqh_first;				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_INSERT_HEAD(head, elm, field) do {			\
Kylie McClain b61f59
	if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)	\
Kylie McClain b61f59
		(head)->stqh_last = &(elm)->field.stqe_next;		\
Kylie McClain b61f59
	(head)->stqh_first = (elm);					\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_INSERT_TAIL(head, elm, field) do {			\
Kylie McClain b61f59
	(elm)->field.stqe_next = NULL;					\
Kylie McClain b61f59
	*(head)->stqh_last = (elm);					\
Kylie McClain b61f59
	(head)->stqh_last = &(elm)->field.stqe_next;			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\
Kylie McClain b61f59
	if (((elm)->field.stqe_next = (listelm)->field.stqe_next) == NULL)\
Kylie McClain b61f59
		(head)->stqh_last = &(elm)->field.stqe_next;		\
Kylie McClain b61f59
	(listelm)->field.stqe_next = (elm);				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_REMOVE_HEAD(head, field) do {				\
Kylie McClain b61f59
	if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) \
Kylie McClain b61f59
		(head)->stqh_last = &(head)->stqh_first;			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_REMOVE(head, elm, type, field) do {			\
Kylie McClain b61f59
	if ((head)->stqh_first == (elm)) {				\
Kylie McClain b61f59
		STAILQ_REMOVE_HEAD((head), field);			\
Kylie McClain b61f59
	} else {							\
Kylie McClain b61f59
		struct type *curelm = (head)->stqh_first;		\
Kylie McClain b61f59
		while (curelm->field.stqe_next != (elm))			\
Kylie McClain b61f59
			curelm = curelm->field.stqe_next;		\
Kylie McClain b61f59
		if ((curelm->field.stqe_next =				\
Kylie McClain b61f59
			curelm->field.stqe_next->field.stqe_next) == NULL) \
Kylie McClain b61f59
			    (head)->stqh_last = &(curelm)->field.stqe_next; \
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_FOREACH(var, head, field)				\
Kylie McClain b61f59
	for ((var) = ((head)->stqh_first);				\
Kylie McClain b61f59
		(var);							\
Kylie McClain b61f59
		(var) = ((var)->field.stqe_next))
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_FOREACH_SAFE(var, head, field, tvar)			\
Kylie McClain b61f59
	for ((var) = STAILQ_FIRST((head));				\
Kylie McClain b61f59
	    (var) && ((tvar) = STAILQ_NEXT((var), field), 1);		\
Kylie McClain b61f59
	    (var) = (tvar))
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_CONCAT(head1, head2) do {				\
Kylie McClain b61f59
	if (!STAILQ_EMPTY((head2))) {					\
Kylie McClain b61f59
		*(head1)->stqh_last = (head2)->stqh_first;		\
Kylie McClain b61f59
		(head1)->stqh_last = (head2)->stqh_last;		\
Kylie McClain b61f59
		STAILQ_INIT((head2));					\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	STAILQ_LAST(head, type, field)					\
Kylie McClain b61f59
	(STAILQ_EMPTY((head)) ?						\
Kylie McClain b61f59
		NULL :							\
Kylie McClain b61f59
	        ((struct type *)(void *)				\
Kylie McClain b61f59
		((char *)((head)->stqh_last) - offsetof(struct type, field))))
Kylie McClain b61f59
Kylie McClain b61f59
Kylie McClain b61f59
#ifndef _KERNEL
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Circular queue definitions. Do not use. We still keep the macros
Kylie McClain b61f59
 * for compatibility but because of pointer aliasing issues their use
Kylie McClain b61f59
 * is discouraged!
Kylie McClain b61f59
 */
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * __launder_type():  We use this ugly hack to work around the the compiler
Kylie McClain b61f59
 * noticing that two types may not alias each other and elide tests in code.
Kylie McClain b61f59
 * We hit this in the CIRCLEQ macros when comparing 'struct name *' and
Kylie McClain b61f59
 * 'struct type *' (see CIRCLEQ_HEAD()).  Modern compilers (such as GCC
Kylie McClain b61f59
 * 4.8) declare these comparisons as always false, causing the code to
Kylie McClain b61f59
 * not run as designed.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * This hack is only to be used for comparisons and thus can be fully const.
Kylie McClain b61f59
 * Do not use for assignment.
Kylie McClain b61f59
 *
Kylie McClain b61f59
 * If we ever choose to change the ABI of the CIRCLEQ macros, we could fix
Kylie McClain b61f59
 * this by changing the head/tail sentinal values, but see the note above
Kylie McClain b61f59
 * this one.
Kylie McClain b61f59
 */
Kylie McClain b61f59
static __inline const void * __launder_type(const void *);
Kylie McClain b61f59
static __inline const void *
Kylie McClain b61f59
__launder_type(const void *__x)
Kylie McClain b61f59
{
Kylie McClain b61f59
	__asm __volatile("" : "+r" (__x));
Kylie McClain b61f59
	return __x;
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#if defined(QUEUEDEBUG)
Kylie McClain b61f59
#define QUEUEDEBUG_CIRCLEQ_HEAD(head, field)				\
Kylie McClain b61f59
	if ((head)->cqh_first != CIRCLEQ_ENDC(head) &&			\
Kylie McClain b61f59
	    (head)->cqh_first->field.cqe_prev != CIRCLEQ_ENDC(head))	\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("CIRCLEQ head forw %p %s:%d", (head),	\
Kylie McClain b61f59
		      __FILE__, __LINE__);				\
Kylie McClain b61f59
	if ((head)->cqh_last != CIRCLEQ_ENDC(head) &&			\
Kylie McClain b61f59
	    (head)->cqh_last->field.cqe_next != CIRCLEQ_ENDC(head))	\
Kylie McClain b61f59
		QUEUEDEBUG_ABORT("CIRCLEQ head back %p %s:%d", (head),	\
Kylie McClain b61f59
		      __FILE__, __LINE__);
Kylie McClain b61f59
#define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field)			\
Kylie McClain b61f59
	if ((elm)->field.cqe_next == CIRCLEQ_ENDC(head)) {		\
Kylie McClain b61f59
		if ((head)->cqh_last != (elm))				\
Kylie McClain b61f59
			QUEUEDEBUG_ABORT("CIRCLEQ elm last %p %s:%d",	\
Kylie McClain b61f59
			    (elm), __FILE__, __LINE__);			\
Kylie McClain b61f59
	} else {							\
Kylie McClain b61f59
		if ((elm)->field.cqe_next->field.cqe_prev != (elm))	\
Kylie McClain b61f59
			QUEUEDEBUG_ABORT("CIRCLEQ elm forw %p %s:%d",	\
Kylie McClain b61f59
			    (elm), __FILE__, __LINE__);			\
Kylie McClain b61f59
	}								\
Kylie McClain b61f59
	if ((elm)->field.cqe_prev == CIRCLEQ_ENDC(head)) {		\
Kylie McClain b61f59
		if ((head)->cqh_first != (elm))				\
Kylie McClain b61f59
			QUEUEDEBUG_ABORT("CIRCLEQ elm first %p %s:%d",	\
Kylie McClain b61f59
			    (elm), __FILE__, __LINE__);			\
Kylie McClain b61f59
	} else {							\
Kylie McClain b61f59
		if ((elm)->field.cqe_prev->field.cqe_next != (elm))	\
Kylie McClain b61f59
			QUEUEDEBUG_ABORT("CIRCLEQ elm prev %p %s:%d",	\
Kylie McClain b61f59
			    (elm), __FILE__, __LINE__);			\
Kylie McClain b61f59
	}
Kylie McClain b61f59
#define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field)			\
Kylie McClain b61f59
	(elm)->field.cqe_next = (void *)1L;				\
Kylie McClain b61f59
	(elm)->field.cqe_prev = (void *)1L;
Kylie McClain b61f59
#else
Kylie McClain b61f59
#define QUEUEDEBUG_CIRCLEQ_HEAD(head, field)
Kylie McClain b61f59
#define QUEUEDEBUG_CIRCLEQ_ELM(head, elm, field)
Kylie McClain b61f59
#define QUEUEDEBUG_CIRCLEQ_POSTREMOVE(elm, field)
Kylie McClain b61f59
#endif
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_HEAD(name, type)					\
Kylie McClain b61f59
struct name {								\
Kylie McClain b61f59
	struct type *cqh_first;		/* first element */		\
Kylie McClain b61f59
	struct type *cqh_last;		/* last element */		\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_HEAD_INITIALIZER(head)					\
Kylie McClain b61f59
	{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_ENTRY(type)						\
Kylie McClain b61f59
struct {								\
Kylie McClain b61f59
	struct type *cqe_next;		/* next element */		\
Kylie McClain b61f59
	struct type *cqe_prev;		/* previous element */		\
Kylie McClain b61f59
}
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Circular queue functions.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	CIRCLEQ_INIT(head) do {						\
Kylie McClain b61f59
	(head)->cqh_first = CIRCLEQ_END(head);				\
Kylie McClain b61f59
	(head)->cqh_last = CIRCLEQ_END(head);				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\
Kylie McClain b61f59
	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
Kylie McClain b61f59
	QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field)		\
Kylie McClain b61f59
	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\
Kylie McClain b61f59
	(elm)->field.cqe_prev = (listelm);				\
Kylie McClain b61f59
	if ((listelm)->field.cqe_next == CIRCLEQ_ENDC(head))		\
Kylie McClain b61f59
		(head)->cqh_last = (elm);				\
Kylie McClain b61f59
	else								\
Kylie McClain b61f59
		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\
Kylie McClain b61f59
	(listelm)->field.cqe_next = (elm);				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\
Kylie McClain b61f59
	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
Kylie McClain b61f59
	QUEUEDEBUG_CIRCLEQ_ELM((head), (listelm), field)		\
Kylie McClain b61f59
	(elm)->field.cqe_next = (listelm);				\
Kylie McClain b61f59
	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\
Kylie McClain b61f59
	if ((listelm)->field.cqe_prev == CIRCLEQ_ENDC(head))		\
Kylie McClain b61f59
		(head)->cqh_first = (elm);				\
Kylie McClain b61f59
	else								\
Kylie McClain b61f59
		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\
Kylie McClain b61f59
	(listelm)->field.cqe_prev = (elm);				\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\
Kylie McClain b61f59
	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
Kylie McClain b61f59
	(elm)->field.cqe_next = (head)->cqh_first;			\
Kylie McClain b61f59
	(elm)->field.cqe_prev = CIRCLEQ_END(head);			\
Kylie McClain b61f59
	if ((head)->cqh_last == CIRCLEQ_ENDC(head))			\
Kylie McClain b61f59
		(head)->cqh_last = (elm);				\
Kylie McClain b61f59
	else								\
Kylie McClain b61f59
		(head)->cqh_first->field.cqe_prev = (elm);		\
Kylie McClain b61f59
	(head)->cqh_first = (elm);					\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\
Kylie McClain b61f59
	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
Kylie McClain b61f59
	(elm)->field.cqe_next = CIRCLEQ_END(head);			\
Kylie McClain b61f59
	(elm)->field.cqe_prev = (head)->cqh_last;			\
Kylie McClain b61f59
	if ((head)->cqh_first == CIRCLEQ_ENDC(head))			\
Kylie McClain b61f59
		(head)->cqh_first = (elm);				\
Kylie McClain b61f59
	else								\
Kylie McClain b61f59
		(head)->cqh_last->field.cqe_next = (elm);		\
Kylie McClain b61f59
	(head)->cqh_last = (elm);					\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_REMOVE(head, elm, field) do {				\
Kylie McClain b61f59
	QUEUEDEBUG_CIRCLEQ_HEAD((head), field)				\
Kylie McClain b61f59
	QUEUEDEBUG_CIRCLEQ_ELM((head), (elm), field)			\
Kylie McClain b61f59
	if ((elm)->field.cqe_next == CIRCLEQ_ENDC(head))		\
Kylie McClain b61f59
		(head)->cqh_last = (elm)->field.cqe_prev;		\
Kylie McClain b61f59
	else								\
Kylie McClain b61f59
		(elm)->field.cqe_next->field.cqe_prev =			\
Kylie McClain b61f59
		    (elm)->field.cqe_prev;				\
Kylie McClain b61f59
	if ((elm)->field.cqe_prev == CIRCLEQ_ENDC(head))		\
Kylie McClain b61f59
		(head)->cqh_first = (elm)->field.cqe_next;		\
Kylie McClain b61f59
	else								\
Kylie McClain b61f59
		(elm)->field.cqe_prev->field.cqe_next =			\
Kylie McClain b61f59
		    (elm)->field.cqe_next;				\
Kylie McClain b61f59
	QUEUEDEBUG_CIRCLEQ_POSTREMOVE((elm), field)			\
Kylie McClain b61f59
} while (/*CONSTCOND*/0)
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_FOREACH(var, head, field)				\
Kylie McClain b61f59
	for ((var) = ((head)->cqh_first);				\
Kylie McClain b61f59
		(var) != CIRCLEQ_ENDC(head);				\
Kylie McClain b61f59
		(var) = ((var)->field.cqe_next))
Kylie McClain b61f59
Kylie McClain b61f59
#define	CIRCLEQ_FOREACH_REVERSE(var, head, field)			\
Kylie McClain b61f59
	for ((var) = ((head)->cqh_last);				\
Kylie McClain b61f59
		(var) != CIRCLEQ_ENDC(head);				\
Kylie McClain b61f59
		(var) = ((var)->field.cqe_prev))
Kylie McClain b61f59
Kylie McClain b61f59
/*
Kylie McClain b61f59
 * Circular queue access methods.
Kylie McClain b61f59
 */
Kylie McClain b61f59
#define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
Kylie McClain b61f59
#define	CIRCLEQ_LAST(head)		((head)->cqh_last)
Kylie McClain b61f59
/* For comparisons */
Kylie McClain b61f59
#define	CIRCLEQ_ENDC(head)		(__launder_type(head))
Kylie McClain b61f59
/* For assignments */
Kylie McClain b61f59
#define	CIRCLEQ_END(head)		((void *)(head))
Kylie McClain b61f59
#define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
Kylie McClain b61f59
#define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
Kylie McClain b61f59
#define	CIRCLEQ_EMPTY(head)						\
Kylie McClain b61f59
    (CIRCLEQ_FIRST(head) == CIRCLEQ_ENDC(head))
Kylie McClain b61f59
Kylie McClain b61f59
#define CIRCLEQ_LOOP_NEXT(head, elm, field)				\
Kylie McClain b61f59
	(((elm)->field.cqe_next == CIRCLEQ_ENDC(head))			\
Kylie McClain b61f59
	    ? ((head)->cqh_first)					\
Kylie McClain b61f59
	    : (elm->field.cqe_next))
Kylie McClain b61f59
#define CIRCLEQ_LOOP_PREV(head, elm, field)				\
Kylie McClain b61f59
	(((elm)->field.cqe_prev == CIRCLEQ_ENDC(head))			\
Kylie McClain b61f59
	    ? ((head)->cqh_last)					\
Kylie McClain b61f59
	    : (elm->field.cqe_prev))
Kylie McClain b61f59
#endif /* !_KERNEL */
Kylie McClain b61f59
Kylie McClain b61f59
#endif	/* !_SYS_QUEUE_H_ */