1d0cef73dSGregory Neil Shapiro /* $OpenBSD: queue.h,v 1.30 2005/10/25 06:37:47 otto Exp $ */ 2d0cef73dSGregory Neil Shapiro /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */ 3d0cef73dSGregory Neil Shapiro 4d0cef73dSGregory Neil Shapiro /* 5d0cef73dSGregory Neil Shapiro * Copyright (c) 1991, 1993 6d0cef73dSGregory Neil Shapiro * The Regents of the University of California. All rights reserved. 7d0cef73dSGregory Neil Shapiro * 8d0cef73dSGregory Neil Shapiro * Redistribution and use in source and binary forms, with or without 9d0cef73dSGregory Neil Shapiro * modification, are permitted provided that the following conditions 10d0cef73dSGregory Neil Shapiro * are met: 11d0cef73dSGregory Neil Shapiro * 1. Redistributions of source code must retain the above copyright 12d0cef73dSGregory Neil Shapiro * notice, this list of conditions and the following disclaimer. 13d0cef73dSGregory Neil Shapiro * 2. Redistributions in binary form must reproduce the above copyright 14d0cef73dSGregory Neil Shapiro * notice, this list of conditions and the following disclaimer in the 15d0cef73dSGregory Neil Shapiro * documentation and/or other materials provided with the distribution. 16d0cef73dSGregory Neil Shapiro * 3. Neither the name of the University nor the names of its contributors 17d0cef73dSGregory Neil Shapiro * may be used to endorse or promote products derived from this software 18d0cef73dSGregory Neil Shapiro * without specific prior written permission. 19d0cef73dSGregory Neil Shapiro * 20d0cef73dSGregory Neil Shapiro * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21d0cef73dSGregory Neil Shapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22d0cef73dSGregory Neil Shapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23d0cef73dSGregory Neil Shapiro * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24d0cef73dSGregory Neil Shapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25d0cef73dSGregory Neil Shapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26d0cef73dSGregory Neil Shapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27d0cef73dSGregory Neil Shapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28d0cef73dSGregory Neil Shapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29d0cef73dSGregory Neil Shapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30d0cef73dSGregory Neil Shapiro * SUCH DAMAGE. 31d0cef73dSGregory Neil Shapiro * 32d0cef73dSGregory Neil Shapiro * @(#)queue.h 8.5 (Berkeley) 8/20/94 33d0cef73dSGregory Neil Shapiro */ 34d0cef73dSGregory Neil Shapiro 35d0cef73dSGregory Neil Shapiro #ifndef SM_TAILQ_H_ 36d0cef73dSGregory Neil Shapiro #define SM_TAILQ_H_ 37d0cef73dSGregory Neil Shapiro 38d0cef73dSGregory Neil Shapiro /* 39ffb83623SGregory Neil Shapiro * $Id: tailq.h,v 1.3 2012-01-21 00:12:14 ashish Exp $ 40ffb83623SGregory Neil Shapiro * 41d0cef73dSGregory Neil Shapiro * This file is a modified copy of queue.h from a BSD system: 42d0cef73dSGregory Neil Shapiro * we only need tail queues here. 43ffb83623SGregory Neil Shapiro * We do not use queue.h directly because there is a conflict with 44ffb83623SGregory Neil Shapiro * some versions of that file on some OSs. 45d0cef73dSGregory Neil Shapiro * 46d0cef73dSGregory Neil Shapiro * A tail queue is headed by a pair of pointers, one to the head of the 47d0cef73dSGregory Neil Shapiro * list and the other to the tail of the list. The elements are doubly 48d0cef73dSGregory Neil Shapiro * linked so that an arbitrary element can be removed without a need to 49d0cef73dSGregory Neil Shapiro * traverse the list. New elements can be added to the list before or 50d0cef73dSGregory Neil Shapiro * after an existing element, at the head of the list, or at the end of 51d0cef73dSGregory Neil Shapiro * the list. A tail queue may be traversed in either direction. 52d0cef73dSGregory Neil Shapiro */ 53d0cef73dSGregory Neil Shapiro 54d0cef73dSGregory Neil Shapiro /* 55d0cef73dSGregory Neil Shapiro * Tail queue definitions. 56d0cef73dSGregory Neil Shapiro */ 57d0cef73dSGregory Neil Shapiro #define SM_TAILQ_HEAD(name, type) \ 58d0cef73dSGregory Neil Shapiro struct name { \ 59d0cef73dSGregory Neil Shapiro struct type *tqh_first; /* first element */ \ 60d0cef73dSGregory Neil Shapiro struct type **tqh_last; /* addr of last next element */ \ 61d0cef73dSGregory Neil Shapiro } 62d0cef73dSGregory Neil Shapiro 63d0cef73dSGregory Neil Shapiro #define SM_TAILQ_HEAD_INITIALIZER(head) \ 64d0cef73dSGregory Neil Shapiro { NULL, &(head).tqh_first } 65d0cef73dSGregory Neil Shapiro 66d0cef73dSGregory Neil Shapiro #define SM_TAILQ_ENTRY(type) \ 67d0cef73dSGregory Neil Shapiro struct { \ 68d0cef73dSGregory Neil Shapiro struct type *tqe_next; /* next element */ \ 69d0cef73dSGregory Neil Shapiro struct type **tqe_prev; /* address of previous next element */ \ 70d0cef73dSGregory Neil Shapiro } 71d0cef73dSGregory Neil Shapiro 72d0cef73dSGregory Neil Shapiro /* 73d0cef73dSGregory Neil Shapiro * tail queue access methods 74d0cef73dSGregory Neil Shapiro */ 75d0cef73dSGregory Neil Shapiro #define SM_TAILQ_FIRST(head) ((head)->tqh_first) 76d0cef73dSGregory Neil Shapiro #define SM_TAILQ_END(head) NULL 77d0cef73dSGregory Neil Shapiro #define SM_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 78d0cef73dSGregory Neil Shapiro #define SM_TAILQ_LAST(head, headname) \ 79d0cef73dSGregory Neil Shapiro (*(((struct headname *)((head)->tqh_last))->tqh_last)) 80d0cef73dSGregory Neil Shapiro /* XXX */ 81d0cef73dSGregory Neil Shapiro #define SM_TAILQ_PREV(elm, headname, field) \ 82d0cef73dSGregory Neil Shapiro (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 83d0cef73dSGregory Neil Shapiro #define SM_TAILQ_EMPTY(head) \ 84d0cef73dSGregory Neil Shapiro (SM_TAILQ_FIRST(head) == SM_TAILQ_END(head)) 85d0cef73dSGregory Neil Shapiro 86d0cef73dSGregory Neil Shapiro #define SM_TAILQ_FOREACH(var, head, field) \ 87d0cef73dSGregory Neil Shapiro for((var) = SM_TAILQ_FIRST(head); \ 88d0cef73dSGregory Neil Shapiro (var) != SM_TAILQ_END(head); \ 89d0cef73dSGregory Neil Shapiro (var) = SM_TAILQ_NEXT(var, field)) 90d0cef73dSGregory Neil Shapiro 91d0cef73dSGregory Neil Shapiro #define SM_TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 92d0cef73dSGregory Neil Shapiro for((var) = SM_TAILQ_LAST(head, headname); \ 93d0cef73dSGregory Neil Shapiro (var) != SM_TAILQ_END(head); \ 94d0cef73dSGregory Neil Shapiro (var) = SM_TAILQ_PREV(var, headname, field)) 95d0cef73dSGregory Neil Shapiro 96d0cef73dSGregory Neil Shapiro /* 97d0cef73dSGregory Neil Shapiro * Tail queue functions. 98d0cef73dSGregory Neil Shapiro */ 99d0cef73dSGregory Neil Shapiro #define SM_TAILQ_INIT(head) do { \ 100d0cef73dSGregory Neil Shapiro (head)->tqh_first = NULL; \ 101d0cef73dSGregory Neil Shapiro (head)->tqh_last = &(head)->tqh_first; \ 102d0cef73dSGregory Neil Shapiro } while (0) 103d0cef73dSGregory Neil Shapiro 104d0cef73dSGregory Neil Shapiro #define SM_TAILQ_INSERT_HEAD(head, elm, field) do { \ 105d0cef73dSGregory Neil Shapiro if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ 106d0cef73dSGregory Neil Shapiro (head)->tqh_first->field.tqe_prev = \ 107d0cef73dSGregory Neil Shapiro &(elm)->field.tqe_next; \ 108d0cef73dSGregory Neil Shapiro else \ 109d0cef73dSGregory Neil Shapiro (head)->tqh_last = &(elm)->field.tqe_next; \ 110d0cef73dSGregory Neil Shapiro (head)->tqh_first = (elm); \ 111d0cef73dSGregory Neil Shapiro (elm)->field.tqe_prev = &(head)->tqh_first; \ 112d0cef73dSGregory Neil Shapiro } while (0) 113d0cef73dSGregory Neil Shapiro 114d0cef73dSGregory Neil Shapiro #define SM_TAILQ_INSERT_TAIL(head, elm, field) do { \ 115d0cef73dSGregory Neil Shapiro (elm)->field.tqe_next = NULL; \ 116d0cef73dSGregory Neil Shapiro (elm)->field.tqe_prev = (head)->tqh_last; \ 117d0cef73dSGregory Neil Shapiro *(head)->tqh_last = (elm); \ 118d0cef73dSGregory Neil Shapiro (head)->tqh_last = &(elm)->field.tqe_next; \ 119d0cef73dSGregory Neil Shapiro } while (0) 120d0cef73dSGregory Neil Shapiro 121d0cef73dSGregory Neil Shapiro #define SM_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 122d0cef73dSGregory Neil Shapiro if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ 123d0cef73dSGregory Neil Shapiro (elm)->field.tqe_next->field.tqe_prev = \ 124d0cef73dSGregory Neil Shapiro &(elm)->field.tqe_next; \ 125d0cef73dSGregory Neil Shapiro else \ 126d0cef73dSGregory Neil Shapiro (head)->tqh_last = &(elm)->field.tqe_next; \ 127d0cef73dSGregory Neil Shapiro (listelm)->field.tqe_next = (elm); \ 128d0cef73dSGregory Neil Shapiro (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ 129d0cef73dSGregory Neil Shapiro } while (0) 130d0cef73dSGregory Neil Shapiro 131d0cef73dSGregory Neil Shapiro #define SM_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 132d0cef73dSGregory Neil Shapiro (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 133d0cef73dSGregory Neil Shapiro (elm)->field.tqe_next = (listelm); \ 134d0cef73dSGregory Neil Shapiro *(listelm)->field.tqe_prev = (elm); \ 135d0cef73dSGregory Neil Shapiro (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ 136d0cef73dSGregory Neil Shapiro } while (0) 137d0cef73dSGregory Neil Shapiro 138d0cef73dSGregory Neil Shapiro #define SM_TAILQ_REMOVE(head, elm, field) do { \ 139d0cef73dSGregory Neil Shapiro if (((elm)->field.tqe_next) != NULL) \ 140d0cef73dSGregory Neil Shapiro (elm)->field.tqe_next->field.tqe_prev = \ 141d0cef73dSGregory Neil Shapiro (elm)->field.tqe_prev; \ 142d0cef73dSGregory Neil Shapiro else \ 143d0cef73dSGregory Neil Shapiro (head)->tqh_last = (elm)->field.tqe_prev; \ 144d0cef73dSGregory Neil Shapiro *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ 145d0cef73dSGregory Neil Shapiro } while (0) 146d0cef73dSGregory Neil Shapiro 147d0cef73dSGregory Neil Shapiro #define SM_TAILQ_REPLACE(head, elm, elm2, field) do { \ 148d0cef73dSGregory Neil Shapiro if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \ 149d0cef73dSGregory Neil Shapiro (elm2)->field.tqe_next->field.tqe_prev = \ 150d0cef73dSGregory Neil Shapiro &(elm2)->field.tqe_next; \ 151d0cef73dSGregory Neil Shapiro else \ 152d0cef73dSGregory Neil Shapiro (head)->tqh_last = &(elm2)->field.tqe_next; \ 153d0cef73dSGregory Neil Shapiro (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ 154d0cef73dSGregory Neil Shapiro *(elm2)->field.tqe_prev = (elm2); \ 155d0cef73dSGregory Neil Shapiro } while (0) 156d0cef73dSGregory Neil Shapiro 157d0cef73dSGregory Neil Shapiro #endif /* !SM_TAILQ_H_ */ 158