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