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: * 3. ***REMOVED*** - see michael@0: * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change 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: * @(#)queue.h 8.3 (Berkeley) 12/13/93 michael@0: */ michael@0: michael@0: #ifndef _QUEUE_H_ michael@0: #define _QUEUE_H_ michael@0: michael@0: /* michael@0: * This file defines three types of data structures: lists, tail queues, michael@0: * and circular queues. 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 after michael@0: * an existing element or at the head of the list. A list may only be michael@0: * 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 after michael@0: * an existing element, at the head of the list, or at the end of the michael@0: * list. A tail queue may only be traversed in the forward direction. michael@0: * michael@0: * A circle 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 after michael@0: * an existing element, at the head of the list, or at the end of the list. michael@0: * A circle queue may be traversed in either direction, but has a more michael@0: * complex end of list detection. michael@0: * michael@0: * For details on the use of these macros, see the queue(3) manual page. michael@0: */ michael@0: michael@0: /* michael@0: * List definitions. 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_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: #define LIST_INIT(head) { \ michael@0: (head)->lh_first = NULL; \ michael@0: } michael@0: michael@0: #define LIST_INSERT_AFTER(listelm, elm, field) { \ michael@0: if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ michael@0: (listelm)->field.le_next->field.le_prev = \ michael@0: &(elm)->field.le_next; \ michael@0: (listelm)->field.le_next = (elm); \ michael@0: (elm)->field.le_prev = &(listelm)->field.le_next; \ michael@0: } michael@0: michael@0: #define LIST_INSERT_HEAD(head, elm, field) { \ michael@0: if (((elm)->field.le_next = (head)->lh_first) != NULL) \ michael@0: (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ michael@0: (head)->lh_first = (elm); \ michael@0: (elm)->field.le_prev = &(head)->lh_first; \ michael@0: } michael@0: michael@0: #define LIST_REMOVE(elm, field) { \ michael@0: if ((elm)->field.le_next != NULL) \ michael@0: (elm)->field.le_next->field.le_prev = \ michael@0: (elm)->field.le_prev; \ michael@0: *(elm)->field.le_prev = (elm)->field.le_next; \ michael@0: } michael@0: michael@0: /* michael@0: * Tail queue definitions. 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: } 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: } michael@0: michael@0: /* michael@0: * Tail queue functions. michael@0: */ michael@0: #define TAILQ_INIT(head) { \ michael@0: (head)->tqh_first = NULL; \ michael@0: (head)->tqh_last = &(head)->tqh_first; \ michael@0: } michael@0: michael@0: #define TAILQ_INSERT_HEAD(head, elm, field) { \ michael@0: if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ michael@0: (elm)->field.tqe_next->field.tqe_prev = \ michael@0: &(elm)->field.tqe_next; \ michael@0: else \ michael@0: (head)->tqh_last = &(elm)->field.tqe_next; \ michael@0: (head)->tqh_first = (elm); \ michael@0: (elm)->field.tqe_prev = &(head)->tqh_first; \ michael@0: } michael@0: michael@0: #define TAILQ_INSERT_TAIL(head, elm, field) { \ michael@0: (elm)->field.tqe_next = NULL; \ michael@0: (elm)->field.tqe_prev = (head)->tqh_last; \ michael@0: *(head)->tqh_last = (elm); \ michael@0: (head)->tqh_last = &(elm)->field.tqe_next; \ michael@0: } michael@0: michael@0: #define TAILQ_INSERT_AFTER(head, listelm, elm, field) { \ michael@0: if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ michael@0: (elm)->field.tqe_next->field.tqe_prev = \ michael@0: &(elm)->field.tqe_next; \ michael@0: else \ michael@0: (head)->tqh_last = &(elm)->field.tqe_next; \ michael@0: (listelm)->field.tqe_next = (elm); \ michael@0: (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ michael@0: } michael@0: michael@0: #define TAILQ_REMOVE(head, elm, field) { \ michael@0: if (((elm)->field.tqe_next) != NULL) \ michael@0: (elm)->field.tqe_next->field.tqe_prev = \ michael@0: (elm)->field.tqe_prev; \ michael@0: else \ michael@0: (head)->tqh_last = (elm)->field.tqe_prev; \ michael@0: *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ michael@0: } michael@0: michael@0: /* michael@0: * Circular queue definitions. michael@0: */ michael@0: #define CIRCLEQ_HEAD(name, type) \ michael@0: struct name { \ michael@0: struct type *cqh_first; /* first element */ \ michael@0: struct type *cqh_last; /* last element */ \ michael@0: } michael@0: michael@0: #define CIRCLEQ_ENTRY(type) \ michael@0: struct { \ michael@0: struct type *cqe_next; /* next element */ \ michael@0: struct type *cqe_prev; /* previous element */ \ michael@0: } michael@0: michael@0: /* michael@0: * Circular queue functions. michael@0: */ michael@0: #define CIRCLEQ_INIT(head) { \ michael@0: (head)->cqh_first = (void *)(head); \ michael@0: (head)->cqh_last = (void *)(head); \ michael@0: } michael@0: michael@0: #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) { \ michael@0: (elm)->field.cqe_next = (listelm)->field.cqe_next; \ michael@0: (elm)->field.cqe_prev = (listelm); \ michael@0: if ((listelm)->field.cqe_next == (void *)(head)) \ michael@0: (head)->cqh_last = (elm); \ michael@0: else \ michael@0: (listelm)->field.cqe_next->field.cqe_prev = (elm); \ michael@0: (listelm)->field.cqe_next = (elm); \ michael@0: } michael@0: michael@0: #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) { \ michael@0: (elm)->field.cqe_next = (listelm); \ michael@0: (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ michael@0: if ((listelm)->field.cqe_prev == (void *)(head)) \ michael@0: (head)->cqh_first = (elm); \ michael@0: else \ michael@0: (listelm)->field.cqe_prev->field.cqe_next = (elm); \ michael@0: (listelm)->field.cqe_prev = (elm); \ michael@0: } michael@0: michael@0: #define CIRCLEQ_INSERT_HEAD(head, elm, field) { \ michael@0: (elm)->field.cqe_next = (head)->cqh_first; \ michael@0: (elm)->field.cqe_prev = (void *)(head); \ michael@0: if ((head)->cqh_last == (void *)(head)) \ michael@0: (head)->cqh_last = (elm); \ michael@0: else \ michael@0: (head)->cqh_first->field.cqe_prev = (elm); \ michael@0: (head)->cqh_first = (elm); \ michael@0: } michael@0: michael@0: #define CIRCLEQ_INSERT_TAIL(head, elm, field) { \ michael@0: (elm)->field.cqe_next = (void *)(head); \ michael@0: (elm)->field.cqe_prev = (head)->cqh_last; \ michael@0: if ((head)->cqh_first == (void *)(head)) \ michael@0: (head)->cqh_first = (elm); \ michael@0: else \ michael@0: (head)->cqh_last->field.cqe_next = (elm); \ michael@0: (head)->cqh_last = (elm); \ michael@0: } michael@0: michael@0: #define CIRCLEQ_REMOVE(head, elm, field) { \ michael@0: if ((elm)->field.cqe_next == (void *)(head)) \ michael@0: (head)->cqh_last = (elm)->field.cqe_prev; \ michael@0: else \ michael@0: (elm)->field.cqe_next->field.cqe_prev = \ michael@0: (elm)->field.cqe_prev; \ michael@0: if ((elm)->field.cqe_prev == (void *)(head)) \ michael@0: (head)->cqh_first = (elm)->field.cqe_next; \ michael@0: else \ michael@0: (elm)->field.cqe_prev->field.cqe_next = \ michael@0: (elm)->field.cqe_next; \ michael@0: } michael@0: #endif /* !_QUEUE_H_ */