Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 1991, 1993 |
michael@0 | 3 | * The Regents of the University of California. All rights reserved. |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions |
michael@0 | 7 | * are met: |
michael@0 | 8 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 9 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 11 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 12 | * documentation and/or other materials provided with the distribution. |
michael@0 | 13 | * 3. ***REMOVED*** - see |
michael@0 | 14 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change |
michael@0 | 15 | * 4. Neither the name of the University nor the names of its contributors |
michael@0 | 16 | * may be used to endorse or promote products derived from this software |
michael@0 | 17 | * without specific prior written permission. |
michael@0 | 18 | * |
michael@0 | 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
michael@0 | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
michael@0 | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
michael@0 | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
michael@0 | 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
michael@0 | 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
michael@0 | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
michael@0 | 29 | * SUCH DAMAGE. |
michael@0 | 30 | * |
michael@0 | 31 | * @(#)queue.h 8.3 (Berkeley) 12/13/93 |
michael@0 | 32 | */ |
michael@0 | 33 | |
michael@0 | 34 | #ifndef _QUEUE_H_ |
michael@0 | 35 | #define _QUEUE_H_ |
michael@0 | 36 | |
michael@0 | 37 | /* |
michael@0 | 38 | * This file defines three types of data structures: lists, tail queues, |
michael@0 | 39 | * and circular queues. |
michael@0 | 40 | * |
michael@0 | 41 | * A list is headed by a single forward pointer (or an array of forward |
michael@0 | 42 | * pointers for a hash table header). The elements are doubly linked |
michael@0 | 43 | * so that an arbitrary element can be removed without a need to |
michael@0 | 44 | * traverse the list. New elements can be added to the list after |
michael@0 | 45 | * an existing element or at the head of the list. A list may only be |
michael@0 | 46 | * traversed in the forward direction. |
michael@0 | 47 | * |
michael@0 | 48 | * A tail queue is headed by a pair of pointers, one to the head of the |
michael@0 | 49 | * list and the other to the tail of the list. The elements are doubly |
michael@0 | 50 | * linked so that an arbitrary element can be removed without a need to |
michael@0 | 51 | * traverse the list. New elements can be added to the list after |
michael@0 | 52 | * an existing element, at the head of the list, or at the end of the |
michael@0 | 53 | * list. A tail queue may only be traversed in the forward direction. |
michael@0 | 54 | * |
michael@0 | 55 | * A circle queue is headed by a pair of pointers, one to the head of the |
michael@0 | 56 | * list and the other to the tail of the list. The elements are doubly |
michael@0 | 57 | * linked so that an arbitrary element can be removed without a need to |
michael@0 | 58 | * traverse the list. New elements can be added to the list before or after |
michael@0 | 59 | * an existing element, at the head of the list, or at the end of the list. |
michael@0 | 60 | * A circle queue may be traversed in either direction, but has a more |
michael@0 | 61 | * complex end of list detection. |
michael@0 | 62 | * |
michael@0 | 63 | * For details on the use of these macros, see the queue(3) manual page. |
michael@0 | 64 | */ |
michael@0 | 65 | |
michael@0 | 66 | /* |
michael@0 | 67 | * List definitions. |
michael@0 | 68 | */ |
michael@0 | 69 | #define LIST_HEAD(name, type) \ |
michael@0 | 70 | struct name { \ |
michael@0 | 71 | struct type *lh_first; /* first element */ \ |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | #define LIST_ENTRY(type) \ |
michael@0 | 75 | struct { \ |
michael@0 | 76 | struct type *le_next; /* next element */ \ |
michael@0 | 77 | struct type **le_prev; /* address of previous next element */ \ |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | /* |
michael@0 | 81 | * List functions. |
michael@0 | 82 | */ |
michael@0 | 83 | #define LIST_INIT(head) { \ |
michael@0 | 84 | (head)->lh_first = NULL; \ |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | #define LIST_INSERT_AFTER(listelm, elm, field) { \ |
michael@0 | 88 | if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ |
michael@0 | 89 | (listelm)->field.le_next->field.le_prev = \ |
michael@0 | 90 | &(elm)->field.le_next; \ |
michael@0 | 91 | (listelm)->field.le_next = (elm); \ |
michael@0 | 92 | (elm)->field.le_prev = &(listelm)->field.le_next; \ |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | #define LIST_INSERT_HEAD(head, elm, field) { \ |
michael@0 | 96 | if (((elm)->field.le_next = (head)->lh_first) != NULL) \ |
michael@0 | 97 | (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ |
michael@0 | 98 | (head)->lh_first = (elm); \ |
michael@0 | 99 | (elm)->field.le_prev = &(head)->lh_first; \ |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | #define LIST_REMOVE(elm, field) { \ |
michael@0 | 103 | if ((elm)->field.le_next != NULL) \ |
michael@0 | 104 | (elm)->field.le_next->field.le_prev = \ |
michael@0 | 105 | (elm)->field.le_prev; \ |
michael@0 | 106 | *(elm)->field.le_prev = (elm)->field.le_next; \ |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | /* |
michael@0 | 110 | * Tail queue definitions. |
michael@0 | 111 | */ |
michael@0 | 112 | #define TAILQ_HEAD(name, type) \ |
michael@0 | 113 | struct name { \ |
michael@0 | 114 | struct type *tqh_first; /* first element */ \ |
michael@0 | 115 | struct type **tqh_last; /* addr of last next element */ \ |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | #define TAILQ_ENTRY(type) \ |
michael@0 | 119 | struct { \ |
michael@0 | 120 | struct type *tqe_next; /* next element */ \ |
michael@0 | 121 | struct type **tqe_prev; /* address of previous next element */ \ |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | /* |
michael@0 | 125 | * Tail queue functions. |
michael@0 | 126 | */ |
michael@0 | 127 | #define TAILQ_INIT(head) { \ |
michael@0 | 128 | (head)->tqh_first = NULL; \ |
michael@0 | 129 | (head)->tqh_last = &(head)->tqh_first; \ |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | #define TAILQ_INSERT_HEAD(head, elm, field) { \ |
michael@0 | 133 | if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ |
michael@0 | 134 | (elm)->field.tqe_next->field.tqe_prev = \ |
michael@0 | 135 | &(elm)->field.tqe_next; \ |
michael@0 | 136 | else \ |
michael@0 | 137 | (head)->tqh_last = &(elm)->field.tqe_next; \ |
michael@0 | 138 | (head)->tqh_first = (elm); \ |
michael@0 | 139 | (elm)->field.tqe_prev = &(head)->tqh_first; \ |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | #define TAILQ_INSERT_TAIL(head, elm, field) { \ |
michael@0 | 143 | (elm)->field.tqe_next = NULL; \ |
michael@0 | 144 | (elm)->field.tqe_prev = (head)->tqh_last; \ |
michael@0 | 145 | *(head)->tqh_last = (elm); \ |
michael@0 | 146 | (head)->tqh_last = &(elm)->field.tqe_next; \ |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | #define TAILQ_INSERT_AFTER(head, listelm, elm, field) { \ |
michael@0 | 150 | if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ |
michael@0 | 151 | (elm)->field.tqe_next->field.tqe_prev = \ |
michael@0 | 152 | &(elm)->field.tqe_next; \ |
michael@0 | 153 | else \ |
michael@0 | 154 | (head)->tqh_last = &(elm)->field.tqe_next; \ |
michael@0 | 155 | (listelm)->field.tqe_next = (elm); \ |
michael@0 | 156 | (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | #define TAILQ_REMOVE(head, elm, field) { \ |
michael@0 | 160 | if (((elm)->field.tqe_next) != NULL) \ |
michael@0 | 161 | (elm)->field.tqe_next->field.tqe_prev = \ |
michael@0 | 162 | (elm)->field.tqe_prev; \ |
michael@0 | 163 | else \ |
michael@0 | 164 | (head)->tqh_last = (elm)->field.tqe_prev; \ |
michael@0 | 165 | *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | /* |
michael@0 | 169 | * Circular queue definitions. |
michael@0 | 170 | */ |
michael@0 | 171 | #define CIRCLEQ_HEAD(name, type) \ |
michael@0 | 172 | struct name { \ |
michael@0 | 173 | struct type *cqh_first; /* first element */ \ |
michael@0 | 174 | struct type *cqh_last; /* last element */ \ |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | #define CIRCLEQ_ENTRY(type) \ |
michael@0 | 178 | struct { \ |
michael@0 | 179 | struct type *cqe_next; /* next element */ \ |
michael@0 | 180 | struct type *cqe_prev; /* previous element */ \ |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | /* |
michael@0 | 184 | * Circular queue functions. |
michael@0 | 185 | */ |
michael@0 | 186 | #define CIRCLEQ_INIT(head) { \ |
michael@0 | 187 | (head)->cqh_first = (void *)(head); \ |
michael@0 | 188 | (head)->cqh_last = (void *)(head); \ |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) { \ |
michael@0 | 192 | (elm)->field.cqe_next = (listelm)->field.cqe_next; \ |
michael@0 | 193 | (elm)->field.cqe_prev = (listelm); \ |
michael@0 | 194 | if ((listelm)->field.cqe_next == (void *)(head)) \ |
michael@0 | 195 | (head)->cqh_last = (elm); \ |
michael@0 | 196 | else \ |
michael@0 | 197 | (listelm)->field.cqe_next->field.cqe_prev = (elm); \ |
michael@0 | 198 | (listelm)->field.cqe_next = (elm); \ |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) { \ |
michael@0 | 202 | (elm)->field.cqe_next = (listelm); \ |
michael@0 | 203 | (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ |
michael@0 | 204 | if ((listelm)->field.cqe_prev == (void *)(head)) \ |
michael@0 | 205 | (head)->cqh_first = (elm); \ |
michael@0 | 206 | else \ |
michael@0 | 207 | (listelm)->field.cqe_prev->field.cqe_next = (elm); \ |
michael@0 | 208 | (listelm)->field.cqe_prev = (elm); \ |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | #define CIRCLEQ_INSERT_HEAD(head, elm, field) { \ |
michael@0 | 212 | (elm)->field.cqe_next = (head)->cqh_first; \ |
michael@0 | 213 | (elm)->field.cqe_prev = (void *)(head); \ |
michael@0 | 214 | if ((head)->cqh_last == (void *)(head)) \ |
michael@0 | 215 | (head)->cqh_last = (elm); \ |
michael@0 | 216 | else \ |
michael@0 | 217 | (head)->cqh_first->field.cqe_prev = (elm); \ |
michael@0 | 218 | (head)->cqh_first = (elm); \ |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | #define CIRCLEQ_INSERT_TAIL(head, elm, field) { \ |
michael@0 | 222 | (elm)->field.cqe_next = (void *)(head); \ |
michael@0 | 223 | (elm)->field.cqe_prev = (head)->cqh_last; \ |
michael@0 | 224 | if ((head)->cqh_first == (void *)(head)) \ |
michael@0 | 225 | (head)->cqh_first = (elm); \ |
michael@0 | 226 | else \ |
michael@0 | 227 | (head)->cqh_last->field.cqe_next = (elm); \ |
michael@0 | 228 | (head)->cqh_last = (elm); \ |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | #define CIRCLEQ_REMOVE(head, elm, field) { \ |
michael@0 | 232 | if ((elm)->field.cqe_next == (void *)(head)) \ |
michael@0 | 233 | (head)->cqh_last = (elm)->field.cqe_prev; \ |
michael@0 | 234 | else \ |
michael@0 | 235 | (elm)->field.cqe_next->field.cqe_prev = \ |
michael@0 | 236 | (elm)->field.cqe_prev; \ |
michael@0 | 237 | if ((elm)->field.cqe_prev == (void *)(head)) \ |
michael@0 | 238 | (head)->cqh_first = (elm)->field.cqe_next; \ |
michael@0 | 239 | else \ |
michael@0 | 240 | (elm)->field.cqe_prev->field.cqe_next = \ |
michael@0 | 241 | (elm)->field.cqe_next; \ |
michael@0 | 242 | } |
michael@0 | 243 | #endif /* !_QUEUE_H_ */ |