michael@0: /*- michael@0: * Copyright (c) 1991, 1993 michael@0: * The Regents of the University of California. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 4. Neither the name of the University nor the names of its contributors michael@0: * may be used to endorse or promote products derived from this software michael@0: * without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: #ifndef _USER_QUEUE_H_ michael@0: #define _USER_QUEUE_H_ michael@0: michael@0: #if !defined (__Userspace_os_Windows) michael@0: #include michael@0: #endif michael@0: /* michael@0: * This file defines four types of data structures: singly-linked lists, michael@0: * singly-linked tail queues, lists and tail queues. michael@0: * michael@0: * A singly-linked list is headed by a single forward pointer. The elements michael@0: * are singly linked for minimum space and pointer manipulation overhead at michael@0: * the expense of O(n) removal for arbitrary elements. New elements can be michael@0: * added to the list after an existing element or at the head of the list. michael@0: * Elements being removed from the head of the list should use the explicit michael@0: * macro for this purpose for optimum efficiency. A singly-linked list may michael@0: * only be traversed in the forward direction. Singly-linked lists are ideal michael@0: * for applications with large datasets and few or no removals or for michael@0: * implementing a LIFO queue. michael@0: * michael@0: * A singly-linked tail queue is headed by a pair of pointers, one to the michael@0: * head of the list and the other to the tail of the list. The elements are michael@0: * singly linked for minimum space and pointer manipulation overhead at the michael@0: * expense of O(n) removal for arbitrary elements. New elements can be added michael@0: * to the list after an existing element, at the head of the list, or at the michael@0: * end of the list. Elements being removed from the head of the tail queue michael@0: * should use the explicit macro for this purpose for optimum efficiency. michael@0: * A singly-linked tail queue may only be traversed in the forward direction. michael@0: * Singly-linked tail queues are ideal for applications with large datasets michael@0: * and few or no removals or for implementing a FIFO queue. michael@0: * michael@0: * A list is headed by a single forward pointer (or an array of forward michael@0: * pointers for a hash table header). The elements are doubly linked michael@0: * so that an arbitrary element can be removed without a need to michael@0: * traverse the list. New elements can be added to the list before michael@0: * or after an existing element or at the head of the list. A list michael@0: * may only be traversed in the forward direction. michael@0: * michael@0: * A tail queue is headed by a pair of pointers, one to the head of the michael@0: * list and the other to the tail of the list. The elements are doubly michael@0: * linked so that an arbitrary element can be removed without a need to michael@0: * traverse the list. New elements can be added to the list before or michael@0: * after an existing element, at the head of the list, or at the end of michael@0: * the list. A tail queue may be traversed in either direction. michael@0: * michael@0: * For details on the use of these macros, see the queue(3) manual page. michael@0: * michael@0: * michael@0: * SLIST LIST STAILQ TAILQ michael@0: * _HEAD + + + + michael@0: * _HEAD_INITIALIZER + + + + michael@0: * _ENTRY + + + + michael@0: * _INIT + + + + michael@0: * _EMPTY + + + + michael@0: * _FIRST + + + + michael@0: * _NEXT + + + + michael@0: * _PREV - - - + michael@0: * _LAST - - + + michael@0: * _FOREACH + + + + michael@0: * _FOREACH_SAFE + + + + michael@0: * _FOREACH_REVERSE - - - + michael@0: * _FOREACH_REVERSE_SAFE - - - + michael@0: * _INSERT_HEAD + + + + michael@0: * _INSERT_BEFORE - + - + michael@0: * _INSERT_AFTER + + + + michael@0: * _INSERT_TAIL - - + + michael@0: * _CONCAT - - + + michael@0: * _REMOVE_AFTER + - + - michael@0: * _REMOVE_HEAD + - + - michael@0: * _REMOVE + + + + michael@0: * _SWAP + + + + michael@0: * michael@0: */ michael@0: #ifdef QUEUE_MACRO_DEBUG michael@0: /* Store the last 2 places the queue element or head was altered */ michael@0: struct qm_trace { michael@0: char * lastfile; michael@0: int lastline; michael@0: char * prevfile; michael@0: int prevline; michael@0: }; michael@0: michael@0: #define TRACEBUF struct qm_trace trace; michael@0: #define TRASHIT(x) do {(x) = (void *)-1;} while (0) michael@0: #define QMD_SAVELINK(name, link) void **name = (void *)&(link) michael@0: michael@0: #define QMD_TRACE_HEAD(head) do { \ michael@0: (head)->trace.prevline = (head)->trace.lastline; \ michael@0: (head)->trace.prevfile = (head)->trace.lastfile; \ michael@0: (head)->trace.lastline = __LINE__; \ michael@0: (head)->trace.lastfile = __FILE__; \ michael@0: } while (0) michael@0: michael@0: #define QMD_TRACE_ELEM(elem) do { \ michael@0: (elem)->trace.prevline = (elem)->trace.lastline; \ michael@0: (elem)->trace.prevfile = (elem)->trace.lastfile; \ michael@0: (elem)->trace.lastline = __LINE__; \ michael@0: (elem)->trace.lastfile = __FILE__; \ michael@0: } while (0) michael@0: michael@0: #else michael@0: #define QMD_TRACE_ELEM(elem) michael@0: #define QMD_TRACE_HEAD(head) michael@0: #define QMD_SAVELINK(name, link) michael@0: #define TRACEBUF michael@0: #define TRASHIT(x) michael@0: #endif /* QUEUE_MACRO_DEBUG */ michael@0: michael@0: /* michael@0: * Singly-linked List declarations. michael@0: */ michael@0: #define SLIST_HEAD(name, type) \ michael@0: struct name { \ michael@0: struct type *slh_first; /* first element */ \ michael@0: } michael@0: michael@0: #define SLIST_HEAD_INITIALIZER(head) \ michael@0: { NULL } michael@0: michael@0: #if defined (__Userspace_os_Windows) michael@0: #if defined (SLIST_ENTRY) michael@0: #undef SLIST_ENTRY michael@0: #endif michael@0: #endif michael@0: michael@0: #define SLIST_ENTRY(type) \ michael@0: struct { \ michael@0: struct type *sle_next; /* next element */ \ michael@0: } michael@0: michael@0: /* michael@0: * Singly-linked List functions. michael@0: */ michael@0: #define SLIST_EMPTY(head) ((head)->slh_first == NULL) michael@0: michael@0: #define SLIST_FIRST(head) ((head)->slh_first) michael@0: michael@0: #define SLIST_FOREACH(var, head, field) \ michael@0: for ((var) = SLIST_FIRST((head)); \ michael@0: (var); \ michael@0: (var) = SLIST_NEXT((var), field)) michael@0: michael@0: #define SLIST_FOREACH_SAFE(var, head, field, tvar) \ michael@0: for ((var) = SLIST_FIRST((head)); \ michael@0: (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ michael@0: (var) = (tvar)) michael@0: michael@0: #define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ michael@0: for ((varp) = &SLIST_FIRST((head)); \ michael@0: ((var) = *(varp)) != NULL; \ michael@0: (varp) = &SLIST_NEXT((var), field)) michael@0: michael@0: #define SLIST_INIT(head) do { \ michael@0: SLIST_FIRST((head)) = NULL; \ michael@0: } while (0) michael@0: michael@0: #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ michael@0: SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ michael@0: SLIST_NEXT((slistelm), field) = (elm); \ michael@0: } while (0) michael@0: michael@0: #define SLIST_INSERT_HEAD(head, elm, field) do { \ michael@0: SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ michael@0: SLIST_FIRST((head)) = (elm); \ michael@0: } while (0) michael@0: michael@0: #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) michael@0: michael@0: #define SLIST_REMOVE(head, elm, type, field) do { \ michael@0: QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ michael@0: if (SLIST_FIRST((head)) == (elm)) { \ michael@0: SLIST_REMOVE_HEAD((head), field); \ michael@0: } \ michael@0: else { \ michael@0: struct type *curelm = SLIST_FIRST((head)); \ michael@0: while (SLIST_NEXT(curelm, field) != (elm)) \ michael@0: curelm = SLIST_NEXT(curelm, field); \ michael@0: SLIST_REMOVE_AFTER(curelm, field); \ michael@0: } \ michael@0: TRASHIT(*oldnext); \ michael@0: } while (0) michael@0: michael@0: #define SLIST_REMOVE_AFTER(elm, field) do { \ michael@0: SLIST_NEXT(elm, field) = \ michael@0: SLIST_NEXT(SLIST_NEXT(elm, field), field); \ michael@0: } while (0) michael@0: michael@0: #define SLIST_REMOVE_HEAD(head, field) do { \ michael@0: SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ michael@0: } while (0) michael@0: michael@0: #define SLIST_SWAP(head1, head2, type) do { \ michael@0: struct type *swap_first = SLIST_FIRST(head1); \ michael@0: SLIST_FIRST(head1) = SLIST_FIRST(head2); \ michael@0: SLIST_FIRST(head2) = swap_first; \ michael@0: } while (0) michael@0: michael@0: /* michael@0: * Singly-linked Tail queue declarations. michael@0: */ michael@0: #define STAILQ_HEAD(name, type) \ michael@0: struct name { \ michael@0: struct type *stqh_first;/* first element */ \ michael@0: struct type **stqh_last;/* addr of last next element */ \ michael@0: } michael@0: michael@0: #define STAILQ_HEAD_INITIALIZER(head) \ michael@0: { NULL, &(head).stqh_first } michael@0: michael@0: #define STAILQ_ENTRY(type) \ michael@0: struct { \ michael@0: struct type *stqe_next; /* next element */ \ michael@0: } michael@0: michael@0: /* michael@0: * Singly-linked Tail queue functions. michael@0: */ michael@0: #define STAILQ_CONCAT(head1, head2) do { \ michael@0: if (!STAILQ_EMPTY((head2))) { \ michael@0: *(head1)->stqh_last = (head2)->stqh_first; \ michael@0: (head1)->stqh_last = (head2)->stqh_last; \ michael@0: STAILQ_INIT((head2)); \ michael@0: } \ michael@0: } while (0) michael@0: michael@0: #define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) michael@0: michael@0: #define STAILQ_FIRST(head) ((head)->stqh_first) michael@0: michael@0: #define STAILQ_FOREACH(var, head, field) \ michael@0: for((var) = STAILQ_FIRST((head)); \ michael@0: (var); \ michael@0: (var) = STAILQ_NEXT((var), field)) michael@0: michael@0: michael@0: #define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ michael@0: for ((var) = STAILQ_FIRST((head)); \ michael@0: (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ michael@0: (var) = (tvar)) michael@0: michael@0: #define STAILQ_INIT(head) do { \ michael@0: STAILQ_FIRST((head)) = NULL; \ michael@0: (head)->stqh_last = &STAILQ_FIRST((head)); \ michael@0: } while (0) michael@0: michael@0: #define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ michael@0: if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ michael@0: (head)->stqh_last = &STAILQ_NEXT((elm), field); \ michael@0: STAILQ_NEXT((tqelm), field) = (elm); \ michael@0: } while (0) michael@0: michael@0: #define STAILQ_INSERT_HEAD(head, elm, field) do { \ michael@0: if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ michael@0: (head)->stqh_last = &STAILQ_NEXT((elm), field); \ michael@0: STAILQ_FIRST((head)) = (elm); \ michael@0: } while (0) michael@0: michael@0: #define STAILQ_INSERT_TAIL(head, elm, field) do { \ michael@0: STAILQ_NEXT((elm), field) = NULL; \ michael@0: *(head)->stqh_last = (elm); \ michael@0: (head)->stqh_last = &STAILQ_NEXT((elm), field); \ michael@0: } while (0) michael@0: michael@0: #define STAILQ_LAST(head, type, field) \ michael@0: (STAILQ_EMPTY((head)) ? \ michael@0: NULL : \ michael@0: ((struct type *)(void *) \ michael@0: ((char *)((head)->stqh_last) - __offsetof(struct type, field)))) michael@0: michael@0: #define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) michael@0: michael@0: #define STAILQ_REMOVE(head, elm, type, field) do { \ michael@0: QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ michael@0: if (STAILQ_FIRST((head)) == (elm)) { \ michael@0: STAILQ_REMOVE_HEAD((head), field); \ michael@0: } \ michael@0: else { \ michael@0: struct type *curelm = STAILQ_FIRST((head)); \ michael@0: while (STAILQ_NEXT(curelm, field) != (elm)) \ michael@0: curelm = STAILQ_NEXT(curelm, field); \ michael@0: STAILQ_REMOVE_AFTER(head, curelm, field); \ michael@0: } \ michael@0: TRASHIT(*oldnext); \ michael@0: } while (0) michael@0: michael@0: #define STAILQ_REMOVE_AFTER(head, elm, field) do { \ michael@0: if ((STAILQ_NEXT(elm, field) = \ michael@0: STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ michael@0: (head)->stqh_last = &STAILQ_NEXT((elm), field); \ michael@0: } while (0) michael@0: michael@0: #define STAILQ_REMOVE_HEAD(head, field) do { \ michael@0: if ((STAILQ_FIRST((head)) = \ michael@0: STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ michael@0: (head)->stqh_last = &STAILQ_FIRST((head)); \ michael@0: } while (0) michael@0: michael@0: #define STAILQ_SWAP(head1, head2, type) do { \ michael@0: struct type *swap_first = STAILQ_FIRST(head1); \ michael@0: struct type **swap_last = (head1)->stqh_last; \ michael@0: STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ michael@0: (head1)->stqh_last = (head2)->stqh_last; \ michael@0: STAILQ_FIRST(head2) = swap_first; \ michael@0: (head2)->stqh_last = swap_last; \ michael@0: if (STAILQ_EMPTY(head1)) \ michael@0: (head1)->stqh_last = &STAILQ_FIRST(head1); \ michael@0: if (STAILQ_EMPTY(head2)) \ michael@0: (head2)->stqh_last = &STAILQ_FIRST(head2); \ michael@0: } while (0) michael@0: michael@0: michael@0: /* michael@0: * List declarations. michael@0: */ michael@0: #define LIST_HEAD(name, type) \ michael@0: struct name { \ michael@0: struct type *lh_first; /* first element */ \ michael@0: } michael@0: michael@0: #define LIST_HEAD_INITIALIZER(head) \ michael@0: { NULL } michael@0: michael@0: #define LIST_ENTRY(type) \ michael@0: struct { \ michael@0: struct type *le_next; /* next element */ \ michael@0: struct type **le_prev; /* address of previous next element */ \ michael@0: } michael@0: michael@0: /* michael@0: * List functions. michael@0: */ michael@0: michael@0: #if defined(INVARIANTS) michael@0: #define QMD_LIST_CHECK_HEAD(head, field) do { \ michael@0: if (LIST_FIRST((head)) != NULL && \ michael@0: LIST_FIRST((head))->field.le_prev != \ michael@0: &LIST_FIRST((head))) \ michael@0: panic("Bad list head %p first->prev != head", (void *)(head)); \ michael@0: } while (0) michael@0: michael@0: #define QMD_LIST_CHECK_NEXT(elm, field) do { \ michael@0: if (LIST_NEXT((elm), field) != NULL && \ michael@0: LIST_NEXT((elm), field)->field.le_prev != \ michael@0: &((elm)->field.le_next)) \ michael@0: panic("Bad link elm %p next->prev != elm", (void *)(elm)); \ michael@0: } while (0) michael@0: michael@0: #define QMD_LIST_CHECK_PREV(elm, field) do { \ michael@0: if (*(elm)->field.le_prev != (elm)) \ michael@0: panic("Bad link elm %p prev->next != elm", (void *)(elm)); \ michael@0: } while (0) michael@0: #else michael@0: #define QMD_LIST_CHECK_HEAD(head, field) michael@0: #define QMD_LIST_CHECK_NEXT(elm, field) michael@0: #define QMD_LIST_CHECK_PREV(elm, field) michael@0: #endif /* (INVARIANTS) */ michael@0: michael@0: #define LIST_EMPTY(head) ((head)->lh_first == NULL) michael@0: michael@0: #define LIST_FIRST(head) ((head)->lh_first) michael@0: michael@0: #define LIST_FOREACH(var, head, field) \ michael@0: for ((var) = LIST_FIRST((head)); \ michael@0: (var); \ michael@0: (var) = LIST_NEXT((var), field)) michael@0: michael@0: #define LIST_FOREACH_SAFE(var, head, field, tvar) \ michael@0: for ((var) = LIST_FIRST((head)); \ michael@0: (var) && ((tvar) = LIST_NEXT((var), field), 1); \ michael@0: (var) = (tvar)) michael@0: michael@0: #define LIST_INIT(head) do { \ michael@0: LIST_FIRST((head)) = NULL; \ michael@0: } while (0) michael@0: michael@0: #define LIST_INSERT_AFTER(listelm, elm, field) do { \ michael@0: QMD_LIST_CHECK_NEXT(listelm, field); \ michael@0: if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ michael@0: LIST_NEXT((listelm), field)->field.le_prev = \ michael@0: &LIST_NEXT((elm), field); \ michael@0: LIST_NEXT((listelm), field) = (elm); \ michael@0: (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ michael@0: } while (0) michael@0: michael@0: #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ michael@0: QMD_LIST_CHECK_PREV(listelm, field); \ michael@0: (elm)->field.le_prev = (listelm)->field.le_prev; \ michael@0: LIST_NEXT((elm), field) = (listelm); \ michael@0: *(listelm)->field.le_prev = (elm); \ michael@0: (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ michael@0: } while (0) michael@0: michael@0: #define LIST_INSERT_HEAD(head, elm, field) do { \ michael@0: QMD_LIST_CHECK_HEAD((head), field); \ michael@0: if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ michael@0: LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ michael@0: LIST_FIRST((head)) = (elm); \ michael@0: (elm)->field.le_prev = &LIST_FIRST((head)); \ michael@0: } while (0) michael@0: michael@0: #define LIST_NEXT(elm, field) ((elm)->field.le_next) michael@0: michael@0: #define LIST_REMOVE(elm, field) do { \ michael@0: QMD_SAVELINK(oldnext, (elm)->field.le_next); \ michael@0: QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ michael@0: QMD_LIST_CHECK_NEXT(elm, field); \ michael@0: QMD_LIST_CHECK_PREV(elm, field); \ michael@0: if (LIST_NEXT((elm), field) != NULL) \ michael@0: LIST_NEXT((elm), field)->field.le_prev = \ michael@0: (elm)->field.le_prev; \ michael@0: *(elm)->field.le_prev = LIST_NEXT((elm), field); \ michael@0: TRASHIT(*oldnext); \ michael@0: TRASHIT(*oldprev); \ michael@0: } while (0) michael@0: michael@0: #define LIST_SWAP(head1, head2, type, field) do { \ michael@0: struct type *swap_tmp = LIST_FIRST((head1)); \ michael@0: LIST_FIRST((head1)) = LIST_FIRST((head2)); \ michael@0: LIST_FIRST((head2)) = swap_tmp; \ michael@0: if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ michael@0: swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ michael@0: if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ michael@0: swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ michael@0: } while (0) michael@0: michael@0: /* michael@0: * Tail queue declarations. michael@0: */ michael@0: #define TAILQ_HEAD(name, type) \ michael@0: struct name { \ michael@0: struct type *tqh_first; /* first element */ \ michael@0: struct type **tqh_last; /* addr of last next element */ \ michael@0: TRACEBUF \ michael@0: } michael@0: michael@0: #define TAILQ_HEAD_INITIALIZER(head) \ michael@0: { NULL, &(head).tqh_first } michael@0: michael@0: #define TAILQ_ENTRY(type) \ michael@0: struct { \ michael@0: struct type *tqe_next; /* next element */ \ michael@0: struct type **tqe_prev; /* address of previous next element */ \ michael@0: TRACEBUF \ michael@0: } michael@0: michael@0: /* michael@0: * Tail queue functions. michael@0: */ michael@0: #if defined(INVARIANTS) michael@0: #define QMD_TAILQ_CHECK_HEAD(head, field) do { \ michael@0: if (!TAILQ_EMPTY(head) && \ michael@0: TAILQ_FIRST((head))->field.tqe_prev != \ michael@0: &TAILQ_FIRST((head))) \ michael@0: panic("Bad tailq head %p first->prev != head", (void *)(head)); \ michael@0: } while (0) michael@0: michael@0: #define QMD_TAILQ_CHECK_TAIL(head, field) do { \ michael@0: if (*(head)->tqh_last != NULL) \ michael@0: panic("Bad tailq NEXT(%p->tqh_last) != NULL", (void *)(head)); \ michael@0: } while (0) michael@0: michael@0: #define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ michael@0: if (TAILQ_NEXT((elm), field) != NULL && \ michael@0: TAILQ_NEXT((elm), field)->field.tqe_prev != \ michael@0: &((elm)->field.tqe_next)) \ michael@0: panic("Bad link elm %p next->prev != elm", (void *)(elm)); \ michael@0: } while (0) michael@0: michael@0: #define QMD_TAILQ_CHECK_PREV(elm, field) do { \ michael@0: if (*(elm)->field.tqe_prev != (elm)) \ michael@0: panic("Bad link elm %p prev->next != elm", (void *)(elm)); \ michael@0: } while (0) michael@0: #else michael@0: #define QMD_TAILQ_CHECK_HEAD(head, field) michael@0: #define QMD_TAILQ_CHECK_TAIL(head, headname) michael@0: #define QMD_TAILQ_CHECK_NEXT(elm, field) michael@0: #define QMD_TAILQ_CHECK_PREV(elm, field) michael@0: #endif /* (INVARIANTS) */ michael@0: michael@0: #define TAILQ_CONCAT(head1, head2, field) do { \ michael@0: if (!TAILQ_EMPTY(head2)) { \ michael@0: *(head1)->tqh_last = (head2)->tqh_first; \ michael@0: (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ michael@0: (head1)->tqh_last = (head2)->tqh_last; \ michael@0: TAILQ_INIT((head2)); \ michael@0: QMD_TRACE_HEAD(head1); \ michael@0: QMD_TRACE_HEAD(head2); \ michael@0: } \ michael@0: } while (0) michael@0: michael@0: #define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) michael@0: michael@0: #define TAILQ_FIRST(head) ((head)->tqh_first) michael@0: michael@0: #define TAILQ_FOREACH(var, head, field) \ michael@0: for ((var) = TAILQ_FIRST((head)); \ michael@0: (var); \ michael@0: (var) = TAILQ_NEXT((var), field)) michael@0: michael@0: #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ michael@0: for ((var) = TAILQ_FIRST((head)); \ michael@0: (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ michael@0: (var) = (tvar)) michael@0: michael@0: #define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ michael@0: for ((var) = TAILQ_LAST((head), headname); \ michael@0: (var); \ michael@0: (var) = TAILQ_PREV((var), headname, field)) michael@0: michael@0: #define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ michael@0: for ((var) = TAILQ_LAST((head), headname); \ michael@0: (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ michael@0: (var) = (tvar)) michael@0: michael@0: #define TAILQ_INIT(head) do { \ michael@0: TAILQ_FIRST((head)) = NULL; \ michael@0: (head)->tqh_last = &TAILQ_FIRST((head)); \ michael@0: QMD_TRACE_HEAD(head); \ michael@0: } while (0) michael@0: michael@0: #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ michael@0: QMD_TAILQ_CHECK_NEXT(listelm, field); \ michael@0: if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ michael@0: TAILQ_NEXT((elm), field)->field.tqe_prev = \ michael@0: &TAILQ_NEXT((elm), field); \ michael@0: else { \ michael@0: (head)->tqh_last = &TAILQ_NEXT((elm), field); \ michael@0: QMD_TRACE_HEAD(head); \ michael@0: } \ michael@0: TAILQ_NEXT((listelm), field) = (elm); \ michael@0: (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ michael@0: QMD_TRACE_ELEM(&(elm)->field); \ michael@0: QMD_TRACE_ELEM(&listelm->field); \ michael@0: } while (0) michael@0: michael@0: #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ michael@0: QMD_TAILQ_CHECK_PREV(listelm, field); \ michael@0: (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ michael@0: TAILQ_NEXT((elm), field) = (listelm); \ michael@0: *(listelm)->field.tqe_prev = (elm); \ michael@0: (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ michael@0: QMD_TRACE_ELEM(&(elm)->field); \ michael@0: QMD_TRACE_ELEM(&listelm->field); \ michael@0: } while (0) michael@0: michael@0: #define TAILQ_INSERT_HEAD(head, elm, field) do { \ michael@0: QMD_TAILQ_CHECK_HEAD(head, field); \ michael@0: if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ michael@0: TAILQ_FIRST((head))->field.tqe_prev = \ michael@0: &TAILQ_NEXT((elm), field); \ michael@0: else \ michael@0: (head)->tqh_last = &TAILQ_NEXT((elm), field); \ michael@0: TAILQ_FIRST((head)) = (elm); \ michael@0: (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ michael@0: QMD_TRACE_HEAD(head); \ michael@0: QMD_TRACE_ELEM(&(elm)->field); \ michael@0: } while (0) michael@0: michael@0: #define TAILQ_INSERT_TAIL(head, elm, field) do { \ michael@0: QMD_TAILQ_CHECK_TAIL(head, field); \ michael@0: TAILQ_NEXT((elm), field) = NULL; \ michael@0: (elm)->field.tqe_prev = (head)->tqh_last; \ michael@0: *(head)->tqh_last = (elm); \ michael@0: (head)->tqh_last = &TAILQ_NEXT((elm), field); \ michael@0: QMD_TRACE_HEAD(head); \ michael@0: QMD_TRACE_ELEM(&(elm)->field); \ michael@0: } while (0) michael@0: michael@0: #define TAILQ_LAST(head, headname) \ michael@0: (*(((struct headname *)((head)->tqh_last))->tqh_last)) michael@0: michael@0: #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) michael@0: michael@0: #define TAILQ_PREV(elm, headname, field) \ michael@0: (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) michael@0: michael@0: #define TAILQ_REMOVE(head, elm, field) do { \ michael@0: QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ michael@0: QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ michael@0: QMD_TAILQ_CHECK_NEXT(elm, field); \ michael@0: QMD_TAILQ_CHECK_PREV(elm, field); \ michael@0: if ((TAILQ_NEXT((elm), field)) != NULL) \ michael@0: TAILQ_NEXT((elm), field)->field.tqe_prev = \ michael@0: (elm)->field.tqe_prev; \ michael@0: else { \ michael@0: (head)->tqh_last = (elm)->field.tqe_prev; \ michael@0: QMD_TRACE_HEAD(head); \ michael@0: } \ michael@0: *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ michael@0: TRASHIT(*oldnext); \ michael@0: TRASHIT(*oldprev); \ michael@0: QMD_TRACE_ELEM(&(elm)->field); \ michael@0: } while (0) michael@0: michael@0: #define TAILQ_SWAP(head1, head2, type, field) do { \ michael@0: struct type *swap_first = (head1)->tqh_first; \ michael@0: struct type **swap_last = (head1)->tqh_last; \ michael@0: (head1)->tqh_first = (head2)->tqh_first; \ michael@0: (head1)->tqh_last = (head2)->tqh_last; \ michael@0: (head2)->tqh_first = swap_first; \ michael@0: (head2)->tqh_last = swap_last; \ michael@0: if ((swap_first = (head1)->tqh_first) != NULL) \ michael@0: swap_first->field.tqe_prev = &(head1)->tqh_first; \ michael@0: else \ michael@0: (head1)->tqh_last = &(head1)->tqh_first; \ michael@0: if ((swap_first = (head2)->tqh_first) != NULL) \ michael@0: swap_first->field.tqe_prev = &(head2)->tqh_first; \ michael@0: else \ michael@0: (head2)->tqh_last = &(head2)->tqh_first; \ michael@0: } while (0) michael@0: michael@0: #endif /* !_SYS_QUEUE_H_ */