]> git.sur5r.net Git - openldap/blobdiff - include/ldap_queue.h
More fixes, add test script
[openldap] / include / ldap_queue.h
index bdb239dc822064532a02b1a14bf942e837b2cbca..dc90ac9be03eac0e94b32bc8d9eebd99b7f03ebe 100644 (file)
@@ -1,17 +1,19 @@
-/*
- * Copyright 2001 The OpenLDAP Foundation, Redwood City, California, USA
+/* ldap_queue.h -- queue macros */
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 2001-2011 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted only as authorized by the OpenLDAP
- * Public License.  A copy of this license is available at
- * http://www.OpenLDAP.org/license.html or in file LICENSE in the
- * top-level directory of the distribution.
+ * Public License.
+ *
+ * A copy of this license is available in file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
-/* stolen from FreeBSD for use in OpenLDAP */
-/* $OpenLDAP$ */
-/*
- * Copyright (c) 1991, 1993
+/* Copyright (c) 1991, 1993
  *     The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  *
  *     @(#)queue.h     8.5 (Berkeley) 8/20/94
  * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.5 2001/09/30 21:12:54 luigi Exp $
+ *
+ * See also: ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
+ */
+/* ACKNOWLEDGEMENTS:
+ * This work is derived from FreeBSD queue.h work.  Adapted for use in
+ * OpenLDAP Software by Kurt D. Zeilenga.
  */
 
 #ifndef _LDAP_QUEUE_H_
  * complex end of list detection.
  *
  * For details on the use of these macros, see the queue(3) manual page.
+ * All macros are prefixed with LDAP_.
  *
- *
- *                     LDAP_SLIST      LIST    LDAP_STAILQ     TAILQ   LDAP_CIRCLEQ
+ *                     SLIST_  LIST_   STAILQ_ TAILQ_  CIRCLEQ_
  * _HEAD               +       +       +       +       +
  * _ENTRY              +       +       +       +       +
  * _INIT               +       +       +       +       +
+ * _ENTRY_INIT         +       +       +       +       +
  * _EMPTY              +       +       +       +       +
  * _FIRST              +       +       +       +       +
  * _NEXT               +       +       +       +       +
  * _REMOVE             +       +       +       +       +
  *
  */
-/*
- * see queue(3) for instructions on how to use
- */
 
 /*
  * Singly-linked List definitions.
  */
-#define LDAP_SLIST_HEAD(name, type)                                            \
+#define LDAP_SLIST_HEAD(name, type)                                    \
 struct name {                                                          \
        struct type *slh_first; /* first element */                     \
 }
 
-#define LDAP_SLIST_HEAD_INITIALIZER(head)                                      \
+#define LDAP_SLIST_HEAD_INITIALIZER(head)                              \
        { NULL }
 
 #define LDAP_SLIST_ENTRY(type)                                         \
@@ -138,6 +144,9 @@ struct {                                                            \
        struct type *sle_next;  /* next element */                      \
 }
 
+#define LDAP_SLIST_ENTRY_INITIALIZER(entry)                            \
+       { NULL }
+
 /*
  * Singly-linked List functions.
  */
@@ -145,14 +154,18 @@ struct {                                                          \
 
 #define        LDAP_SLIST_FIRST(head)  ((head)->slh_first)
 
-#define LDAP_SLIST_FOREACH(var, head, field)                                   \
+#define LDAP_SLIST_FOREACH(var, head, field)                           \
        for((var) = (head)->slh_first; (var); (var) = (var)->field.sle_next)
 
 #define LDAP_SLIST_INIT(head) {                                                \
        (head)->slh_first = NULL;                                       \
 }
 
-#define LDAP_SLIST_INSERT_AFTER(slistelm, elm, field) do {                     \
+#define LDAP_SLIST_ENTRY_INIT(var, field) {                            \
+       (var)->field.sle_next = NULL;                                   \
+}
+
+#define LDAP_SLIST_INSERT_AFTER(slistelm, elm, field) do {             \
        (elm)->field.sle_next = (slistelm)->field.sle_next;             \
        (slistelm)->field.sle_next = (elm);                             \
 } while (0)
@@ -164,7 +177,7 @@ struct {                                                            \
 
 #define LDAP_SLIST_NEXT(elm, field)    ((elm)->field.sle_next)
 
-#define LDAP_SLIST_REMOVE_HEAD(head, field) do {                               \
+#define LDAP_SLIST_REMOVE_HEAD(head, field) do {                       \
        (head)->slh_first = (head)->slh_first->field.sle_next;          \
 } while (0)
 
