Update.
[glibc.git] / db2 / include / queue.h
blobf606eb04975dd73a07c2beece90ca60d8aabbfa7
1 /* BSDI $Id$ */
3 /*
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 * @(#)queue.h 8.5 (Berkeley) 8/20/94
38 #ifndef _SYS_QUEUE_H_
39 #define _SYS_QUEUE_H_
42 * This file defines three types of data structures: lists, tail queues,
43 * and circular queues.
45 * A list is headed by a single forward pointer (or an array of forward
46 * pointers for a hash table header). The elements are doubly linked
47 * so that an arbitrary element can be removed without a need to
48 * traverse the list. New elements can be added to the list before
49 * or after an existing element or at the head of the list. A list
50 * may only be traversed in the forward direction.
52 * A tail queue is headed by a pair of pointers, one to the head of the
53 * list and the other to the tail of the list. The elements are doubly
54 * linked so that an arbitrary element can be removed without a need to
55 * traverse the list. New elements can be added to the list before or
56 * after an existing element, at the head of the list, or at the end of
57 * the list. A tail queue may only be traversed in the forward direction.
59 * A circle queue is headed by a pair of pointers, one to the head of the
60 * list and the other to the tail of the list. The elements are doubly
61 * linked so that an arbitrary element can be removed without a need to
62 * traverse the list. New elements can be added to the list before or after
63 * an existing element, at the head of the list, or at the end of the list.
64 * A circle queue may be traversed in either direction, but has a more
65 * complex end of list detection.
67 * For details on the use of these macros, see the queue(3) manual page.
71 * List definitions.
73 #define LIST_HEAD(name, type) \
74 struct name { \
75 struct type *lh_first; /* first element */ \
78 #define LIST_ENTRY(type) \
79 struct { \
80 struct type *le_next; /* next element */ \
81 struct type **le_prev; /* address of previous next element */ \
84 #define LIST_FIRST(head) ((head)->lh_first)
85 #define LIST_NEXT(elm, field) ((elm)->field.le_next)
86 #define LIST_END(head) NULL
89 * List functions.
91 #define LIST_INIT(head) { \
92 (head)->lh_first = NULL; \
95 #define LIST_INSERT_AFTER(listelm, elm, field) do { \
96 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
97 (listelm)->field.le_next->field.le_prev = \
98 &(elm)->field.le_next; \
99 (listelm)->field.le_next = (elm); \
100 (elm)->field.le_prev = &(listelm)->field.le_next; \
101 } while (0)
103 #define LIST_INSERT_BEFORE(listelm, elm, field) do { \
104 (elm)->field.le_prev = (listelm)->field.le_prev; \
105 (elm)->field.le_next = (listelm); \
106 *(listelm)->field.le_prev = (elm); \
107 (listelm)->field.le_prev = &(elm)->field.le_next; \
108 } while (0)
110 #define LIST_INSERT_HEAD(head, elm, field) do { \
111 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
112 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
113 (head)->lh_first = (elm); \
114 (elm)->field.le_prev = &(head)->lh_first; \
115 } while (0)
117 #define LIST_REMOVE(elm, field) do { \
118 if ((elm)->field.le_next != NULL) \
119 (elm)->field.le_next->field.le_prev = \
120 (elm)->field.le_prev; \
121 *(elm)->field.le_prev = (elm)->field.le_next; \
122 } while (0)
125 * Tail queue definitions.
127 #define TAILQ_HEAD(name, type) \
128 struct name { \
129 struct type *tqh_first; /* first element */ \
130 struct type **tqh_last; /* addr of last next element */ \
133 #define TAILQ_ENTRY(type) \
134 struct { \
135 struct type *tqe_next; /* next element */ \
136 struct type **tqe_prev; /* address of previous next element */ \
139 #define TAILQ_FIRST(head) ((head)->tqh_first)
140 #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
141 #define TAILQ_END(head) NULL
144 * Tail queue functions.
146 #define TAILQ_INIT(head) do { \
147 (head)->tqh_first = NULL; \
148 (head)->tqh_last = &(head)->tqh_first; \
149 } while (0)
151 #define TAILQ_INSERT_HEAD(head, elm, field) do { \
152 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
153 (head)->tqh_first->field.tqe_prev = \
154 &(elm)->field.tqe_next; \
155 else \
156 (head)->tqh_last = &(elm)->field.tqe_next; \
157 (head)->tqh_first = (elm); \
158 (elm)->field.tqe_prev = &(head)->tqh_first; \
159 } while (0)
161 #define TAILQ_INSERT_TAIL(head, elm, field) do { \
162 (elm)->field.tqe_next = NULL; \
163 (elm)->field.tqe_prev = (head)->tqh_last; \
164 *(head)->tqh_last = (elm); \
165 (head)->tqh_last = &(elm)->field.tqe_next; \
166 } while (0)
168 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
169 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
170 (elm)->field.tqe_next->field.tqe_prev = \
171 &(elm)->field.tqe_next; \
172 else \
173 (head)->tqh_last = &(elm)->field.tqe_next; \
174 (listelm)->field.tqe_next = (elm); \
175 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
176 } while (0)
178 #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
179 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
180 (elm)->field.tqe_next = (listelm); \
181 *(listelm)->field.tqe_prev = (elm); \
182 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
183 } while (0)
185 #define TAILQ_REMOVE(head, elm, field) do { \
186 if (((elm)->field.tqe_next) != NULL) \
187 (elm)->field.tqe_next->field.tqe_prev = \
188 (elm)->field.tqe_prev; \
189 else \
190 (head)->tqh_last = (elm)->field.tqe_prev; \
191 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
192 } while (0)
195 * Circular queue definitions.
197 #define CIRCLEQ_HEAD(name, type) \
198 struct name { \
199 struct type *cqh_first; /* first element */ \
200 struct type *cqh_last; /* last element */ \
203 #define CIRCLEQ_ENTRY(type) \
204 struct { \
205 struct type *cqe_next; /* next element */ \
206 struct type *cqe_prev; /* previous element */ \
209 #define CIRCLEQ_FIRST(head) ((head)->cqh_first)
210 #define CIRCLEQ_LAST(head) ((head)->cqh_last)
211 #define CIRCLEQ_END(head) ((void *)(head))
212 #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
213 #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
216 * Circular queue functions.
218 #define CIRCLEQ_INIT(head) do { \
219 (head)->cqh_first = (void *)(head); \
220 (head)->cqh_last = (void *)(head); \
221 } while (0)
223 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
224 (elm)->field.cqe_next = (listelm)->field.cqe_next; \
225 (elm)->field.cqe_prev = (listelm); \
226 if ((listelm)->field.cqe_next == (void *)(head)) \
227 (head)->cqh_last = (elm); \
228 else \
229 (listelm)->field.cqe_next->field.cqe_prev = (elm); \
230 (listelm)->field.cqe_next = (elm); \
231 } while (0)
233 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
234 (elm)->field.cqe_next = (listelm); \
235 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
236 if ((listelm)->field.cqe_prev == (void *)(head)) \
237 (head)->cqh_first = (elm); \
238 else \
239 (listelm)->field.cqe_prev->field.cqe_next = (elm); \
240 (listelm)->field.cqe_prev = (elm); \
241 } while (0)
243 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
244 (elm)->field.cqe_next = (head)->cqh_first; \
245 (elm)->field.cqe_prev = (void *)(head); \
246 if ((head)->cqh_last == (void *)(head)) \
247 (head)->cqh_last = (elm); \
248 else \
249 (head)->cqh_first->field.cqe_prev = (elm); \
250 (head)->cqh_first = (elm); \
251 } while (0)
253 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
254 (elm)->field.cqe_next = (void *)(head); \
255 (elm)->field.cqe_prev = (head)->cqh_last; \
256 if ((head)->cqh_first == (void *)(head)) \
257 (head)->cqh_first = (elm); \
258 else \
259 (head)->cqh_last->field.cqe_next = (elm); \
260 (head)->cqh_last = (elm); \
261 } while (0)
263 #define CIRCLEQ_REMOVE(head, elm, field) do { \
264 if ((elm)->field.cqe_next == (void *)(head)) \
265 (head)->cqh_last = (elm)->field.cqe_prev; \
266 else \
267 (elm)->field.cqe_next->field.cqe_prev = \
268 (elm)->field.cqe_prev; \
269 if ((elm)->field.cqe_prev == (void *)(head)) \
270 (head)->cqh_first = (elm)->field.cqe_next; \
271 else \
272 (elm)->field.cqe_prev->field.cqe_next = \
273 (elm)->field.cqe_next; \
274 } while (0)
275 #endif /* !_SYS_QUEUE_H_ */