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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)queue.h 8.5 (Berkeley) 8/20/94 30 * $FreeBSD$ 31 * 32 * $FreeBSD$ 33 */ 34 35 #ifndef _QUEUE_H_ 36 #define _QUEUE_H_ 37 38 #undef __offsetof 39 #define __offsetof(type, field) ((size_t)(&((type *)0)->field)) 40 41 /* 42 * Singly-linked Tail queue declarations. 43 */ 44 #undef STAILQ_HEAD 45 #define STAILQ_HEAD(name, type) \ 46 struct name { \ 47 struct type *stqh_first;/* first element */ \ 48 struct type **stqh_last;/* addr of last next element */ \ 49 } 50 51 #undef STAILQ_HEAD_INITIALIZER 52 #define STAILQ_HEAD_INITIALIZER(head) \ 53 { NULL, &(head).stqh_first } 54 55 #undef STAILQ_ENTRY 56 #define STAILQ_ENTRY(type) \ 57 struct { \ 58 struct type *stqe_next; /* next element */ \ 59 } 60 61 #undef STAILQ_EMPTY 62 #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) 63 64 #undef STAILQ_FIRST 65 #define STAILQ_FIRST(head) ((head)->stqh_first) 66 67 #undef STAILQ_FOREACH 68 #define STAILQ_FOREACH(var, head, field) \ 69 for((var) = STAILQ_FIRST((head)); \ 70 (var); \ 71 (var) = STAILQ_NEXT((var), field)) 72 73 #undef STAILQ_FOREACH_SAFE 74 #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ 75 for ((var) = STAILQ_FIRST((head)); \ 76 (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ 77 (var) = (tvar)) 78 79 #undef STAILQ_INIT 80 #define STAILQ_INIT(head) do { \ 81 STAILQ_FIRST((head)) = NULL; \ 82 (head)->stqh_last = &STAILQ_FIRST((head)); \ 83 } while (0) 84 85 #undef STAILQ_INSERT_AFTER 86 #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ 87 if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ 88 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 89 STAILQ_NEXT((tqelm), field) = (elm); \ 90 } while (0) 91 92 #undef STAILQ_INSERT_HEAD 93 #define STAILQ_INSERT_HEAD(head, elm, field) do { \ 94 if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ 95 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 96 STAILQ_FIRST((head)) = (elm); \ 97 } while (0) 98 99 #undef STAILQ_INSERT_TAIL 100 #define STAILQ_INSERT_TAIL(head, elm, field) do { \ 101 STAILQ_NEXT((elm), field) = NULL; \ 102 *(head)->stqh_last = (elm); \ 103 (head)->stqh_last = &STAILQ_NEXT((elm), field); \ 104 } while (0) 105 106 #undef STAILQ_LAST 107 #define STAILQ_LAST(head, type, field) \ 108 (STAILQ_EMPTY((head)) ? \ 109 NULL : \ 110 ((struct type *)(void *) \ 111 ((char *)((head)->stqh_last) - __offsetof(struct type, field)))) 112 113 #undef STAILQ_NEXT 114 #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) 115 116 #undef STAILQ_REMOVE 117 #define STAILQ_REMOVE(head, elm, type, field) do { \ 118 if (STAILQ_FIRST((head)) == (elm)) { \ 119 STAILQ_REMOVE_HEAD((head), field); \ 120 } \ 121 else { \ 122 struct type *curelm = STAILQ_FIRST((head)); \ 123 while (STAILQ_NEXT(curelm, field) != (elm)) \ 124 curelm = STAILQ_NEXT(curelm, field); \ 125 if ((STAILQ_NEXT(curelm, field) = \ 126 STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\ 127 (head)->stqh_last = &STAILQ_NEXT((curelm), field);\ 128 } \ 129 } while (0) 130 131 #undef STAILQ_REMOVE_HEAD 132 #define STAILQ_REMOVE_HEAD(head, field) do { \ 133 if ((STAILQ_FIRST((head)) = \ 134 STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ 135 (head)->stqh_last = &STAILQ_FIRST((head)); \ 136 } while (0) 137 138 #undef STAILQ_REMOVE_HEAD_UNTIL 139 #define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \ 140 if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \ 141 (head)->stqh_last = &STAILQ_FIRST((head)); \ 142 } while (0) 143 144 /* 145 * List declarations. 146 */ 147 #undef LIST_HEAD 148 #define LIST_HEAD(name, type) \ 149 struct name { \ 150 struct type *lh_first; /* first element */ \ 151 } 152 153 #undef LIST_HEAD_INITIALIZER 154 #define LIST_HEAD_INITIALIZER(head) \ 155 { NULL } 156 157 #undef LIST_ENTRY 158 #define LIST_ENTRY(type) \ 159 struct { \ 160 struct type *le_next; /* next element */ \ 161 struct type **le_prev; /* address of previous next element */ \ 162 } 163 164 /* 165 * List functions. 166 */ 167 168 #undef LIST_EMPTY 169 #define LIST_EMPTY(head) ((head)->lh_first == NULL) 170 171 #undef LIST_FIRST 172 #define LIST_FIRST(head) ((head)->lh_first) 173 174 #undef LIST_FOREACH 175 #define LIST_FOREACH(var, head, field) \ 176 for ((var) = LIST_FIRST((head)); \ 177 (var); \ 178 (var) = LIST_NEXT((var), field)) 179 180 #undef LIST_FOREACH_SAFE 181 #define LIST_FOREACH_SAFE(var, head, field, tvar) \ 182 for ((var) = LIST_FIRST((head)); \ 183 (var) && ((tvar) = LIST_NEXT((var), field), 1); \ 184 (var) = (tvar)) 185 186 #undef LIST_INIT 187 #define LIST_INIT(head) do { \ 188 LIST_FIRST((head)) = NULL; \ 189 } while (0) 190 191 #undef LIST_INSERT_AFTER 192 #define LIST_INSERT_AFTER(listelm, elm, field) do { \ 193 if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ 194 LIST_NEXT((listelm), field)->field.le_prev = \ 195 &LIST_NEXT((elm), field); \ 196 LIST_NEXT((listelm), field) = (elm); \ 197 (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ 198 } while (0) 199 200 #undef LIST_INSERT_BEFORE 201 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ 202 (elm)->field.le_prev = (listelm)->field.le_prev; \ 203 LIST_NEXT((elm), field) = (listelm); \ 204 *(listelm)->field.le_prev = (elm); \ 205 (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ 206 } while (0) 207 208 #undef LIST_INSERT_HEAD 209 #define LIST_INSERT_HEAD(head, elm, field) do { \ 210 if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ 211 LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ 212 LIST_FIRST((head)) = (elm); \ 213 (elm)->field.le_prev = &LIST_FIRST((head)); \ 214 } while (0) 215 216 #undef LIST_NEXT 217 #define LIST_NEXT(elm, field) ((elm)->field.le_next) 218 219 #undef LIST_REMOVE 220 #define LIST_REMOVE(elm, field) do { \ 221 if (LIST_NEXT((elm), field) != NULL) \ 222 LIST_NEXT((elm), field)->field.le_prev = \ 223 (elm)->field.le_prev; \ 224 *(elm)->field.le_prev = LIST_NEXT((elm), field); \ 225 } while (0) 226 227 #endif /* !_QUEUE_H_ */ 228