@@ -184,13 +197,13 @@ struct {                                                          \
 /*
  * Singly-linked Tail queue definitions.
  */
-#define LDAP_STAILQ_HEAD(name, type)                                           \
+#define LDAP_STAILQ_HEAD(name, type)                                   \
 struct name {                                                          \
        struct type *stqh_first;/* first element */                     \
        struct type **stqh_last;/* addr of last next element */         \
 }
 
-#define LDAP_STAILQ_HEAD_INITIALIZER(head)                                     \
+#define LDAP_STAILQ_HEAD_INITIALIZER(head)                             \
        { NULL, &(head).stqh_first }
 
 #define LDAP_STAILQ_ENTRY(type)                                                \
@@ -198,20 +211,27 @@ struct {                                                          \
        struct type *stqe_next; /* next element */                      \
 }
 
+#define LDAP_STAILQ_ENTRY_INITIALIZER(entry)                           \
+       { NULL }
+
 /*
  * Singly-linked Tail queue functions.
  */
 #define LDAP_STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
 
-#define        LDAP_STAILQ_INIT(head) do {                                             \
+#define        LDAP_STAILQ_INIT(head) do {                                     \
        (head)->stqh_first = NULL;                                      \
        (head)->stqh_last = &(head)->stqh_first;                        \
 } while (0)
 
+#define LDAP_STAILQ_ENTRY_INIT(var, field) {                           \
+       (entry)->field.stqe_next = NULL;                                \
+}
+
 #define LDAP_STAILQ_FIRST(head)        ((head)->stqh_first)
 
-#define        LDAP_STAILQ_LAST(head, type, field)                                     \
-       (LDAP_STAILQ_EMPTY(head) ?                                              \
+#define        LDAP_STAILQ_LAST(head, type, field)                             \
+       (LDAP_STAILQ_EMPTY(head) ?                                      \
                NULL :                                                  \
                ((struct type *)                                        \
                ((char *)((head)->stqh_last) - offsetof(struct type, field))))
@@ -239,13 +259,13 @@ struct {                                                          \
 
 #define LDAP_STAILQ_NEXT(elm, field)   ((elm)->field.stqe_next)
 
-#define LDAP_STAILQ_REMOVE_HEAD(head, field) do {                              \
+#define LDAP_STAILQ_REMOVE_HEAD(head, field) do {                      \
        if (((head)->stqh_first =                                       \
             (head)->stqh_first->field.stqe_next) == NULL)              \
                (head)->stqh_last = &(head)->stqh_first;                \
 } while (0)
 
-#define LDAP_STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {                   \
+#define LDAP_STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do {           \
        if (((head)->stqh_first = (elm)->field.stqe_next) == NULL)      \
                (head)->stqh_last = &(head)->stqh_first;                \
 } while (0)
@@ -267,12 +287,12 @@ struct {                                                          \
 /*
  * List definitions.
  */
-#define LDAP_LIST_HEAD(name, type)                                             \
+#define LDAP_LIST_HEAD(name, type)                                     \
 struct name {                                                          \
        struct type *lh_first;  /* first element */                     \
 }
 
-#define LDAP_LIST_HEAD_INITIALIZER(head)                                       \
+#define LDAP_LIST_HEAD_INITIALIZER(head)                               \
        { NULL }
 
 #define LDAP_LIST_ENTRY(type)                                          \
@@ -281,6 +301,9 @@ struct {                                                            \
        struct type **le_prev;  /* address of previous next element */  \
 }
 
+#define LDAP_LIST_ENTRY_INITIALIZER(entry)                     \
+       { NULL, NULL }
+
 /*
  * List functions.
  */
@@ -289,14 +312,19 @@ struct {                                                          \
 
 #define LDAP_LIST_FIRST(head)  ((head)->lh_first)
 
-#define LDAP_LIST_FOREACH(var, head, field)                                    \
+#define LDAP_LIST_FOREACH(var, head, field)                            \
        for((var) = (head)->lh_first; (var); (var) = (var)->field.le_next)
 
-#define        LDAP_LIST_INIT(head) do {                                               \
+#define        LDAP_LIST_INIT(head) do {                                       \
        (head)->lh_first = NULL;                                        \
 } while (0)
 
-#define LDAP_LIST_INSERT_AFTER(listelm, elm, field) do {                       \
+#define LDAP_LIST_ENTRY_INIT(var, field) do {                          \
+       (var)->field.le_next = NULL;                                    \
+       (var)->field.le_prev = NULL;                                    \
+} while (0)
+
+#define LDAP_LIST_INSERT_AFTER(listelm, elm, field) do {               \
        if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)  \
                (listelm)->field.le_next->field.le_prev =               \
                    &(elm)->field.le_next;                              \
