1c19800e8SDoug Rabson /* $NetBSD: queue.h,v 1.38 2004/04/18 14:12:05 lukem Exp $ */ 2*ae771770SStanislav Sedov /* $Id$ */ 3c19800e8SDoug Rabson 4c19800e8SDoug Rabson /* 5c19800e8SDoug Rabson * Copyright (c) 1991, 1993 6c19800e8SDoug Rabson * The Regents of the University of California. All rights reserved. 7c19800e8SDoug Rabson * 8c19800e8SDoug Rabson * Redistribution and use in source and binary forms, with or without 9c19800e8SDoug Rabson * modification, are permitted provided that the following conditions 10c19800e8SDoug Rabson * are met: 11c19800e8SDoug Rabson * 1. Redistributions of source code must retain the above copyright 12c19800e8SDoug Rabson * notice, this list of conditions and the following disclaimer. 13c19800e8SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 14c19800e8SDoug Rabson * notice, this list of conditions and the following disclaimer in the 15c19800e8SDoug Rabson * documentation and/or other materials provided with the distribution. 16c19800e8SDoug Rabson * 3. Neither the name of the University nor the names of its contributors 17c19800e8SDoug Rabson * may be used to endorse or promote products derived from this software 18c19800e8SDoug Rabson * without specific prior written permission. 19c19800e8SDoug Rabson * 20c19800e8SDoug Rabson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21c19800e8SDoug Rabson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22c19800e8SDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23c19800e8SDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24c19800e8SDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25c19800e8SDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26c19800e8SDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27c19800e8SDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28c19800e8SDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29c19800e8SDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30c19800e8SDoug Rabson * SUCH DAMAGE. 31c19800e8SDoug Rabson * 32c19800e8SDoug Rabson * @(#)queue.h 8.5 (Berkeley) 8/20/94 33c19800e8SDoug Rabson */ 34c19800e8SDoug Rabson 35c19800e8SDoug Rabson #ifndef _ASN1_QUEUE_H_ 36c19800e8SDoug Rabson #define _ASN1_QUEUE_H_ 37c19800e8SDoug Rabson 38c19800e8SDoug Rabson /* 39c19800e8SDoug Rabson * Tail queue definitions. 40c19800e8SDoug Rabson */ 41c19800e8SDoug Rabson #define ASN1_TAILQ_HEAD(name, type) \ 42c19800e8SDoug Rabson struct name { \ 43c19800e8SDoug Rabson struct type *tqh_first; /* first element */ \ 44c19800e8SDoug Rabson struct type **tqh_last; /* addr of last next element */ \ 45c19800e8SDoug Rabson } 46c19800e8SDoug Rabson 47c19800e8SDoug Rabson #define ASN1_TAILQ_HEAD_INITIALIZER(head) \ 48c19800e8SDoug Rabson { NULL, &(head).tqh_first } 49c19800e8SDoug Rabson #define ASN1_TAILQ_ENTRY(type) \ 50c19800e8SDoug Rabson struct { \ 51c19800e8SDoug Rabson struct type *tqe_next; /* next element */ \ 52c19800e8SDoug Rabson struct type **tqe_prev; /* address of previous next element */ \ 53c19800e8SDoug Rabson } 54c19800e8SDoug Rabson 55c19800e8SDoug Rabson /* 56c19800e8SDoug Rabson * Tail queue functions. 57c19800e8SDoug Rabson */ 58c19800e8SDoug Rabson #if defined(_KERNEL) && defined(QUEUEDEBUG) 59c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_INSERT_HEAD(head, elm, field) \ 60c19800e8SDoug Rabson if ((head)->tqh_first && \ 61c19800e8SDoug Rabson (head)->tqh_first->field.tqe_prev != &(head)->tqh_first) \ 62c19800e8SDoug Rabson panic("ASN1_TAILQ_INSERT_HEAD %p %s:%d", (head), __FILE__, __LINE__); 63c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_INSERT_TAIL(head, elm, field) \ 64c19800e8SDoug Rabson if (*(head)->tqh_last != NULL) \ 65c19800e8SDoug Rabson panic("ASN1_TAILQ_INSERT_TAIL %p %s:%d", (head), __FILE__, __LINE__); 66c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_OP(elm, field) \ 67c19800e8SDoug Rabson if ((elm)->field.tqe_next && \ 68c19800e8SDoug Rabson (elm)->field.tqe_next->field.tqe_prev != \ 69c19800e8SDoug Rabson &(elm)->field.tqe_next) \ 70c19800e8SDoug Rabson panic("ASN1_TAILQ_* forw %p %s:%d", (elm), __FILE__, __LINE__);\ 71c19800e8SDoug Rabson if (*(elm)->field.tqe_prev != (elm)) \ 72c19800e8SDoug Rabson panic("ASN1_TAILQ_* back %p %s:%d", (elm), __FILE__, __LINE__); 73c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_PREREMOVE(head, elm, field) \ 74c19800e8SDoug Rabson if ((elm)->field.tqe_next == NULL && \ 75c19800e8SDoug Rabson (head)->tqh_last != &(elm)->field.tqe_next) \ 76c19800e8SDoug Rabson panic("ASN1_TAILQ_PREREMOVE head %p elm %p %s:%d", \ 77c19800e8SDoug Rabson (head), (elm), __FILE__, __LINE__); 78c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_POSTREMOVE(elm, field) \ 79c19800e8SDoug Rabson (elm)->field.tqe_next = (void *)1L; \ 80c19800e8SDoug Rabson (elm)->field.tqe_prev = (void *)1L; 81c19800e8SDoug Rabson #else 82c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_INSERT_HEAD(head, elm, field) 83c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_INSERT_TAIL(head, elm, field) 84c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_OP(elm, field) 85c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_PREREMOVE(head, elm, field) 86c19800e8SDoug Rabson #define QUEUEDEBUG_ASN1_TAILQ_POSTREMOVE(elm, field) 87c19800e8SDoug Rabson #endif 88c19800e8SDoug Rabson 89c19800e8SDoug Rabson #define ASN1_TAILQ_INIT(head) do { \ 90c19800e8SDoug Rabson (head)->tqh_first = NULL; \ 91c19800e8SDoug Rabson (head)->tqh_last = &(head)->tqh_first; \ 92c19800e8SDoug Rabson } while (/*CONSTCOND*/0) 93c19800e8SDoug Rabson 94c19800e8SDoug Rabson #define ASN1_TAILQ_INSERT_HEAD(head, elm, field) do { \ 95c19800e8SDoug Rabson QUEUEDEBUG_ASN1_TAILQ_INSERT_HEAD((head), (elm), field) \ 96c19800e8SDoug Rabson if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ 97c19800e8SDoug Rabson (head)->tqh_first->field.tqe_prev = \ 98c19800e8SDoug Rabson &(elm)->field.tqe_next; \ 99c19800e8SDoug Rabson else \ 100c19800e8SDoug Rabson (head)->tqh_last = &(elm)->field.tqe_next; \ 101c19800e8SDoug Rabson (head)->tqh_first = (elm); \ 102c19800e8SDoug Rabson (elm)->field.tqe_prev = &(head)->tqh_first; \ 103c19800e8SDoug Rabson } while (/*CONSTCOND*/0) 104c19800e8SDoug Rabson 105c19800e8SDoug Rabson #define ASN1_TAILQ_INSERT_TAIL(head, elm, field) do { \ 106c19800e8SDoug Rabson QUEUEDEBUG_ASN1_TAILQ_INSERT_TAIL((head), (elm), field) \ 107c19800e8SDoug Rabson (elm)->field.tqe_next = NULL; \ 108c19800e8SDoug Rabson (elm)->field.tqe_prev = (head)->tqh_last; \ 109c19800e8SDoug Rabson *(head)->tqh_last = (elm); \ 110c19800e8SDoug Rabson (head)->tqh_last = &(elm)->field.tqe_next; \ 111c19800e8SDoug Rabson } while (/*CONSTCOND*/0) 112c19800e8SDoug Rabson 113c19800e8SDoug Rabson #define ASN1_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ 114c19800e8SDoug Rabson QUEUEDEBUG_ASN1_TAILQ_OP((listelm), field) \ 115c19800e8SDoug Rabson if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ 116c19800e8SDoug Rabson (elm)->field.tqe_next->field.tqe_prev = \ 117c19800e8SDoug Rabson &(elm)->field.tqe_next; \ 118c19800e8SDoug Rabson else \ 119c19800e8SDoug Rabson (head)->tqh_last = &(elm)->field.tqe_next; \ 120c19800e8SDoug Rabson (listelm)->field.tqe_next = (elm); \ 121c19800e8SDoug Rabson (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ 122c19800e8SDoug Rabson } while (/*CONSTCOND*/0) 123c19800e8SDoug Rabson 124c19800e8SDoug Rabson #define ASN1_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ 125c19800e8SDoug Rabson QUEUEDEBUG_ASN1_TAILQ_OP((listelm), field) \ 126c19800e8SDoug Rabson (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ 127c19800e8SDoug Rabson (elm)->field.tqe_next = (listelm); \ 128c19800e8SDoug Rabson *(listelm)->field.tqe_prev = (elm); \ 129c19800e8SDoug Rabson (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ 130c19800e8SDoug Rabson } while (/*CONSTCOND*/0) 131c19800e8SDoug Rabson 132c19800e8SDoug Rabson #define ASN1_TAILQ_REMOVE(head, elm, field) do { \ 133c19800e8SDoug Rabson QUEUEDEBUG_ASN1_TAILQ_PREREMOVE((head), (elm), field) \ 134c19800e8SDoug Rabson QUEUEDEBUG_ASN1_TAILQ_OP((elm), field) \ 135c19800e8SDoug Rabson if (((elm)->field.tqe_next) != NULL) \ 136c19800e8SDoug Rabson (elm)->field.tqe_next->field.tqe_prev = \ 137c19800e8SDoug Rabson (elm)->field.tqe_prev; \ 138c19800e8SDoug Rabson else \ 139c19800e8SDoug Rabson (head)->tqh_last = (elm)->field.tqe_prev; \ 140c19800e8SDoug Rabson *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ 141c19800e8SDoug Rabson QUEUEDEBUG_ASN1_TAILQ_POSTREMOVE((elm), field); \ 142c19800e8SDoug Rabson } while (/*CONSTCOND*/0) 143c19800e8SDoug Rabson 144c19800e8SDoug Rabson #define ASN1_TAILQ_FOREACH(var, head, field) \ 145c19800e8SDoug Rabson for ((var) = ((head)->tqh_first); \ 146c19800e8SDoug Rabson (var); \ 147c19800e8SDoug Rabson (var) = ((var)->field.tqe_next)) 148c19800e8SDoug Rabson 149c19800e8SDoug Rabson #define ASN1_TAILQ_FOREACH_REVERSE(var, head, headname, field) \ 150c19800e8SDoug Rabson for ((var) = (*(((struct headname *)((head)->tqh_last))->tqh_last)); \ 151c19800e8SDoug Rabson (var); \ 152c19800e8SDoug Rabson (var) = (*(((struct headname *)((var)->field.tqe_prev))->tqh_last))) 153c19800e8SDoug Rabson 154c19800e8SDoug Rabson /* 155c19800e8SDoug Rabson * Tail queue access methods. 156c19800e8SDoug Rabson */ 157c19800e8SDoug Rabson #define ASN1_TAILQ_EMPTY(head) ((head)->tqh_first == NULL) 158c19800e8SDoug Rabson #define ASN1_TAILQ_FIRST(head) ((head)->tqh_first) 159c19800e8SDoug Rabson #define ASN1_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) 160c19800e8SDoug Rabson 161c19800e8SDoug Rabson #define ASN1_TAILQ_LAST(head, headname) \ 162c19800e8SDoug Rabson (*(((struct headname *)((head)->tqh_last))->tqh_last)) 163c19800e8SDoug Rabson #define ASN1_TAILQ_PREV(elm, headname, field) \ 164c19800e8SDoug Rabson (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) 165c19800e8SDoug Rabson 166c19800e8SDoug Rabson 167c19800e8SDoug Rabson #endif /* !_ASN1_QUEUE_H_ */ 168