tailq.h revision 058561cbaa119a6f2659bc27ef343e1b47266bb2
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck/* $OpenBSD: queue.h,v 1.30 2005/10/25 06:37:47 otto Exp $ */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck/*
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * Copyright (c) 1991, 1993
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * The Regents of the University of California. All rights reserved.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck *
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * Redistribution and use in source and binary forms, with or without
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * modification, are permitted provided that the following conditions
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * are met:
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * 1. Redistributions of source code must retain the above copyright
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * notice, this list of conditions and the following disclaimer.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * 2. Redistributions in binary form must reproduce the above copyright
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * notice, this list of conditions and the following disclaimer in the
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * documentation and/or other materials provided with the distribution.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * 3. Neither the name of the University nor the names of its contributors
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * may be used to endorse or promote products derived from this software
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * without specific prior written permission.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck *
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * SUCH DAMAGE.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck *
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * @(#)queue.h 8.5 (Berkeley) 8/20/94
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#ifndef SM_TAILQ_H_
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_H_
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#pragma ident "%Z%%M% %I% %E% SMI"
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck/*
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * This file is a modified copy of queue.h from a BSD system:
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * we only need tail queues here.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck *
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * A tail queue is headed by a pair of pointers, one to the head of the
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * list and the other to the tail of the list. The elements are doubly
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * linked so that an arbitrary element can be removed without a need to
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * traverse the list. New elements can be added to the list before or
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * after an existing element, at the head of the list, or at the end of
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * the list. A tail queue may be traversed in either direction.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck/*
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * Tail queue definitions.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_HEAD(name, type) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeckstruct name { \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck struct type *tqh_first; /* first element */ \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck struct type **tqh_last; /* addr of last next element */ \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck}
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_HEAD_INITIALIZER(head) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck { NULL, &(head).tqh_first }
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_ENTRY(type) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeckstruct { \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck struct type *tqe_next; /* next element */ \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck struct type **tqe_prev; /* address of previous next element */ \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck}
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck/*
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * tail queue access methods
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_FIRST(head) ((head)->tqh_first)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_END(head) NULL
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_LAST(head, headname) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (*(((struct headname *)((head)->tqh_last))->tqh_last))
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck/* XXX */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_PREV(elm, headname, field) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_EMPTY(head) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (SM_TAILQ_FIRST(head) == SM_TAILQ_END(head))
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_FOREACH(var, head, field) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck for((var) = SM_TAILQ_FIRST(head); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (var) != SM_TAILQ_END(head); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (var) = SM_TAILQ_NEXT(var, field))
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck for((var) = SM_TAILQ_LAST(head, headname); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (var) != SM_TAILQ_END(head); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (var) = SM_TAILQ_PREV(var, headname, field))
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck/*
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck * Tail queue functions.
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck */
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_INIT(head) do { \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (head)->tqh_first = NULL; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (head)->tqh_last = &(head)->tqh_first; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck} while (0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_INSERT_HEAD(head, elm, field) do { \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (head)->tqh_first->field.tqe_prev = \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck &(elm)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck else \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (head)->tqh_last = &(elm)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (head)->tqh_first = (elm); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm)->field.tqe_prev = &(head)->tqh_first; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck} while (0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_INSERT_TAIL(head, elm, field) do { \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm)->field.tqe_next = NULL; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm)->field.tqe_prev = (head)->tqh_last; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck *(head)->tqh_last = (elm); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (head)->tqh_last = &(elm)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck} while (0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm)->field.tqe_next->field.tqe_prev = \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck &(elm)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck else \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (head)->tqh_last = &(elm)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (listelm)->field.tqe_next = (elm); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck} while (0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm)->field.tqe_next = (listelm); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck *(listelm)->field.tqe_prev = (elm); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck} while (0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_REMOVE(head, elm, field) do { \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck if (((elm)->field.tqe_next) != NULL) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm)->field.tqe_next->field.tqe_prev = \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm)->field.tqe_prev; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck else \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (head)->tqh_last = (elm)->field.tqe_prev; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck} while (0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#define SM_TAILQ_REPLACE(head, elm, elm2, field) do { \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm2)->field.tqe_next->field.tqe_prev = \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck &(elm2)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck else \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (head)->tqh_last = &(elm2)->field.tqe_next; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck *(elm2)->field.tqe_prev = (elm2); \
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck} while (0)
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck
058561cbaa119a6f2659bc27ef343e1b47266bb2jbeck#endif /* !SM_TAILQ_H_ */