@@ -304,14 +332,14 @@ struct {                                                          \
        (elm)->field.le_prev = &(listelm)->field.le_next;               \
 } while (0)
 
-#define LDAP_LIST_INSERT_BEFORE(listelm, elm, field) do {                      \
+#define LDAP_LIST_INSERT_BEFORE(listelm, elm, field) do {              \
        (elm)->field.le_prev = (listelm)->field.le_prev;                \
        (elm)->field.le_next = (listelm);                               \
        *(listelm)->field.le_prev = (elm);                              \
        (listelm)->field.le_prev = &(elm)->field.le_next;               \
 } while (0)
 
-#define LDAP_LIST_INSERT_HEAD(head, elm, field) do {                           \
+#define LDAP_LIST_INSERT_HEAD(head, elm, field) do {                   \
        if (((elm)->field.le_next = (head)->lh_first) != NULL)          \
                (head)->lh_first->field.le_prev = &(elm)->field.le_next;\
        (head)->lh_first = (elm);                                       \
@@ -320,7 +348,7 @@ struct {                                                            \
 
 #define LDAP_LIST_NEXT(elm, field)     ((elm)->field.le_next)
 
-#define LDAP_LIST_REMOVE(elm, field) do {                                      \
+#define LDAP_LIST_REMOVE(elm, field) do {                              \
        if ((elm)->field.le_next != NULL)                               \
                (elm)->field.le_next->field.le_prev =                   \
                    (elm)->field.le_prev;                               \
@@ -330,13 +358,13 @@ struct {                                                          \
 /*
  * Tail queue definitions.
  */
-#define LDAP_TAILQ_HEAD(name, type)                                            \
+#define LDAP_TAILQ_HEAD(name, type)                                    \
 struct name {                                                          \
        struct type *tqh_first; /* first element */                     \
        struct type **tqh_last; /* addr of last next element */         \
 }
 
-#define LDAP_TAILQ_HEAD_INITIALIZER(head)                                      \
+#define LDAP_TAILQ_HEAD_INITIALIZER(head)                              \
        { NULL, &(head).tqh_first }
 
 #define LDAP_TAILQ_ENTRY(type)                                         \
@@ -345,34 +373,48 @@ struct {                                                          \
        struct type **tqe_prev; /* address of previous next element */  \
 }
 
+#define LDAP_TAILQ_ENTRY_INITIALIZER(entry)                            \
+       { NULL, NULL }
+
 /*
  * Tail queue functions.
  */
 #define        LDAP_TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
 
-#define LDAP_TAILQ_FOREACH(var, head, field)                                   \
-       for (var = LDAP_TAILQ_FIRST(head); var; var = TAILQ_NEXT(var, field))
+#define LDAP_TAILQ_FOREACH(var, head, field)                           \
+       for (var = LDAP_TAILQ_FIRST(head); var; var = LDAP_TAILQ_NEXT(var, field))
 
-#define LDAP_TAILQ_FOREACH_REVERSE(var, head, headname, field)         \
-       for ((var) = LDAP_TAILQ_LAST((head), headname);                 \
+#define LDAP_TAILQ_FOREACH_REVERSE(var, head, type, field)             \
+       for ((var) = LDAP_TAILQ_LAST((head), type, field);              \
             (var);                                                     \
-            (var) = LDAP_TAILQ_PREV((var), headname, field))
+            (var) = LDAP_TAILQ_PREV((var), head, type, field))
 
 #define        LDAP_TAILQ_FIRST(head) ((head)->tqh_first)
 
-#define        LDAP_TAILQ_LAST(head, headname) \
-       (*(((struct headname *)((head)->tqh_last))->tqh_last))
+#define LDAP_TAILQ_LAST(head, type, field)                             \
+       (LDAP_TAILQ_EMPTY(head) ?                                       \
+               NULL :                                                  \
+               ((struct type *)                                        \
+               ((char *)((head)->tqh_last) - offsetof(struct type, field))))
 
 #define        LDAP_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
 
-#define LDAP_TAILQ_PREV(elm, headname, field) \
-       (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
+#define LDAP_TAILQ_PREV(elm, head, type, field)                        \
+       ((struct type *)((elm)->field.tqe_prev) == LDAP_TAILQ_FIRST(head) ? \
+       NULL :                                                          \
+       ((struct type *)                                                \
+       ((char *)((elm)->field.tqe_prev) - offsetof(struct type, field))))
 
-#define        LDAP_TAILQ_INIT(head) do {                                              \
+#define        LDAP_TAILQ_INIT(head) do {                                      \
        (head)->tqh_first = NULL;                                       \
        (head)->tqh_last = &(head)->tqh_first;                          \
 } while (0)
 
+#define LDAP_TAILQ_ENTRY_INIT(var, field) do {                         \
+       (var)->field.tqe_next = NULL;                                   \
+       (var)->field.tqe_prev = NULL;                                   \
+} while (0)
+
 #define LDAP_TAILQ_INSERT_HEAD(head, elm, field) do {                  \
        if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)        \
                (head)->tqh_first->field.tqe_prev =                     \
