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