1058561cbSjbeck /* $OpenBSD: queue.h,v 1.30 2005/10/25 06:37:47 otto Exp $ */ 2058561cbSjbeck /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */ 3058561cbSjbeck 4058561cbSjbeck /* 5058561cbSjbeck * Copyright (c) 1991, 1993 6058561cbSjbeck * The Regents of the University of California. All rights reserved. 7058561cbSjbeck * 8058561cbSjbeck * Redistribution and use in source and binary forms, with or without 9058561cbSjbeck * modification, are permitted provided that the following conditions 10058561cbSjbeck * are met: 11058561cbSjbeck * 1. Redistributions of source code must retain the above copyright 12058561cbSjbeck * notice, this list of conditions and the following disclaimer. 13058561cbSjbeck * 2. Redistributions in binary form must reproduce the above copyright 14058561cbSjbeck * notice, this list of conditions and the following disclaimer in the 15058561cbSjbeck * documentation and/or other materials provided with the distribution. 16058561cbSjbeck * 3. Neither the name of the University nor the names of its contributors 17058561cbSjbeck * may be used to endorse or promote products derived from this software 18058561cbSjbeck * without specific prior written permission. 19058561cbSjbeck * 20058561cbSjbeck * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21058561cbSjbeck * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22058561cbSjbeck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23058561cbSjbeck * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24058561cbSjbeck * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25058561cbSjbeck * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26058561cbSjbeck * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27058561cbSjbeck * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28058561cbSjbeck * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29058561cbSjbeck * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30058561cbSjbeck * SUCH DAMAGE. 31058561cbSjbeck * 32058561cbSjbeck * @(#)queue.h 8.5 (Berkeley) 8/20/94 33058561cbSjbeck */ 34058561cbSjbeck 35058561cbSjbeck #ifndef SM_TAILQ_H_ 36058561cbSjbeck #define SM_TAILQ_H_ 37058561cbSjbeck 38058561cbSjbeck #pragma ident "%Z%%M% %I% %E% SMI" 39058561cbSjbeck 40058561cbSjbeck /* 41*7800901eSjbeck * $Id: tailq.h,v 1.2 2007/06/29 23:09:57 ca Exp $ 42*7800901eSjbeck * 43058561cbSjbeck * This file is a modified copy of queue.h from a BSD system: 44058561cbSjbeck * we only need tail queues here. 45*7800901eSjbeck * We do not use queue.h directly because there is a conflict with 46*7800901eSjbeck * some versions of that file on some OSs. 47058561cbSjbeck * 48058561cbSjbeck * A tail queue is headed by a pair of pointers, one to the head of the 49058561cbSjbeck * list and the other to the tail of the list. The elements are doubly 50058561cbSjbeck * linked so that an arbitrary element can be removed without a need to 51058561cbSjbeck * traverse the list. New elements can be added to the list before or 52058561cbSjbeck * after an existing element, at the head of the list, or at the end of 53058561cbSjbeck * the list. A tail queue may be traversed in either direction. 54058561cbSjbeck */ 55058561cbSjbeck 56058561cbSjbeck /* 57058561cbSjbeck * Tail queue definitions. 58058561cbSjbeck */ 59058561cbSjbeck #define SM_TAILQ_HEAD(name, type) \ 60058561cbSjbeck struct name { \ 61058561cbSjbeck struct type *tqh_first; /* first element */ \ 62058561cbSjbeck struct type **tqh_last; /* addr of last next element */ \ 63058561cbSjbeck } 64058561cbSjbeck 65058561cbSjbeck #define SM_TAILQ_HEAD_INITIALIZER(head) \ 66058561cbSjbeck { NULL, &(head).tqh_first } 67058561cbSjbeck 68058561cbSjbeck #define SM_TAILQ_ENTRY(type) \ 69058561cbSjbeck struct { \ 70058561cbSjbeck struct type *tqe_next; /* next element */ \ 71058561cbSjbeck struct type **tqe_prev; /* address of previous next element */ \ 72058561cbSjbeck } 73058561cbSjbeck 74058561cbSjbeck /* 75058561cbSjbeck * tail queue access methods 76058561cbSjbeck */ 77058561cbSjbeck #define SM_TAILQ_FIRST(head) ((head)->tqh_first) 78058561cbSjbeck #define SM_TAILQ_END(head) NULL 79058561cbSjbeck #define SM_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 80058561cbSjbeck #define SM_TAILQ_LAST(head, headname) \ 81058561cbSjbeck (*(((struct headname *)((head)->tqh_last))->tqh_last)) 82058561cbSjbeck /* XXX */ 83058561cbSjbeck #define SM_TAILQ_PREV(elm, headname, field) \ 84058561cbSjbeck (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 85058561cbSjbeck #define SM_TAILQ_EMPTY(head) \ 86058561cbSjbeck (SM_TAILQ_FIRST(head) == SM_TAILQ_END(head)) 87058561cbSjbeck 88058561cbSjbeck #define SM_TAILQ_FOREACH(var, head, field) \ 89058561cbSjbeck for((var) = SM_TAILQ_FIRST(head); \ 90058561cbSjbeck (var) != SM_TAILQ_END(head); \ 91058561cbSjbeck (var) = SM_TAILQ_NEXT(var, field)) 92058561cbSjbeck 93058561cbSjbeck #define SM_TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 94058561cbSjbeck for((var) = SM_TAILQ_LAST(head, headname); \ 95058561cbSjbeck (var) != SM_TAILQ_END(head); \ 96058561cbSjbeck (var) = SM_TAILQ_PREV(var, headname, field)) 97058561cbSjbeck 98058561cbSjbeck /* 99058561cbSjbeck * Tail queue functions. 100058561cbSjbeck */ 101058561cbSjbeck #define SM_TAILQ_INIT(head) do { \ 102058561cbSjbeck (head)->tqh_first = NULL; \ 103058561cbSjbeck (head)->tqh_last = &(head)->tqh_first; \ 104058561cbSjbeck } while (0) 105058561cbSjbeck 106058561cbSjbeck #define SM_TAILQ_INSERT_HEAD(head, elm, field) do { \ 107058561cbSjbeck if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ 108058561cbSjbeck (head)->tqh_first->field.tqe_prev = \ 109058561cbSjbeck &(elm)->field.tqe_next; \ 110058561cbSjbeck else \ 111058561cbSjbeck (head)->tqh_last = &(elm)->field.tqe_next; \ 112058561cbSjbeck (head)->tqh_first = (elm); \ 113058561cbSjbeck (elm)->field.tqe_prev = &(head)->tqh_first; \ 114058561cbSjbeck } while (0) 115058561cbSjbeck 116058561cbSjbeck #define SM_TAILQ_INSERT_TAIL(head, elm, field) do { \ 117058561cbSjbeck (elm)->field.tqe_next = NULL; \ 118058561cbSjbeck (elm)->field.tqe_prev = (head)->tqh_last; \ 119058561cbSjbeck *(head)->tqh_last = (elm); \ 120058561cbSjbeck (head)->tqh_last = &(elm)->field.tqe_next; \ 121058561cbSjbeck } while (0) 122058561cbSjbeck 123058561cbSjbeck #define SM_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 124058561cbSjbeck if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ 125058561cbSjbeck (elm)->field.tqe_next->field.tqe_prev = \ 126058561cbSjbeck &(elm)->field.tqe_next; \ 127058561cbSjbeck else \ 128058561cbSjbeck (head)->tqh_last = &(elm)->field.tqe_next; \ 129058561cbSjbeck (listelm)->field.tqe_next = (elm); \ 130058561cbSjbeck (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ 131058561cbSjbeck } while (0) 132058561cbSjbeck 133058561cbSjbeck #define SM_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 134058561cbSjbeck (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 135058561cbSjbeck (elm)->field.tqe_next = (listelm); \ 136058561cbSjbeck *(listelm)->field.tqe_prev = (elm); \ 137058561cbSjbeck (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ 138058561cbSjbeck } while (0) 139058561cbSjbeck 140058561cbSjbeck #define SM_TAILQ_REMOVE(head, elm, field) do { \ 141058561cbSjbeck if (((elm)->field.tqe_next) != NULL) \ 142058561cbSjbeck (elm)->field.tqe_next->field.tqe_prev = \ 143058561cbSjbeck (elm)->field.tqe_prev; \ 144058561cbSjbeck else \ 145058561cbSjbeck (head)->tqh_last = (elm)->field.tqe_prev; \ 146058561cbSjbeck *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ 147058561cbSjbeck } while (0) 148058561cbSjbeck 149058561cbSjbeck #define SM_TAILQ_REPLACE(head, elm, elm2, field) do { \ 150058561cbSjbeck if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \ 151058561cbSjbeck (elm2)->field.tqe_next->field.tqe_prev = \ 152058561cbSjbeck &(elm2)->field.tqe_next; \ 153058561cbSjbeck else \ 154058561cbSjbeck (head)->tqh_last = &(elm2)->field.tqe_next; \ 155058561cbSjbeck (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ 156058561cbSjbeck *(elm2)->field.tqe_prev = (elm2); \ 157058561cbSjbeck } while (0) 158058561cbSjbeck 159058561cbSjbeck #endif /* !SM_TAILQ_H_ */ 160