@@ -400,14 +442,14 @@ struct {                                                          \
        (elm)->field.tqe_prev = &(listelm)->field.tqe_next;             \
 } while (0)
 
-#define LDAP_TAILQ_INSERT_BEFORE(listelm, elm, field) do {                     \
+#define LDAP_TAILQ_INSERT_BEFORE(listelm, elm, field) do {             \
        (elm)->field.tqe_prev = (listelm)->field.tqe_prev;              \
        (elm)->field.tqe_next = (listelm);                              \
        *(listelm)->field.tqe_prev = (elm);                             \
        (listelm)->field.tqe_prev = &(elm)->field.tqe_next;             \
 } while (0)
 
-#define LDAP_TAILQ_REMOVE(head, elm, field) do {                               \
+#define LDAP_TAILQ_REMOVE(head, elm, field) do {                       \
        if (((elm)->field.tqe_next) != NULL)                            \
                (elm)->field.tqe_next->field.tqe_prev =                 \
                    (elm)->field.tqe_prev;                              \
@@ -425,7 +467,7 @@ struct name {                                                               \
        struct type *cqh_last;          /* last element */              \
 }
 
-#define LDAP_CIRCLEQ_ENTRY(type)                                               \
+#define LDAP_CIRCLEQ_ENTRY(type)                                       \
 struct {                                                               \
        struct type *cqe_next;          /* next element */              \
        struct type *cqe_prev;          /* previous element */          \
@@ -448,12 +490,17 @@ struct {                                                          \
            (var) != (void *)(head);                                    \
            (var) = (var)->field.cqe_prev)
 
-#define        LDAP_CIRCLEQ_INIT(head) do {                                            \
+#define        LDAP_CIRCLEQ_INIT(head) do {                                    \
        (head)->cqh_first = (void *)(head);                             \
        (head)->cqh_last = (void *)(head);                              \
 } while (0)
 
-#define LDAP_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {              \
+#define LDAP_CIRCLEQ_ENTRY_INIT(var, field) do {                       \
+       (var)->field.cqe_next = NULL;                                   \
+       (var)->field.cqe_prev = NULL;                                   \
+} while (0)
+
+#define LDAP_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {      \
        (elm)->field.cqe_next = (listelm)->field.cqe_next;              \
        (elm)->field.cqe_prev = (listelm);                              \
        if ((listelm)->field.cqe_next == (void *)(head))                \
@@ -463,7 +510,7 @@ struct {                                                            \
        (listelm)->field.cqe_next = (elm);                              \
 } while (0)
 
-#define LDAP_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {             \
+#define LDAP_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {     \
        (elm)->field.cqe_next = (listelm);                              \
        (elm)->field.cqe_prev = (listelm)->field.cqe_prev;              \
        if ((listelm)->field.cqe_prev == (void *)(head))                \
@@ -499,7 +546,7 @@ struct {                                                            \
 
 #define LDAP_CIRCLEQ_PREV(elm,field) ((elm)->field.cqe_prev)
 
-#define        LDAP_CIRCLEQ_REMOVE(head, elm, field) do {                              \
+#define        LDAP_CIRCLEQ_REMOVE(head, elm, field) do {                      \
        if ((elm)->field.cqe_next == (void *)(head))                    \
                (head)->cqh_last = (elm)->field.cqe_prev;               \
        else                                                            \