Imported Upstream version 2.5.0
[debian/amanda] / common-src / queue.h
1 /* Amanda $Id: queue.h,v 1.2 1999/05/24 16:10:35 kashmir Exp $ */
2 /* from: $NetBSD: queue.h,v 1.16 1998/07/10 23:28:31 nathanw Exp $ */
3
4 /*
5  * Copyright (c) 1991, 1993
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)queue.h     8.5 (Berkeley) 8/20/94
37  */
38
39 #ifndef QUEUE_H
40 #define QUEUE_H
41
42 /*
43  * This file defines four types of data structures: lists, simple queues,
44  * tail queues, and circular queues.
45  *
46  * A list is headed by a single forward pointer (or an array of forward
47  * pointers for a hash table header). The elements are doubly linked
48  * so that an arbitrary element can be removed without a need to
49  * traverse the list. New elements can be added to the list before
50  * or after an existing element or at the head of the list. A list
51  * may only be traversed in the forward direction.
52  *
53  * A simple queue is headed by a pair of pointers, one the head of the
54  * list and the other to the tail of the list. The elements are singly
55  * linked to save space, so only elements can only be removed from the
56  * head of the list. New elements can be added to the list before or after
57  * an existing element, at the head of the list, or at the end of the
58  * list. A simple queue may only be traversed in the forward direction.
59  *
60  * A tail queue is headed by a pair of pointers, one to the head of the
61  * list and the other to the tail of the list. The elements are doubly
62  * linked so that an arbitrary element can be removed without a need to
63  * traverse the list. New elements can be added to the list before or
64  * after an existing element, at the head of the list, or at the end of
65  * the list. A tail queue may only be traversed in the forward direction.
66  *
67  * A circle queue is headed by a pair of pointers, one to the head of the
68  * list and the other to the tail of the list. The elements are doubly
69  * linked so that an arbitrary element can be removed without a need to
70  * traverse the list. New elements can be added to the list before or after
71  * an existing element, at the head of the list, or at the end of the list.
72  * A circle queue may be traversed in either direction, but has a more
73  * complex end of list detection.
74  *
75  * For details on the use of these macros, see the QUEUE-API document
76  * in the docs directory.
77  *
78  * Note that the #undef's in this file exist so we override any
79  * macros declared in <sys/queue.h>.
80  */
81
82 /*
83  * List definitions.
84  */
85 #undef LIST_HEAD
86 #define LIST_HEAD(name, type)                                           \
87 struct name {                                                           \
88         struct type *lh_first;  /* first element */                     \
89 }
90
91 #undef LIST_HEAD_INITIALIZER
92 #define LIST_HEAD_INITIALIZER(head)                                     \
93         { NULL }
94
95 #undef LIST_ENTRY
96 #define LIST_ENTRY(type)                                                \
97 struct {                                                                \
98         struct type *le_next;   /* next element */                      \
99         struct type **le_prev;  /* address of previous next element */  \
100 }
101
102 /*
103  * List functions.
104  */
105 #undef LIST_INIT
106 #define LIST_INIT(head) do {                                            \
107         (head)->lh_first = NULL;                                        \
108 } while (0)
109
110 #undef LIST_INSERT_AFTER
111 #define LIST_INSERT_AFTER(listelm, elm, field) do {                     \
112         if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
113                 (listelm)->field.le_next->field.le_prev =               \
114                     &(elm)->field.le_next;                              \
115         (listelm)->field.le_next = (elm);                               \
116         (elm)->field.le_prev = &(listelm)->field.le_next;               \
117 } while (0)
118
119 #undef LIST_INSERT_BEFORE
120 #define LIST_INSERT_BEFORE(listelm, elm, field) do {                    \
121         (elm)->field.le_prev = (listelm)->field.le_prev;                \
122         (elm)->field.le_next = (listelm);                               \
123         *(listelm)->field.le_prev = (elm);                              \
124         (listelm)->field.le_prev = &(elm)->field.le_next;               \
125 } while (0)
126
127 #undef LIST_INSERT_HEAD
128 #define LIST_INSERT_HEAD(head, elm, field) do {                         \
129         if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
130                 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
131         (head)->lh_first = (elm);                                       \
132         (elm)->field.le_prev = &(head)->lh_first;                       \
133 } while (0)
134
135 #undef LIST_REMOVE
136 #define LIST_REMOVE(elm, field) do {                                    \
137         if ((elm)->field.le_next != NULL)                               \
138                 (elm)->field.le_next->field.le_prev =                   \
139                     (elm)->field.le_prev;                               \
140         *(elm)->field.le_prev = (elm)->field.le_next;                   \
141 } while (0)
142
143 /*
144  * List access methods.
145  */
146 #undef LIST_FIRST
147 #define LIST_FIRST(head)                ((head)->lh_first)
148
149 #undef LIST_NEXT
150 #define LIST_NEXT(elm, field)           ((elm)->field.le_next)
151
152 /*
153  * Simple queue definitions.
154  */
155 #undef SIMPLEQ_HEAD
156 #define SIMPLEQ_HEAD(name, type)                                        \
157 struct name {                                                           \
158         struct type *sqh_first; /* first element */                     \
159         struct type **sqh_last; /* addr of last next element */         \
160 }
161
162 #undef SIMPLEQ_HEAD_INITIALIZER
163 #define SIMPLEQ_HEAD_INITIALIZER(head)                                  \
164         { NULL, &(head).sqh_first }
165
166 #undef SIMPLEQ_ENTRY
167 #define SIMPLEQ_ENTRY(type)                                             \
168 struct {                                                                \
169         struct type *sqe_next;  /* next element */                      \
170 }
171
172 /*
173  * Simple queue functions.
174  */
175 #undef SIMPLEQ_INIT
176 #define SIMPLEQ_INIT(head) do {                                         \
177         (head)->sqh_first = NULL;                                       \
178         (head)->sqh_last = &(head)->sqh_first;                          \
179 } while (0)
180
181 #undef SIMPLEQ_INSERT_HEAD
182 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do {                      \
183         if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)        \
184                 (head)->sqh_last = &(elm)->field.sqe_next;              \
185         (head)->sqh_first = (elm);                                      \
186 } while (0)
187
188 #undef SIMPLEQ_INSERT_TAIL
189 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do {                      \
190         (elm)->field.sqe_next = NULL;                                   \
191         *(head)->sqh_last = (elm);                                      \
192         (head)->sqh_last = &(elm)->field.sqe_next;                      \
193 } while (0)
194
195 #undef SIMPLEQ_INSERT_AFTER
196 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {            \
197         if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
198                 (head)->sqh_last = &(elm)->field.sqe_next;              \
199         (listelm)->field.sqe_next = (elm);                              \
200 } while (0)
201
202 #undef SIMPLEQ_REMOVE_HEAD
203 #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do {                      \
204         if (((head)->sqh_first = (elm)->field.sqe_next) == NULL)        \
205                 (head)->sqh_last = &(head)->sqh_first;                  \
206 } while (0)
207
208 /*
209  * Simple queue access methods.
210  */
211 #undef SIMPLEQ_FIRST
212 #define SIMPLEQ_FIRST(head)             ((head)->sqh_first)
213
214 #undef SIMPLEQ_NEXT
215 #define SIMPLEQ_NEXT(elm, field)        ((elm)->field.sqe_next)
216
217 /*
218  * Tail queue definitions.
219  */
220 #undef TAILQ_HEAD
221 #define TAILQ_HEAD(name, type)                                          \
222 struct name {                                                           \
223         struct type *tqh_first; /* first element */                     \
224         struct type **tqh_last; /* addr of last next element */         \
225 }
226
227 #undef TAILQ_HEAD_INITIALIZER
228 #define TAILQ_HEAD_INITIALIZER(head)                                    \
229         { NULL, &(head).tqh_first }
230
231 #undef TAILQ_ENTRY
232 #define TAILQ_ENTRY(type)                                               \
233 struct {                                                                \
234         struct type *tqe_next;  /* next element */                      \
235         struct type **tqe_prev; /* address of previous next element */  \
236 }
237
238 /*
239  * Tail queue functions.
240  */
241 #undef TAILQ_INIT
242 #define TAILQ_INIT(head) do {                                           \
243         (head)->tqh_first = NULL;                                       \
244         (head)->tqh_last = &(head)->tqh_first;                          \
245 } while (0)
246
247 #undef TAILQ_INSERT_HEAD
248 #define TAILQ_INSERT_HEAD(head, elm, field) do {                        \
249         if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
250                 (head)->tqh_first->field.tqe_prev =                     \
251                     &(elm)->field.tqe_next;                             \
252         else                                                            \
253                 (head)->tqh_last = &(elm)->field.tqe_next;              \
254         (head)->tqh_first = (elm);                                      \
255         (elm)->field.tqe_prev = &(head)->tqh_first;                     \
256 } while (0)
257
258 #undef TAILQ_INSERT_TAIL
259 #define TAILQ_INSERT_TAIL(head, elm, field) do {                        \
260         (elm)->field.tqe_next = NULL;                                   \
261         (elm)->field.tqe_prev = (head)->tqh_last;                       \
262         *(head)->tqh_last = (elm);                                      \
263         (head)->tqh_last = &(elm)->field.tqe_next;                      \
264 } while (0)
265
266 #undef TAILQ_INSERT_AFTER
267 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {              \
268         if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
269                 (elm)->field.tqe_next->field.tqe_prev =                 \
270                     &(elm)->field.tqe_next;                             \
271         else                                                            \
272                 (head)->tqh_last = &(elm)->field.tqe_next;              \
273         (listelm)->field.tqe_next = (elm);                              \
274         (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
275 } while (0)
276
277 #undef TAILQ_INSERT_BEFORE
278 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do {                   \
279         (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
280         (elm)->field.tqe_next = (listelm);                              \
281         *(listelm)->field.tqe_prev = (elm);                             \
282         (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
283 } while (0)
284
285 #undef TAILQ_REMOVE
286 #define TAILQ_REMOVE(head, elm, field) do {                             \
287         if (((elm)->field.tqe_next) != NULL)                            \
288                 (elm)->field.tqe_next->field.tqe_prev =                 \
289                     (elm)->field.tqe_prev;                              \
290         else                                                            \
291                 (head)->tqh_last = (elm)->field.tqe_prev;               \
292         *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
293 } while (0)
294
295 /*
296  * Tail queue access methods.
297  */
298 #undef TAILQ_FIRST
299 #define TAILQ_FIRST(head)               ((head)->tqh_first)
300
301 #undef TAILQ_NEXT
302 #define TAILQ_NEXT(elm, field)          ((elm)->field.tqe_next)
303
304 /*
305  * Circular queue definitions.
306  */
307 #undef CIRCLEQ_HEAD
308 #define CIRCLEQ_HEAD(name, type)                                        \
309 struct name {                                                           \
310         struct type *cqh_first;         /* first element */             \
311         struct type *cqh_last;          /* last element */              \
312 }
313
314 #undef CIRCLEQ_HEAD_INITIALIZER
315 #define CIRCLEQ_HEAD_INITIALIZER(head)                                  \
316         { (void *)&head, (void *)&head }
317
318 #undef CIRCLEQ_ENTRY
319 #define CIRCLEQ_ENTRY(type)                                             \
320 struct {                                                                \
321         struct type *cqe_next;          /* next element */              \
322         struct type *cqe_prev;          /* previous element */          \
323 }
324
325 /*
326  * Circular queue functions.
327  */
328 #undef CIRCLEQ_INIT
329 #define CIRCLEQ_INIT(head) do {                                         \
330         (head)->cqh_first = (void *)(head);                             \
331         (head)->cqh_last = (void *)(head);                              \
332 } while (0)
333
334 #undef CIRCLEQ_INSERT_AFTER
335 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {            \
336         (elm)->field.cqe_next = (listelm)->field.cqe_next;              \
337         (elm)->field.cqe_prev = (listelm);                              \
338         if ((listelm)->field.cqe_next == (void *)(head))                \
339                 (head)->cqh_last = (elm);                               \
340         else                                                            \
341                 (listelm)->field.cqe_next->field.cqe_prev = (elm);      \
342         (listelm)->field.cqe_next = (elm);                              \
343 } while (0)
344
345 #undef CIRCLEQ_INSERT_BEFORE
346 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {           \
347         (elm)->field.cqe_next = (listelm);                              \
348         (elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
349         if ((listelm)->field.cqe_prev == (void *)(head))                \
350                 (head)->cqh_first = (elm);                              \
351         else                                                            \
352                 (listelm)->field.cqe_prev->field.cqe_next = (elm);      \
353         (listelm)->field.cqe_prev = (elm);                              \
354 } while (0)
355
356 #undef CIRCLEQ_INSERT_HEAD
357 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do {                      \
358         (elm)->field.cqe_next = (head)->cqh_first;                      \
359         (elm)->field.cqe_prev = (void *)(head);                         \
360         if ((head)->cqh_last == (void *)(head))                         \
361                 (head)->cqh_last = (elm);                               \
362         else                                                            \
363                 (head)->cqh_first->field.cqe_prev = (elm);              \
364         (head)->cqh_first = (elm);                                      \
365 } while (0)
366
367 #undef CIRCLEQ_INSERT_TAIL
368 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do {                      \
369         (elm)->field.cqe_next = (void *)(head);                         \
370         (elm)->field.cqe_prev = (head)->cqh_last;                       \
371         if ((head)->cqh_first == (void *)(head))                        \
372                 (head)->cqh_first = (elm);                              \
373         else                                                            \
374                 (head)->cqh_last->field.cqe_next = (elm);               \
375         (head)->cqh_last = (elm);                                       \
376 } while (0)
377
378 #undef CIRCLEQ_REMOVE
379 #define CIRCLEQ_REMOVE(head, elm, field) do {                           \
380         if ((elm)->field.cqe_next == (void *)(head))                    \
381                 (head)->cqh_last = (elm)->field.cqe_prev;               \
382         else                                                            \
383                 (elm)->field.cqe_next->field.cqe_prev =                 \
384                     (elm)->field.cqe_prev;                              \
385         if ((elm)->field.cqe_prev == (void *)(head))                    \
386                 (head)->cqh_first = (elm)->field.cqe_next;              \
387         else                                                            \
388                 (elm)->field.cqe_prev->field.cqe_next =                 \
389                     (elm)->field.cqe_next;                              \
390 } while (0)
391
392 /*
393  * Circular queue access methods.
394  */
395 #undef CIRCLEQ_FIRST
396 #define CIRCLEQ_FIRST(head)             ((head)->cqh_first)
397
398 #undef CIRCLEQ_LAST
399 #define CIRCLEQ_LAST(head)              ((head)->cqh_last)
400
401 #undef CIRCLEQ_NEXT
402 #define CIRCLEQ_NEXT(elm, field)        ((elm)->field.cqe_next)
403
404 #undef CIRCLEQ_PREV
405 #define CIRCLEQ_PREV(elm, field)        ((elm)->field.cqe_prev)
406 #endif  /* !QUEUE_H */