1 /* ldap_queue.h -- queue macros */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Copyright (c) 1991, 1993
17 * The Regents of the University of California. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 * must display the following acknowledgement:
29 * This product includes software developed by the University of
30 * California, Berkeley and its contributors.
31 * 4. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * @(#)queue.h 8.5 (Berkeley) 8/20/94
48 * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.5 2001/09/30 21:12:54 luigi Exp $
50 * See also: ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
53 * This work is derived from FreeBSD queue.h work. Adapted for use in
54 * OpenLDAP Software by Kurt D. Zeilenga.
57 #ifndef _LDAP_QUEUE_H_
58 #define _LDAP_QUEUE_H_
61 * This file defines five types of data structures: singly-linked lists,
62 * singly-linked tail queues, lists, tail queues, and circular queues.
64 * A singly-linked list is headed by a single forward pointer. The elements
65 * are singly linked for minimum space and pointer manipulation overhead at
66 * the expense of O(n) removal for arbitrary elements. New elements can be
67 * added to the list after an existing element or at the head of the list.
68 * Elements being removed from the head of the list should use the explicit
69 * macro for this purpose for optimum efficiency. A singly-linked list may
70 * only be traversed in the forward direction. Singly-linked lists are ideal
71 * for applications with large datasets and few or no removals or for
72 * implementing a LIFO queue.
74 * A singly-linked tail queue is headed by a pair of pointers, one to the
75 * head of the list and the other to the tail of the list. The elements are
76 * singly linked for minimum space and pointer manipulation overhead at the
77 * expense of O(n) removal for arbitrary elements. New elements can be added
78 * to the list after an existing element, at the head of the list, or at the
79 * end of the list. Elements being removed from the head of the tail queue
80 * should use the explicit macro for this purpose for optimum efficiency.
81 * A singly-linked tail queue may only be traversed in the forward direction.
82 * Singly-linked tail queues are ideal for applications with large datasets
83 * and few or no removals or for implementing a FIFO queue.
85 * A list is headed by a single forward pointer (or an array of forward
86 * pointers for a hash table header). The elements are doubly linked
87 * so that an arbitrary element can be removed without a need to
88 * traverse the list. New elements can be added to the list before
89 * or after an existing element or at the head of the list. A list
90 * may only be traversed in the forward direction.
92 * A tail queue is headed by a pair of pointers, one to the head of the
93 * list and the other to the tail of the list. The elements are doubly
94 * linked so that an arbitrary element can be removed without a need to
95 * traverse the list. New elements can be added to the list before or
96 * after an existing element, at the head of the list, or at the end of
97 * the list. A tail queue may be traversed in either direction.
99 * A circle queue is headed by a pair of pointers, one to the head of the
100 * list and the other to the tail of the list. The elements are doubly
101 * linked so that an arbitrary element can be removed without a need to
102 * traverse the list. New elements can be added to the list before or after
103 * an existing element, at the head of the list, or at the end of the list.
104 * A circle queue may be traversed in either direction, but has a more
105 * complex end of list detection.
107 * For details on the use of these macros, see the queue(3) manual page.
108 * All macros are prefixed with LDAP_.
110 * SLIST_ LIST_ STAILQ_ TAILQ_ CIRCLEQ_
120 * _FOREACH_REVERSE - - - + +
121 * _INSERT_HEAD + + + + +
122 * _INSERT_BEFORE - + - + +
123 * _INSERT_AFTER + + + + +
124 * _INSERT_TAIL - - + + +
125 * _REMOVE_HEAD + - + - -
131 * Singly-linked List definitions.
133 #define LDAP_SLIST_HEAD(name, type) \
135 struct type *slh_first; /* first element */ \
138 #define LDAP_SLIST_HEAD_INITIALIZER(head) \
141 #define LDAP_SLIST_ENTRY(type) \
143 struct type *sle_next; /* next element */ \
146 #define LDAP_SLIST_ENTRY_INITIALIZER(entry) \
150 * Singly-linked List functions.
152 #define LDAP_SLIST_EMPTY(head) ((head)->slh_first == NULL)
154 #define LDAP_SLIST_FIRST(head) ((head)->slh_first)
156 #define LDAP_SLIST_FOREACH(var, head, field) \
157 for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
159 #define LDAP_SLIST_INIT(head) { \
160 (head)->slh_first = NULL; \
163 #define LDAP_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
164 (elm)->field.sle_next = (slistelm)->field.sle_next; \
165 (slistelm)->field.sle_next = (elm); \
168 #define LDAP_SLIST_INSERT_HEAD(head, elm, field) do { \
169 (elm)->field.sle_next = (head)->slh_first; \
170 (head)->slh_first = (elm); \
173 #define LDAP_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
175 #define LDAP_SLIST_REMOVE_HEAD(head, field) do { \
176 (head)->slh_first = (head)->slh_first->field.sle_next; \
179 #define LDAP_SLIST_REMOVE(head, elm, type, field) do { \
180 if ((head)->slh_first == (elm)) { \
181 LDAP_SLIST_REMOVE_HEAD((head), field); \
184 struct type *curelm = (head)->slh_first; \
185 while( curelm->field.sle_next != (elm) ) \
186 curelm = curelm->field.sle_next; \
187 curelm->field.sle_next = \
188 curelm->field.sle_next->field.sle_next; \
193 * Singly-linked Tail queue definitions.
195 #define LDAP_STAILQ_HEAD(name, type) \
197 struct type *stqh_first;/* first element */ \
198 struct type **stqh_last;/* addr of last next element */ \
201 #define LDAP_STAILQ_HEAD_INITIALIZER(head) \
202 { NULL, &(head).stqh_first }
204 #define LDAP_STAILQ_ENTRY(type) \
206 struct type *stqe_next; /* next element */ \
209 #define LDAP_STAILQ_ENTRY_INITIALIZER(entry) \
213 * Singly-linked Tail queue functions.
215 #define LDAP_STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
217 #define LDAP_STAILQ_INIT(head) do { \
218 (head)->stqh_first = NULL; \
219 (head)->stqh_last = &(head)->stqh_first; \
222 #define LDAP_STAILQ_FIRST(head) ((head)->stqh_first)
224 #define LDAP_STAILQ_LAST(head, type, field) \
225 (LDAP_STAILQ_EMPTY(head) ? \
228 ((char *)((head)->stqh_last) - offsetof(struct type, field))))
230 #define LDAP_STAILQ_FOREACH(var, head, field) \
231 for((var) = (head)->stqh_first; (var); (var) = (var)->field.stqe_next)
233 #define LDAP_STAILQ_INSERT_HEAD(head, elm, field) do { \
234 if (((elm)->field.stqe_next = (head)->stqh_first) == NULL) \
235 (head)->stqh_last = &(elm)->field.stqe_next; \
236 (head)->stqh_first = (elm); \
239 #define LDAP_STAILQ_INSERT_TAIL(head, elm, field) do { \
240 (elm)->field.stqe_next = NULL; \
241 *(head)->stqh_last = (elm); \
242 (head)->stqh_last = &(elm)->field.stqe_next; \
245 #define LDAP_STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
246 if (((elm)->field.stqe_next = (tqelm)->field.stqe_next) == NULL)\
247 (head)->stqh_last = &(elm)->field.stqe_next; \
248 (tqelm)->field.stqe_next = (elm); \
251 #define LDAP_STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
253 #define LDAP_STAILQ_REMOVE_HEAD(head, field) do { \
254 if (((head)->stqh_first = \
255 (head)->stqh_first->field.stqe_next) == NULL) \
256 (head)->stqh_last = &(head)->stqh_first; \
259 #define LDAP_STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \
260 if (((head)->stqh_first = (elm)->field.stqe_next) == NULL) \
261 (head)->stqh_last = &(head)->stqh_first; \
264 #define LDAP_STAILQ_REMOVE(head, elm, type, field) do { \
265 if ((head)->stqh_first == (elm)) { \
266 LDAP_STAILQ_REMOVE_HEAD(head, field); \
269 struct type *curelm = (head)->stqh_first; \
270 while( curelm->field.stqe_next != (elm) ) \
271 curelm = curelm->field.stqe_next; \
272 if((curelm->field.stqe_next = \
273 curelm->field.stqe_next->field.stqe_next) == NULL) \
274 (head)->stqh_last = &(curelm)->field.stqe_next; \
281 #define LDAP_LIST_HEAD(name, type) \
283 struct type *lh_first; /* first element */ \
286 #define LDAP_LIST_HEAD_INITIALIZER(head) \
289 #define LDAP_LIST_ENTRY(type) \
291 struct type *le_next; /* next element */ \
292 struct type **le_prev; /* address of previous next element */ \
295 #define LDAP_LIST_ENTRY_INITIALIZER(entry) \
302 #define LDAP_LIST_EMPTY(head) ((head)->lh_first == NULL)
304 #define LDAP_LIST_FIRST(head) ((head)->lh_first)
306 #define LDAP_LIST_FOREACH(var, head, field) \
307 for((var) = (head)->lh_first; (var); (var) = (var)->field.le_next)
309 #define LDAP_LIST_INIT(head) do { \
310 (head)->lh_first = NULL; \
313 #define LDAP_LIST_INSERT_AFTER(listelm, elm, field) do { \
314 if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
315 (listelm)->field.le_next->field.le_prev = \
316 &(elm)->field.le_next; \
317 (listelm)->field.le_next = (elm); \
318 (elm)->field.le_prev = &(listelm)->field.le_next; \
321 #define LDAP_LIST_INSERT_BEFORE(listelm, elm, field) do { \
322 (elm)->field.le_prev = (listelm)->field.le_prev; \
323 (elm)->field.le_next = (listelm); \
324 *(listelm)->field.le_prev = (elm); \
325 (listelm)->field.le_prev = &(elm)->field.le_next; \
328 #define LDAP_LIST_INSERT_HEAD(head, elm, field) do { \
329 if (((elm)->field.le_next = (head)->lh_first) != NULL) \
330 (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
331 (head)->lh_first = (elm); \
332 (elm)->field.le_prev = &(head)->lh_first; \
335 #define LDAP_LIST_NEXT(elm, field) ((elm)->field.le_next)
337 #define LDAP_LIST_REMOVE(elm, field) do { \
338 if ((elm)->field.le_next != NULL) \
339 (elm)->field.le_next->field.le_prev = \
340 (elm)->field.le_prev; \
341 *(elm)->field.le_prev = (elm)->field.le_next; \
345 * Tail queue definitions.
347 #define LDAP_TAILQ_HEAD(name, type) \
349 struct type *tqh_first; /* first element */ \
350 struct type **tqh_last; /* addr of last next element */ \
353 #define LDAP_TAILQ_HEAD_INITIALIZER(head) \
354 { NULL, &(head).tqh_first }
356 #define LDAP_TAILQ_ENTRY(type) \
358 struct type *tqe_next; /* next element */ \
359 struct type **tqe_prev; /* address of previous next element */ \
362 #define LDAP_TAILQ_ENTRY_INITIALIZER(entry) \
366 * Tail queue functions.
368 #define LDAP_TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
370 #define LDAP_TAILQ_FOREACH(var, head, field) \
371 for (var = LDAP_TAILQ_FIRST(head); var; var = LDAP_TAILQ_NEXT(var, field))
373 #define LDAP_TAILQ_FOREACH_REVERSE(var, head, type, field) \
374 for ((var) = LDAP_TAILQ_LAST((head), type, field); \
376 (var) = LDAP_TAILQ_PREV((var), head, type, field))
378 #define LDAP_TAILQ_FIRST(head) ((head)->tqh_first)
380 #define LDAP_TAILQ_LAST(head, type, field) \
381 (LDAP_TAILQ_EMPTY(head) ? \
384 ((char *)((head)->tqh_last) - offsetof(struct type, field))))
386 #define LDAP_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
388 #define LDAP_TAILQ_PREV(elm, head, type, field) \
389 ((struct type *)((elm)->field.tqe_prev) == LDAP_TAILQ_FIRST(head) ? \
392 ((char *)((elm)->field.tqe_prev) - offsetof(struct type, field))))
394 #define LDAP_TAILQ_INIT(head) do { \
395 (head)->tqh_first = NULL; \
396 (head)->tqh_last = &(head)->tqh_first; \
399 #define LDAP_TAILQ_INSERT_HEAD(head, elm, field) do { \
400 if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
401 (head)->tqh_first->field.tqe_prev = \
402 &(elm)->field.tqe_next; \
404 (head)->tqh_last = &(elm)->field.tqe_next; \
405 (head)->tqh_first = (elm); \
406 (elm)->field.tqe_prev = &(head)->tqh_first; \
409 #define LDAP_TAILQ_INSERT_TAIL(head, elm, field) do { \
410 (elm)->field.tqe_next = NULL; \
411 (elm)->field.tqe_prev = (head)->tqh_last; \
412 *(head)->tqh_last = (elm); \
413 (head)->tqh_last = &(elm)->field.tqe_next; \
416 #define LDAP_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
417 if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
418 (elm)->field.tqe_next->field.tqe_prev = \
419 &(elm)->field.tqe_next; \
421 (head)->tqh_last = &(elm)->field.tqe_next; \
422 (listelm)->field.tqe_next = (elm); \
423 (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
426 #define LDAP_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
427 (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
428 (elm)->field.tqe_next = (listelm); \
429 *(listelm)->field.tqe_prev = (elm); \
430 (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
433 #define LDAP_TAILQ_REMOVE(head, elm, field) do { \
434 if (((elm)->field.tqe_next) != NULL) \
435 (elm)->field.tqe_next->field.tqe_prev = \
436 (elm)->field.tqe_prev; \
438 (head)->tqh_last = (elm)->field.tqe_prev; \
439 *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
443 * Circular queue definitions.
445 #define LDAP_CIRCLEQ_HEAD(name, type) \
447 struct type *cqh_first; /* first element */ \
448 struct type *cqh_last; /* last element */ \
451 #define LDAP_CIRCLEQ_ENTRY(type) \
453 struct type *cqe_next; /* next element */ \
454 struct type *cqe_prev; /* previous element */ \
458 * Circular queue functions.
460 #define LDAP_CIRCLEQ_EMPTY(head) ((head)->cqh_first == (void *)(head))
462 #define LDAP_CIRCLEQ_FIRST(head) ((head)->cqh_first)
464 #define LDAP_CIRCLEQ_FOREACH(var, head, field) \
465 for((var) = (head)->cqh_first; \
466 (var) != (void *)(head); \
467 (var) = (var)->field.cqe_next)
469 #define LDAP_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
470 for((var) = (head)->cqh_last; \
471 (var) != (void *)(head); \
472 (var) = (var)->field.cqe_prev)
474 #define LDAP_CIRCLEQ_INIT(head) do { \
475 (head)->cqh_first = (void *)(head); \
476 (head)->cqh_last = (void *)(head); \
479 #define LDAP_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
480 (elm)->field.cqe_next = (listelm)->field.cqe_next; \
481 (elm)->field.cqe_prev = (listelm); \
482 if ((listelm)->field.cqe_next == (void *)(head)) \
483 (head)->cqh_last = (elm); \
485 (listelm)->field.cqe_next->field.cqe_prev = (elm); \
486 (listelm)->field.cqe_next = (elm); \
489 #define LDAP_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
490 (elm)->field.cqe_next = (listelm); \
491 (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
492 if ((listelm)->field.cqe_prev == (void *)(head)) \
493 (head)->cqh_first = (elm); \
495 (listelm)->field.cqe_prev->field.cqe_next = (elm); \
496 (listelm)->field.cqe_prev = (elm); \
499 #define LDAP_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
500 (elm)->field.cqe_next = (head)->cqh_first; \
501 (elm)->field.cqe_prev = (void *)(head); \
502 if ((head)->cqh_last == (void *)(head)) \
503 (head)->cqh_last = (elm); \
505 (head)->cqh_first->field.cqe_prev = (elm); \
506 (head)->cqh_first = (elm); \
509 #define LDAP_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
510 (elm)->field.cqe_next = (void *)(head); \
511 (elm)->field.cqe_prev = (head)->cqh_last; \
512 if ((head)->cqh_first == (void *)(head)) \
513 (head)->cqh_first = (elm); \
515 (head)->cqh_last->field.cqe_next = (elm); \
516 (head)->cqh_last = (elm); \
519 #define LDAP_CIRCLEQ_LAST(head) ((head)->cqh_last)
521 #define LDAP_CIRCLEQ_NEXT(elm,field) ((elm)->field.cqe_next)
523 #define LDAP_CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
525 #define LDAP_CIRCLEQ_REMOVE(head, elm, field) do { \
526 if ((elm)->field.cqe_next == (void *)(head)) \
527 (head)->cqh_last = (elm)->field.cqe_prev; \
529 (elm)->field.cqe_next->field.cqe_prev = \
530 (elm)->field.cqe_prev; \
531 if ((elm)->field.cqe_prev == (void *)(head)) \
532 (head)->cqh_first = (elm)->field.cqe_next; \
534 (elm)->field.cqe_prev->field.cqe_next = \
535 (elm)->field.cqe_next; \
538 #endif /* !_LDAP_QUEUE_H_ */