queue.h revision 13697
1/* 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)queue.h 8.4 (Berkeley) 1/4/94 34 * $Id: queue.h,v 1.4 1995/12/03 13:45:34 bde Exp $ 35 */ 36 37#ifndef _SYS_QUEUE_H_ 38#define _SYS_QUEUE_H_ 39 40/* 41 * This file defines three types of data structures: lists, tail queues, 42 * and circular queues. 43 * 44 * A list is headed by a single forward pointer (or an array of forward 45 * pointers for a hash table header). The elements are doubly linked 46 * so that an arbitrary element can be removed without a need to 47 * traverse the list. New elements can be added to the list after 48 * an existing element or at the head of the list. A list may only be 49 * traversed in the forward direction. 50 * 51 * A tail queue is headed by a pair of pointers, one to the head of the 52 * list and the other to the tail of the list. The elements are doubly 53 * linked so that an arbitrary element can be removed without a need to 54 * traverse the list. New elements can be added to the list after 55 * an existing element, at the head of the list, or at the end of the 56 * list. A tail queue may only be traversed in the forward direction. 57 * 58 * A circle queue is headed by a pair of pointers, one to the head of the 59 * list and the other to the tail of the list. The elements are doubly 60 * linked so that an arbitrary element can be removed without a need to 61 * traverse the list. New elements can be added to the list before or after 62 * an existing element, at the head of the list, or at the end of the list. 63 * A circle queue may be traversed in either direction, but has a more 64 * complex end of list detection. 65 * 66 * For details on the use of these macros, see the queue(3) manual page. 67 */ 68 69/* 70 * List definitions. 71 */ 72#define LIST_HEAD(name, type) \ 73struct name { \ 74 struct type *lh_first; /* first element */ \ 75} 76 77#define LIST_ENTRY(type) \ 78struct { \ 79 struct type *le_next; /* next element */ \ 80 struct type **le_prev; /* address of previous next element */ \ 81} 82 83/* 84 * List functions. 85 */ 86#define LIST_INIT(head) { \ 87 (head)->lh_first = NULL; \ 88} 89 90#define LIST_INSERT_AFTER(listelm, elm, field) { \ 91 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ 92 (listelm)->field.le_next->field.le_prev = \ 93 &(elm)->field.le_next; \ 94 (listelm)->field.le_next = (elm); \ 95 (elm)->field.le_prev = &(listelm)->field.le_next; \ 96} 97 98#define LIST_INSERT_BEFORE(listelm, elm, field) { \ 99 (elm)->field.le_prev = (listelm)->field.le_prev; \ 100 (elm)->field.le_next = (listelm); \ 101 *(listelm)->field.le_prev = (elm); \ 102 (listelm)->field.le_prev = &(elm)->field.le_next; \ 103} 104 105#define LIST_INSERT_HEAD(head, elm, field) { \ 106 if (((elm)->field.le_next = (head)->lh_first) != NULL) \ 107 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ 108 (head)->lh_first = (elm); \ 109 (elm)->field.le_prev = &(head)->lh_first; \ 110} 111 112#define LIST_REMOVE(elm, field) { \ 113 if ((elm)->field.le_next != NULL) \ 114 (elm)->field.le_next->field.le_prev = \ 115 (elm)->field.le_prev; \ 116 *(elm)->field.le_prev = (elm)->field.le_next; \ 117} 118 119/* 120 * Tail queue definitions. 121 */ 122#define TAILQ_HEAD(name, type) \ 123struct name { \ 124 struct type *tqh_first; /* first element */ \ 125 struct type **tqh_last; /* addr of last next element */ \ 126} 127 128#define TAILQ_ENTRY(type) \ 129struct { \ 130 struct type *tqe_next; /* next element */ \ 131 struct type **tqe_prev; /* address of previous next element */ \ 132} 133 134/* 135 * Tail queue functions. 136 */ 137#define TAILQ_INIT(head) { \ 138 (head)->tqh_first = NULL; \ 139 (head)->tqh_last = &(head)->tqh_first; \ 140} 141 142#define TAILQ_INSERT_HEAD(head, elm, field) { \ 143 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ 144 (elm)->field.tqe_next->field.tqe_prev = \ 145 &(elm)->field.tqe_next; \ 146 else \ 147 (head)->tqh_last = &(elm)->field.tqe_next; \ 148 (head)->tqh_first = (elm); \ 149 (elm)->field.tqe_prev = &(head)->tqh_first; \ 150} 151 152#define TAILQ_INSERT_TAIL(head, elm, field) { \ 153 (elm)->field.tqe_next = NULL; \ 154 (elm)->field.tqe_prev = (head)->tqh_last; \ 155 *(head)->tqh_last = (elm); \ 156 (head)->tqh_last = &(elm)->field.tqe_next; \ 157} 158 159#define TAILQ_INSERT_AFTER(head, listelm, elm, field) { \ 160 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ 161 (elm)->field.tqe_next->field.tqe_prev = \ 162 &(elm)->field.tqe_next; \ 163 else \ 164 (head)->tqh_last = &(elm)->field.tqe_next; \ 165 (listelm)->field.tqe_next = (elm); \ 166 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ 167} 168 169#define TAILQ_INSERT_BEFORE(head, listelm, elm, field) { \ 170 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 171 (elm)->field.tqe_next = (listelm); \ 172 *(listelm)->field.tqe_prev = (elm); \ 173 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ 174} 175 176#define TAILQ_REMOVE(head, elm, field) { \ 177 if (((elm)->field.tqe_next) != NULL) \ 178 (elm)->field.tqe_next->field.tqe_prev = \ 179 (elm)->field.tqe_prev; \ 180 else \ 181 (head)->tqh_last = (elm)->field.tqe_prev; \ 182 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ 183} 184 185/* 186 * Circular queue definitions. 187 */ 188#define CIRCLEQ_HEAD(name, type) \ 189struct name { \ 190 struct type *cqh_first; /* first element */ \ 191 struct type *cqh_last; /* last element */ \ 192} 193 194#define CIRCLEQ_ENTRY(type) \ 195struct { \ 196 struct type *cqe_next; /* next element */ \ 197 struct type *cqe_prev; /* previous element */ \ 198} 199 200/* 201 * Circular queue functions. 202 */ 203#define CIRCLEQ_INIT(head) { \ 204 (head)->cqh_first = (void *)(head); \ 205 (head)->cqh_last = (void *)(head); \ 206} 207 208#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) { \ 209 (elm)->field.cqe_next = (listelm)->field.cqe_next; \ 210 (elm)->field.cqe_prev = (listelm); \ 211 if ((listelm)->field.cqe_next == (void *)(head)) \ 212 (head)->cqh_last = (elm); \ 213 else \ 214 (listelm)->field.cqe_next->field.cqe_prev = (elm); \ 215 (listelm)->field.cqe_next = (elm); \ 216} 217 218#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) { \ 219 (elm)->field.cqe_next = (listelm); \ 220 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ 221 if ((listelm)->field.cqe_prev == (void *)(head)) \ 222 (head)->cqh_first = (elm); \ 223 else \ 224 (listelm)->field.cqe_prev->field.cqe_next = (elm); \ 225 (listelm)->field.cqe_prev = (elm); \ 226} 227 228#define CIRCLEQ_INSERT_HEAD(head, elm, field) { \ 229 (elm)->field.cqe_next = (head)->cqh_first; \ 230 (elm)->field.cqe_prev = (void *)(head); \ 231 if ((head)->cqh_last == (void *)(head)) \ 232 (head)->cqh_last = (elm); \ 233 else \ 234 (head)->cqh_first->field.cqe_prev = (elm); \ 235 (head)->cqh_first = (elm); \ 236} 237 238#define CIRCLEQ_INSERT_TAIL(head, elm, field) { \ 239 (elm)->field.cqe_next = (void *)(head); \ 240 (elm)->field.cqe_prev = (head)->cqh_last; \ 241 if ((head)->cqh_first == (void *)(head)) \ 242 (head)->cqh_first = (elm); \ 243 else \ 244 (head)->cqh_last->field.cqe_next = (elm); \ 245 (head)->cqh_last = (elm); \ 246} 247 248#define CIRCLEQ_REMOVE(head, elm, field) { \ 249 if ((elm)->field.cqe_next == (void *)(head)) \ 250 (head)->cqh_last = (elm)->field.cqe_prev; \ 251 else \ 252 (elm)->field.cqe_next->field.cqe_prev = \ 253 (elm)->field.cqe_prev; \ 254 if ((elm)->field.cqe_prev == (void *)(head)) \ 255 (head)->cqh_first = (elm)->field.cqe_next; \ 256 else \ 257 (elm)->field.cqe_prev->field.cqe_next = \ 258 (elm)->field.cqe_next; \ 259} 260 261#ifdef KERNEL 262 263/* 264 * XXX insque() and remque() are an old way of handling certain queues. 265 * They bogusly assumes that all queue heads look alike. 266 */ 267 268struct quehead { 269 struct quehead *qh_link; 270 struct quehead *qh_rlink; 271}; 272 273#ifdef __GNUC__ 274 275static __inline void 276insque(void *a, void *b) 277{ 278 struct quehead *element = a, *head = b; 279 280 element->qh_link = head->qh_link; 281 element->qh_rlink = head; 282 head->qh_link = element; 283 element->qh_link->qh_rlink = element; 284} 285 286static __inline void 287remque(void *a) 288{ 289 struct quehead *element = a; 290 291 element->qh_link->qh_rlink = element->qh_rlink; 292 element->qh_rlink->qh_link = element->qh_link; 293 element->qh_rlink = 0; 294} 295 296#else /* !__GNUC__ */ 297 298void insque __P((void *a, void *b)); 299void remque __P((void *a)); 300 301#endif /* __GNUC__ */ 302 303#endif /* KERNEL */ 304 305#endif /* !_SYS_QUEUE_H_ */ 306