| 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 |
*/ |
| 30 |
|
| 31 |
#ifndef _USER_QUEUE_H_ |
| 32 |
#define _USER_QUEUE_H_ |
| 33 |
|
| 34 |
/* |
| 35 |
* This file defines four types of data structures: singly-linked lists, |
| 36 |
* singly-linked tail queues, lists and tail queues. |
| 37 |
* |
| 38 |
* A singly-linked list is headed by a single forward pointer. The elements |
| 39 |
* are singly linked for minimum space and pointer manipulation overhead at |
| 40 |
* the expense of O(n) removal for arbitrary elements. New elements can be |
| 41 |
* added to the list after an existing element or at the head of the list. |
| 42 |
* Elements being removed from the head of the list should use the explicit |
| 43 |
* macro for this purpose for optimum efficiency. A singly-linked list may |
| 44 |
* only be traversed in the forward direction. Singly-linked lists are ideal |
| 45 |
* for applications with large datasets and few or no removals or for |
| 46 |
* implementing a LIFO queue. |
| 47 |
* |
| 48 |
* A singly-linked tail queue is headed by a pair of pointers, one to the |
| 49 |
* head of the list and the other to the tail of the list. The elements are |
| 50 |
* singly linked for minimum space and pointer manipulation overhead at the |
| 51 |
* expense of O(n) removal for arbitrary elements. New elements can be added |
| 52 |
* to the list after an existing element, at the head of the list, or at the |
| 53 |
* end of the list. Elements being removed from the head of the tail queue |
| 54 |
* should use the explicit macro for this purpose for optimum efficiency. |
| 55 |
* A singly-linked tail queue may only be traversed in the forward direction. |
| 56 |
* Singly-linked tail queues are ideal for applications with large datasets |
| 57 |
* and few or no removals or for implementing a FIFO queue. |
| 58 |
* |
| 59 |
* A list is headed by a single forward pointer (or an array of forward |
| 60 |
* pointers for a hash table header). The elements are doubly linked |
| 61 |
* so that an arbitrary element can be removed without a need to |
| 62 |
* traverse the list. New elements can be added to the list before |
| 63 |
* or after an existing element or at the head of the list. A list |
| 64 |
* may only be traversed in the forward direction. |
| 65 |
* |
| 66 |
* A tail queue is headed by a pair of pointers, one to the head of the |
| 67 |
* list and the other to the tail of the list. The elements are doubly |
| 68 |
* linked so that an arbitrary element can be removed without a need to |
| 69 |
* traverse the list. New elements can be added to the list before or |
| 70 |
* after an existing element, at the head of the list, or at the end of |
| 71 |
* the list. A tail queue may be traversed in either direction. |
| 72 |
* |
| 73 |
* For details on the use of these macros, see the queue(3) manual page. |
| 74 |
* |
| 75 |
* |
| 76 |
* SLIST LIST STAILQ TAILQ |
| 77 |
* _HEAD + + + + |
| 78 |
* _HEAD_INITIALIZER + + + + |
| 79 |
* _ENTRY + + + + |
| 80 |
* _INIT + + + + |
| 81 |
* _EMPTY + + + + |
| 82 |
* _FIRST + + + + |
| 83 |
* _NEXT + + + + |
| 84 |
* _PREV - - - + |
| 85 |
* _LAST - - + + |
| 86 |
* _FOREACH + + + + |
| 87 |
* _FOREACH_SAFE + + + + |
| 88 |
* _FOREACH_REVERSE - - - + |
| 89 |
* _FOREACH_REVERSE_SAFE - - - + |
| 90 |
* _INSERT_HEAD + + + + |
| 91 |
* _INSERT_BEFORE - + - + |
| 92 |
* _INSERT_AFTER + + + + |
| 93 |
* _INSERT_TAIL - - + + |
| 94 |
* _CONCAT - - + + |
| 95 |
* _REMOVE_AFTER + - + - |
| 96 |
* _REMOVE_HEAD + - + - |
| 97 |
* _REMOVE + + + + |
| 98 |
* _SWAP + + + + |
| 99 |
* |
| 100 |
*/ |
| 101 |
#ifdef QUEUE_MACRO_DEBUG |
| 102 |
/* Store the last 2 places the queue element or head was altered */ |
| 103 |
struct qm_trace { |
| 104 |
char * lastfile; |
| 105 |
int lastline; |
| 106 |
char * prevfile; |
| 107 |
int prevline; |
| 108 |
}; |
| 109 |
|
| 110 |
#define TRACEBUF struct qm_trace trace; |
| 111 |
#define TRASHIT(x) do {(x) = (void *)-1;} while (0) |
| 112 |
#define QMD_SAVELINK(name, link) void **name = (void *)&(link) |
| 113 |
|
| 114 |
#define QMD_TRACE_HEAD(head) do { \ |
| 115 |
(head)->trace.prevline = (head)->trace.lastline; \ |
| 116 |
(head)->trace.prevfile = (head)->trace.lastfile; \ |
| 117 |
(head)->trace.lastline = __LINE__; \ |
| 118 |
(head)->trace.lastfile = __FILE__; \ |
| 119 |
} while (0) |
| 120 |
|
| 121 |
#define QMD_TRACE_ELEM(elem) do { \ |
| 122 |
(elem)->trace.prevline = (elem)->trace.lastline; \ |
| 123 |
(elem)->trace.prevfile = (elem)->trace.lastfile; \ |
| 124 |
(elem)->trace.lastline = __LINE__; \ |
| 125 |
(elem)->trace.lastfile = __FILE__; \ |
| 126 |
} while (0) |
| 127 |
|
| 128 |
#else |
| 129 |
#define QMD_TRACE_ELEM(elem) |
| 130 |
#define QMD_TRACE_HEAD(head) |
| 131 |
#define QMD_SAVELINK(name, link) |
| 132 |
#define TRACEBUF |
| 133 |
#define TRASHIT(x) |
| 134 |
#endif /* QUEUE_MACRO_DEBUG */ |
| 135 |
|
| 136 |
/* |
| 137 |
* Singly-linked List declarations. |
| 138 |
*/ |
| 139 |
#define SLIST_HEAD(name, type) \ |
| 140 |
struct name { \ |
| 141 |
struct type *slh_first; /* first element */ \ |
| 142 |
} |
| 143 |
|
| 144 |
#define SLIST_HEAD_INITIALIZER(head) \ |
| 145 |
{ NULL } |
| 146 |
|
| 147 |
#if defined(_WIN32) |
| 148 |
#if defined(SLIST_ENTRY) |
| 149 |
#undef SLIST_ENTRY |
| 150 |
#endif |
| 151 |
#endif |
| 152 |
|
| 153 |
#define SLIST_ENTRY(type) \ |
| 154 |
struct { \ |
| 155 |
struct type *sle_next; /* next element */ \ |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Singly-linked List functions. |
| 160 |
*/ |
| 161 |
#define SLIST_EMPTY(head) ((head)->slh_first == NULL) |
| 162 |
|
| 163 |
#define SLIST_FIRST(head) ((head)->slh_first) |
| 164 |
|
| 165 |
#define SLIST_FOREACH(var, head, field) \ |
| 166 |
for ((var) = SLIST_FIRST((head)); \ |
| 167 |
(var); \ |
| 168 |
(var) = SLIST_NEXT((var), field)) |
| 169 |
|
| 170 |
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ |
| 171 |
for ((var) = SLIST_FIRST((head)); \ |
| 172 |
(var) && ((tvar) = SLIST_NEXT((var), field), 1); \ |
| 173 |
(var) = (tvar)) |
| 174 |
|
| 175 |
#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ |
| 176 |
for ((varp) = &SLIST_FIRST((head)); \ |
| 177 |
((var) = *(varp)) != NULL; \ |
| 178 |
(varp) = &SLIST_NEXT((var), field)) |
| 179 |
|
| 180 |
#define SLIST_INIT(head) do { \ |
| 181 |
SLIST_FIRST((head)) = NULL; \ |
| 182 |
} while (0) |
| 183 |
|
| 184 |
#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ |
| 185 |
SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ |
| 186 |
SLIST_NEXT((slistelm), field) = (elm); \ |
| 187 |
} while (0) |
| 188 |
|
| 189 |
#define SLIST_INSERT_HEAD(head, elm, field) do { \ |
| 190 |
SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ |
| 191 |
SLIST_FIRST((head)) = (elm); \ |
| 192 |
} while (0) |
| 193 |
|
| 194 |
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) |
| 195 |
|
| 196 |
#define SLIST_REMOVE(head, elm, type, field) do { \ |
| 197 |
QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ |
| 198 |
if (SLIST_FIRST((head)) == (elm)) { \ |
| 199 |
SLIST_REMOVE_HEAD((head), field); \ |
| 200 |
} \ |
| 201 |
else { \ |
| 202 |
struct type *curelm = SLIST_FIRST((head)); \ |
| 203 |
while (SLIST_NEXT(curelm, field) != (elm)) \ |
| 204 |
curelm = SLIST_NEXT(curelm, field); \ |
| 205 |
SLIST_REMOVE_AFTER(curelm, field); \ |
| 206 |
} \ |
| 207 |
TRASHIT(*oldnext); \ |
| 208 |
} while (0) |
| 209 |
|
| 210 |
#define SLIST_REMOVE_AFTER(elm, field) do { \ |
| 211 |
SLIST_NEXT(elm, field) = \ |
| 212 |
SLIST_NEXT(SLIST_NEXT(elm, field), field); \ |
| 213 |
} while (0) |
| 214 |
|
| 215 |
#define SLIST_REMOVE_HEAD(head, field) do { \ |
| 216 |
SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ |
| 217 |
} while (0) |
| 218 |
|
| 219 |
#define SLIST_SWAP(head1, head2, type) do { \ |
| 220 |
struct type *swap_first = SLIST_FIRST(head1); \ |
| 221 |
SLIST_FIRST(head1) = SLIST_FIRST(head2); \ |
| 222 |
SLIST_FIRST(head2) = swap_first; \ |
| 223 |
} while (0) |
| 224 |
|
| 225 |
/* |
| 226 |
* Singly-linked Tail queue declarations. |
| 227 |
*/ |
| 228 |
#define STAILQ_HEAD(name, type) \ |
| 229 |
struct name { \ |
| 230 |
struct type *stqh_first;/* first element */ \ |
| 231 |
struct type **stqh_last;/* addr of last next element */ \ |
| 232 |
} |
| 233 |
|
| 234 |
#define STAILQ_HEAD_INITIALIZER(head) \ |
| 235 |
{ NULL, &(head).stqh_first } |
| 236 |
|
| 237 |
#define STAILQ_ENTRY(type) \ |
| 238 |
struct { \ |
| 239 |
struct type *stqe_next; /* next element */ \ |
| 240 |
} |
| 241 |
|
| 242 |
/* |
| 243 |
* Singly-linked Tail queue functions. |
| 244 |
*/ |
| 245 |
#define STAILQ_CONCAT(head1, head2) do { \ |
| 246 |
if (!STAILQ_EMPTY((head2))) { \ |
| 247 |
*(head1)->stqh_last = (head2)->stqh_first; \ |
| 248 |
(head1)->stqh_last = (head2)->stqh_last; \ |
| 249 |
STAILQ_INIT((head2)); \ |
| 250 |
} \ |
| 251 |
} while (0) |
| 252 |
|
| 253 |
#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) |
| 254 |
|
| 255 |
#define STAILQ_FIRST(head) ((head)->stqh_first) |
| 256 |
|
| 257 |
#define STAILQ_FOREACH(var, head, field) \ |
| 258 |
for((var) = STAILQ_FIRST((head)); \ |
| 259 |
(var); \ |
| 260 |
(var) = STAILQ_NEXT((var), field)) |
| 261 |
|
| 262 |
|
| 263 |
#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ |
| 264 |
for ((var) = STAILQ_FIRST((head)); \ |
| 265 |
(var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ |
| 266 |
(var) = (tvar)) |
| 267 |
|
| 268 |
#define STAILQ_INIT(head) do { \ |
| 269 |
STAILQ_FIRST((head)) = NULL; \ |
| 270 |
(head)->stqh_last = &STAILQ_FIRST((head)); \ |
| 271 |
} while (0) |
| 272 |
|
| 273 |
#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ |
| 274 |
if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ |
| 275 |
(head)->stqh_last = &STAILQ_NEXT((elm), field); \ |
| 276 |
STAILQ_NEXT((tqelm), field) = (elm); \ |
| 277 |
} while (0) |
| 278 |
|
| 279 |
#define STAILQ_INSERT_HEAD(head, elm, field) do { \ |
| 280 |
if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ |
| 281 |
(head)->stqh_last = &STAILQ_NEXT((elm), field); \ |
| 282 |
STAILQ_FIRST((head)) = (elm); \ |
| 283 |
} while (0) |
| 284 |
|
| 285 |
#define STAILQ_INSERT_TAIL(head, elm, field) do { \ |
| 286 |
STAILQ_NEXT((elm), field) = NULL; \ |
| 287 |
*(head)->stqh_last = (elm); \ |
| 288 |
(head)->stqh_last = &STAILQ_NEXT((elm), field); \ |
| 289 |
} while (0) |
| 290 |
|
| 291 |
#define STAILQ_LAST(head, type, field) \ |
| 292 |
(STAILQ_EMPTY((head)) ? \ |
| 293 |
NULL : \ |
| 294 |
((struct type *)(void *) \ |
| 295 |
((char *)((head)->stqh_last) - __offsetof(struct type, field)))) |
| 296 |
|
| 297 |
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) |
| 298 |
|
| 299 |
#define STAILQ_REMOVE(head, elm, type, field) do { \ |
| 300 |
QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ |
| 301 |
if (STAILQ_FIRST((head)) == (elm)) { \ |
| 302 |
STAILQ_REMOVE_HEAD((head), field); \ |
| 303 |
} \ |
| 304 |
else { \ |
| 305 |
struct type *curelm = STAILQ_FIRST((head)); \ |
| 306 |
while (STAILQ_NEXT(curelm, field) != (elm)) \ |
| 307 |
curelm = STAILQ_NEXT(curelm, field); \ |
| 308 |
STAILQ_REMOVE_AFTER(head, curelm, field); \ |
| 309 |
} \ |
| 310 |
TRASHIT(*oldnext); \ |
| 311 |
} while (0) |
| 312 |
|
| 313 |
#define STAILQ_REMOVE_AFTER(head, elm, field) do { \ |
| 314 |
if ((STAILQ_NEXT(elm, field) = \ |
| 315 |
STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ |
| 316 |
(head)->stqh_last = &STAILQ_NEXT((elm), field); \ |
| 317 |
} while (0) |
| 318 |
|
| 319 |
#define STAILQ_REMOVE_HEAD(head, field) do { \ |
| 320 |
if ((STAILQ_FIRST((head)) = \ |
| 321 |
STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ |
| 322 |
(head)->stqh_last = &STAILQ_FIRST((head)); \ |
| 323 |
} while (0) |
| 324 |
|
| 325 |
#define STAILQ_SWAP(head1, head2, type) do { \ |
| 326 |
struct type *swap_first = STAILQ_FIRST(head1); \ |
| 327 |
struct type **swap_last = (head1)->stqh_last; \ |
| 328 |
STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ |
| 329 |
(head1)->stqh_last = (head2)->stqh_last; \ |
| 330 |
STAILQ_FIRST(head2) = swap_first; \ |
| 331 |
(head2)->stqh_last = swap_last; \ |
| 332 |
if (STAILQ_EMPTY(head1)) \ |
| 333 |
(head1)->stqh_last = &STAILQ_FIRST(head1); \ |
| 334 |
if (STAILQ_EMPTY(head2)) \ |
| 335 |
(head2)->stqh_last = &STAILQ_FIRST(head2); \ |
| 336 |
} while (0) |
| 337 |
|
| 338 |
|
| 339 |
/* |
| 340 |
* List declarations. |
| 341 |
*/ |
| 342 |
#define LIST_HEAD(name, type) \ |
| 343 |
struct name { \ |
| 344 |
struct type *lh_first; /* first element */ \ |
| 345 |
} |
| 346 |
|
| 347 |
#define LIST_HEAD_INITIALIZER(head) \ |
| 348 |
{ NULL } |
| 349 |
|
| 350 |
#define LIST_ENTRY(type) \ |
| 351 |
struct { \ |
| 352 |
struct type *le_next; /* next element */ \ |
| 353 |
struct type **le_prev; /* address of previous next element */ \ |
| 354 |
} |
| 355 |
|
| 356 |
/* |
| 357 |
* List functions. |
| 358 |
*/ |
| 359 |
|
| 360 |
#if defined(INVARIANTS) |
| 361 |
#define QMD_LIST_CHECK_HEAD(head, field) do { \ |
| 362 |
if (LIST_FIRST((head)) != NULL && \ |
| 363 |
LIST_FIRST((head))->field.le_prev != \ |
| 364 |
&LIST_FIRST((head))) \ |
| 365 |
panic("Bad list head %p first->prev != head", (void *)(head)); \ |
| 366 |
} while (0) |
| 367 |
|
| 368 |
#define QMD_LIST_CHECK_NEXT(elm, field) do { \ |
| 369 |
if (LIST_NEXT((elm), field) != NULL && \ |
| 370 |
LIST_NEXT((elm), field)->field.le_prev != \ |
| 371 |
&((elm)->field.le_next)) \ |
| 372 |
panic("Bad link elm %p next->prev != elm", (void *)(elm)); \ |
| 373 |
} while (0) |
| 374 |
|
| 375 |
#define QMD_LIST_CHECK_PREV(elm, field) do { \ |
| 376 |
if (*(elm)->field.le_prev != (elm)) \ |
| 377 |
panic("Bad link elm %p prev->next != elm", (void *)(elm)); \ |
| 378 |
} while (0) |
| 379 |
#else |
| 380 |
#define QMD_LIST_CHECK_HEAD(head, field) |
| 381 |
#define QMD_LIST_CHECK_NEXT(elm, field) |
| 382 |
#define QMD_LIST_CHECK_PREV(elm, field) |
| 383 |
#endif /* (INVARIANTS) */ |
| 384 |
|
| 385 |
#define LIST_EMPTY(head) ((head)->lh_first == NULL) |
| 386 |
|
| 387 |
#define LIST_FIRST(head) ((head)->lh_first) |
| 388 |
|
| 389 |
#define LIST_FOREACH(var, head, field) \ |
| 390 |
for ((var) = LIST_FIRST((head)); \ |
| 391 |
(var); \ |
| 392 |
(var) = LIST_NEXT((var), field)) |
| 393 |
|
| 394 |
#define LIST_FOREACH_SAFE(var, head, field, tvar) \ |
| 395 |
for ((var) = LIST_FIRST((head)); \ |
| 396 |
(var) && ((tvar) = LIST_NEXT((var), field), 1); \ |
| 397 |
(var) = (tvar)) |
| 398 |
|
| 399 |
#define LIST_INIT(head) do { \ |
| 400 |
LIST_FIRST((head)) = NULL; \ |
| 401 |
} while (0) |
| 402 |
|
| 403 |
#define LIST_INSERT_AFTER(listelm, elm, field) do { \ |
| 404 |
QMD_LIST_CHECK_NEXT(listelm, field); \ |
| 405 |
if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ |
| 406 |
LIST_NEXT((listelm), field)->field.le_prev = \ |
| 407 |
&LIST_NEXT((elm), field); \ |
| 408 |
LIST_NEXT((listelm), field) = (elm); \ |
| 409 |
(elm)->field.le_prev = &LIST_NEXT((listelm), field); \ |
| 410 |
} while (0) |
| 411 |
|
| 412 |
#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ |
| 413 |
QMD_LIST_CHECK_PREV(listelm, field); \ |
| 414 |
(elm)->field.le_prev = (listelm)->field.le_prev; \ |
| 415 |
LIST_NEXT((elm), field) = (listelm); \ |
| 416 |
*(listelm)->field.le_prev = (elm); \ |
| 417 |
(listelm)->field.le_prev = &LIST_NEXT((elm), field); \ |
| 418 |
} while (0) |
| 419 |
|
| 420 |
#define LIST_INSERT_HEAD(head, elm, field) do { \ |
| 421 |
QMD_LIST_CHECK_HEAD((head), field); \ |
| 422 |
if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ |
| 423 |
LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ |
| 424 |
LIST_FIRST((head)) = (elm); \ |
| 425 |
(elm)->field.le_prev = &LIST_FIRST((head)); \ |
| 426 |
} while (0) |
| 427 |
|
| 428 |
#define LIST_NEXT(elm, field) ((elm)->field.le_next) |
| 429 |
|
| 430 |
#define LIST_REMOVE(elm, field) do { \ |
| 431 |
QMD_SAVELINK(oldnext, (elm)->field.le_next); \ |
| 432 |
QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ |
| 433 |
QMD_LIST_CHECK_NEXT(elm, field); \ |
| 434 |
QMD_LIST_CHECK_PREV(elm, field); \ |
| 435 |
if (LIST_NEXT((elm), field) != NULL) \ |
| 436 |
LIST_NEXT((elm), field)->field.le_prev = \ |
| 437 |
(elm)->field.le_prev; \ |
| 438 |
*(elm)->field.le_prev = LIST_NEXT((elm), field); \ |
| 439 |
TRASHIT(*oldnext); \ |
| 440 |
TRASHIT(*oldprev); \ |
| 441 |
} while (0) |
| 442 |
|
| 443 |
#define LIST_SWAP(head1, head2, type, field) do { \ |
| 444 |
struct type *swap_tmp = LIST_FIRST((head1)); \ |
| 445 |
LIST_FIRST((head1)) = LIST_FIRST((head2)); \ |
| 446 |
LIST_FIRST((head2)) = swap_tmp; \ |
| 447 |
if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ |
| 448 |
swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ |
| 449 |
if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ |
| 450 |
swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ |
| 451 |
} while (0) |
| 452 |
|
| 453 |
/* |
| 454 |
* Tail queue declarations. |
| 455 |
*/ |
| 456 |
#define TAILQ_HEAD(name, type) \ |
| 457 |
struct name { \ |
| 458 |
struct type *tqh_first; /* first element */ \ |
| 459 |
struct type **tqh_last; /* addr of last next element */ \ |
| 460 |
TRACEBUF \ |
| 461 |
} |
| 462 |
|
| 463 |
#define TAILQ_HEAD_INITIALIZER(head) \ |
| 464 |
{ NULL, &(head).tqh_first } |
| 465 |
|
| 466 |
#define TAILQ_ENTRY(type) \ |
| 467 |
struct { \ |
| 468 |
struct type *tqe_next; /* next element */ \ |
| 469 |
struct type **tqe_prev; /* address of previous next element */ \ |
| 470 |
TRACEBUF \ |
| 471 |
} |
| 472 |
|
| 473 |
/* |
| 474 |
* Tail queue functions. |
| 475 |
*/ |
| 476 |
#if defined(INVARIANTS) |
| 477 |
#define QMD_TAILQ_CHECK_HEAD(head, field) do { \ |
| 478 |
if (!TAILQ_EMPTY(head) && \ |
| 479 |
TAILQ_FIRST((head))->field.tqe_prev != \ |
| 480 |
&TAILQ_FIRST((head))) \ |
| 481 |
panic("Bad tailq head %p first->prev != head", (void *)(head)); \ |
| 482 |
} while (0) |
| 483 |
|
| 484 |
#define QMD_TAILQ_CHECK_TAIL(head, field) do { \ |
| 485 |
if (*(head)->tqh_last != NULL) \ |
| 486 |
panic("Bad tailq NEXT(%p->tqh_last) != NULL", (void *)(head)); \ |
| 487 |
} while (0) |
| 488 |
|
| 489 |
#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ |
| 490 |
if (TAILQ_NEXT((elm), field) != NULL && \ |
| 491 |
TAILQ_NEXT((elm), field)->field.tqe_prev != \ |
| 492 |
&((elm)->field.tqe_next)) \ |
| 493 |
panic("Bad link elm %p next->prev != elm", (void *)(elm)); \ |
| 494 |
} while (0) |
| 495 |
|
| 496 |
#define QMD_TAILQ_CHECK_PREV(elm, field) do { \ |
| 497 |
if (*(elm)->field.tqe_prev != (elm)) \ |
| 498 |
panic("Bad link elm %p prev->next != elm", (void *)(elm)); \ |
| 499 |
} while (0) |
| 500 |
#else |
| 501 |
#define QMD_TAILQ_CHECK_HEAD(head, field) |
| 502 |
#define QMD_TAILQ_CHECK_TAIL(head, headname) |
| 503 |
#define QMD_TAILQ_CHECK_NEXT(elm, field) |
| 504 |
#define QMD_TAILQ_CHECK_PREV(elm, field) |
| 505 |
#endif /* (INVARIANTS) */ |
| 506 |
|
| 507 |
#define TAILQ_CONCAT(head1, head2, field) do { \ |
| 508 |
if (!TAILQ_EMPTY(head2)) { \ |
| 509 |
*(head1)->tqh_last = (head2)->tqh_first; \ |
| 510 |
(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ |
| 511 |
(head1)->tqh_last = (head2)->tqh_last; \ |
| 512 |
TAILQ_INIT((head2)); \ |
| 513 |
QMD_TRACE_HEAD(head1); \ |
| 514 |
QMD_TRACE_HEAD(head2); \ |
| 515 |
} \ |
| 516 |
} while (0) |
| 517 |
|
| 518 |
#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) |
| 519 |
|
| 520 |
#define TAILQ_FIRST(head) ((head)->tqh_first) |
| 521 |
|
| 522 |
#define TAILQ_FOREACH(var, head, field) \ |
| 523 |
for ((var) = TAILQ_FIRST((head)); \ |
| 524 |
(var); \ |
| 525 |
(var) = TAILQ_NEXT((var), field)) |
| 526 |
|
| 527 |
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ |
| 528 |
for ((var) = TAILQ_FIRST((head)); \ |
| 529 |
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ |
| 530 |
(var) = (tvar)) |
| 531 |
|
| 532 |
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ |
| 533 |
for ((var) = TAILQ_LAST((head), headname); \ |
| 534 |
(var); \ |
| 535 |
(var) = TAILQ_PREV((var), headname, field)) |
| 536 |
|
| 537 |
#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ |
| 538 |
for ((var) = TAILQ_LAST((head), headname); \ |
| 539 |
(var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ |
| 540 |
(var) = (tvar)) |
| 541 |
|
| 542 |
#define TAILQ_INIT(head) do { \ |
| 543 |
TAILQ_FIRST((head)) = NULL; \ |
| 544 |
(head)->tqh_last = &TAILQ_FIRST((head)); \ |
| 545 |
QMD_TRACE_HEAD(head); \ |
| 546 |
} while (0) |
| 547 |
|
| 548 |
#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ |
| 549 |
QMD_TAILQ_CHECK_NEXT(listelm, field); \ |
| 550 |
if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ |
| 551 |
TAILQ_NEXT((elm), field)->field.tqe_prev = \ |
| 552 |
&TAILQ_NEXT((elm), field); \ |
| 553 |
else { \ |
| 554 |
(head)->tqh_last = &TAILQ_NEXT((elm), field); \ |
| 555 |
QMD_TRACE_HEAD(head); \ |
| 556 |
} \ |
| 557 |
TAILQ_NEXT((listelm), field) = (elm); \ |
| 558 |
(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ |
| 559 |
QMD_TRACE_ELEM(&(elm)->field); \ |
| 560 |
QMD_TRACE_ELEM(&listelm->field); \ |
| 561 |
} while (0) |
| 562 |
|
| 563 |
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ |
| 564 |
QMD_TAILQ_CHECK_PREV(listelm, field); \ |
| 565 |
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ |
| 566 |
TAILQ_NEXT((elm), field) = (listelm); \ |
| 567 |
*(listelm)->field.tqe_prev = (elm); \ |
| 568 |
(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ |
| 569 |
QMD_TRACE_ELEM(&(elm)->field); \ |
| 570 |
QMD_TRACE_ELEM(&listelm->field); \ |
| 571 |
} while (0) |
| 572 |
|
| 573 |
#define TAILQ_INSERT_HEAD(head, elm, field) do { \ |
| 574 |
QMD_TAILQ_CHECK_HEAD(head, field); \ |
| 575 |
if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ |
| 576 |
TAILQ_FIRST((head))->field.tqe_prev = \ |
| 577 |
&TAILQ_NEXT((elm), field); \ |
| 578 |
else \ |
| 579 |
(head)->tqh_last = &TAILQ_NEXT((elm), field); \ |
| 580 |
TAILQ_FIRST((head)) = (elm); \ |
| 581 |
(elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ |
| 582 |
QMD_TRACE_HEAD(head); \ |
| 583 |
QMD_TRACE_ELEM(&(elm)->field); \ |
| 584 |
} while (0) |
| 585 |
|
| 586 |
#define TAILQ_INSERT_TAIL(head, elm, field) do { \ |
| 587 |
QMD_TAILQ_CHECK_TAIL(head, field); \ |
| 588 |
TAILQ_NEXT((elm), field) = NULL; \ |
| 589 |
(elm)->field.tqe_prev = (head)->tqh_last; \ |
| 590 |
*(head)->tqh_last = (elm); \ |
| 591 |
(head)->tqh_last = &TAILQ_NEXT((elm), field); \ |
| 592 |
QMD_TRACE_HEAD(head); \ |
| 593 |
QMD_TRACE_ELEM(&(elm)->field); \ |
| 594 |
} while (0) |
| 595 |
|
| 596 |
#define TAILQ_LAST(head, headname) \ |
| 597 |
(*(((struct headname *)((head)->tqh_last))->tqh_last)) |
| 598 |
|
| 599 |
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) |
| 600 |
|
| 601 |
#define TAILQ_PREV(elm, headname, field) \ |
| 602 |
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) |
| 603 |
|
| 604 |
#define TAILQ_REMOVE(head, elm, field) do { \ |
| 605 |
QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ |
| 606 |
QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ |
| 607 |
QMD_TAILQ_CHECK_NEXT(elm, field); \ |
| 608 |
QMD_TAILQ_CHECK_PREV(elm, field); \ |
| 609 |
if ((TAILQ_NEXT((elm), field)) != NULL) \ |
| 610 |
TAILQ_NEXT((elm), field)->field.tqe_prev = \ |
| 611 |
(elm)->field.tqe_prev; \ |
| 612 |
else { \ |
| 613 |
(head)->tqh_last = (elm)->field.tqe_prev; \ |
| 614 |
QMD_TRACE_HEAD(head); \ |
| 615 |
} \ |
| 616 |
*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ |
| 617 |
TRASHIT(*oldnext); \ |
| 618 |
TRASHIT(*oldprev); \ |
| 619 |
QMD_TRACE_ELEM(&(elm)->field); \ |
| 620 |
} while (0) |
| 621 |
|
| 622 |
#define TAILQ_SWAP(head1, head2, type, field) do { \ |
| 623 |
struct type *swap_first = (head1)->tqh_first; \ |
| 624 |
struct type **swap_last = (head1)->tqh_last; \ |
| 625 |
(head1)->tqh_first = (head2)->tqh_first; \ |
| 626 |
(head1)->tqh_last = (head2)->tqh_last; \ |
| 627 |
(head2)->tqh_first = swap_first; \ |
| 628 |
(head2)->tqh_last = swap_last; \ |
| 629 |
if ((swap_first = (head1)->tqh_first) != NULL) \ |
| 630 |
swap_first->field.tqe_prev = &(head1)->tqh_first; \ |
| 631 |
else \ |
| 632 |
(head1)->tqh_last = &(head1)->tqh_first; \ |
| 633 |
if ((swap_first = (head2)->tqh_first) != NULL) \ |
| 634 |
swap_first->field.tqe_prev = &(head2)->tqh_first; \ |
| 635 |
else \ |
| 636 |
(head2)->tqh_last = &(head2)->tqh_first; \ |
| 637 |
} while (0) |
| 638 |
|
| 639 |
#endif /* !_SYS_QUEUE_H_ */ |