tevent: add tevent_dlinklist.h as copy from lib/util/dlinklist.h
authorStefan Metzmacher <metze@samba.org>
Mon, 24 Apr 2023 10:39:17 +0000 (12:39 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Wed, 19 Jul 2023 08:02:33 +0000 (08:02 +0000)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
lib/tevent/tevent_dlinklist.h [new file with mode: 0644]
lib/tevent/tevent_util.h

diff --git a/lib/tevent/tevent_dlinklist.h b/lib/tevent/tevent_dlinklist.h
new file mode 100644 (file)
index 0000000..a775e8d
--- /dev/null
@@ -0,0 +1,177 @@
+/*
+   Unix SMB/CIFS implementation.
+   some simple double linked list macros
+
+   Copyright (C) Andrew Tridgell 1998-2010
+
+     ** NOTE! The following LGPL license applies to this file (*dlinklist.h).
+     ** This does NOT imply that all of Samba is released under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+/* To use these macros you must have a structure containing a next and
+   prev pointer */
+
+#ifndef _DLINKLIST_H
+#define _DLINKLIST_H
+
+/*
+  February 2010 - changed list format to have a prev pointer from the
+  list head. This makes DLIST_ADD_END() O(1) even though we only have
+  one list pointer.
+
+  The scheme is as follows:
+
+     1) with no entries in the list:
+          list_head == NULL
+
+     2) with 1 entry in the list:
+          list_head->next == NULL
+          list_head->prev == list_head
+
+     3) with 2 entries in the list:
+          list_head->next == element2
+          list_head->prev == element2
+         element2->prev == list_head
+         element2->next == NULL
+
+     4) with N entries in the list:
+          list_head->next == element2
+          list_head->prev == elementN
+         elementN->prev == element{N-1}
+         elementN->next == NULL
+
+  This allows us to find the tail of the list by using
+  list_head->prev, which means we can add to the end of the list in
+  O(1) time
+ */
+
+
+/*
+   add an element at the front of a list
+*/
+#define DLIST_ADD(list, p) \
+do { \
+        if (!(list)) { \
+               (p)->prev = (list) = (p);  \
+               (p)->next = NULL; \
+       } else { \
+               (p)->prev = (list)->prev; \
+               (list)->prev = (p); \
+               (p)->next = (list); \
+               (list) = (p); \
+       } \
+} while (0)
+
+/*
+   remove an element from a list
+   Note that the element doesn't have to be in the list. If it
+   isn't then this is a no-op
+*/
+#define DLIST_REMOVE(list, p) \
+do { \
+       if ((p) == (list)) { \
+               if ((p)->next) (p)->next->prev = (p)->prev; \
+               (list) = (p)->next; \
+       } else if ((p)->prev && (list) && (p) == (list)->prev) {        \
+               (p)->prev->next = NULL; \
+               (list)->prev = (p)->prev; \
+       } else { \
+               if ((p)->prev) (p)->prev->next = (p)->next; \
+               if ((p)->next) (p)->next->prev = (p)->prev; \
+       } \
+       if ((p) != (list)) (p)->next = (p)->prev = NULL;        \
+} while (0)
+
+/*
+   find the head of the list given any element in it.
+   Note that this costs O(N), so you should avoid this macro
+   if at all possible!
+*/
+#define DLIST_HEAD(p, result_head) \
+do { \
+       (result_head) = (p); \
+       while (DLIST_PREV(result_head)) (result_head) = (result_head)->prev; \
+} while(0)
+
+/* return the last element in the list */
+#define DLIST_TAIL(list) ((list)?(list)->prev:NULL)
+
+/* return the previous element in the list. */
+#define DLIST_PREV(p) (((p)->prev && (p)->prev->next != NULL)?(p)->prev:NULL)
+
+/* insert 'p' after the given element 'el' in a list. If el is NULL then
+   this is the same as a DLIST_ADD() */
+#define DLIST_ADD_AFTER(list, p, el) \
+do { \
+        if (!(list) || !(el)) { \
+               DLIST_ADD(list, p); \
+       } else { \
+               (p)->prev = (el);   \
+               (p)->next = (el)->next;         \
+               (el)->next = (p);               \
+               if ((p)->next) (p)->next->prev = (p);   \
+               if ((list)->prev == (el)) (list)->prev = (p); \
+       }\
+} while (0)
+
+
+/*
+   add to the end of a list.
+*/
+#define DLIST_ADD_END(list, p) \
+do { \
+       if (!(list)) { \
+               DLIST_ADD(list, p); \
+       } else { \
+               DLIST_ADD_AFTER(list, p, (list)->prev); \
+       } \
+} while (0)
+
+/* promote an element to the front of a list */
+#define DLIST_PROMOTE(list, p) \
+do { \
+          DLIST_REMOVE(list, p); \
+          DLIST_ADD(list, p); \
+} while (0)
+
+/*
+   demote an element to the end of a list.
+*/
+#define DLIST_DEMOTE(list, p) \
+do { \
+       DLIST_REMOVE(list, p); \
+       DLIST_ADD_END(list, p); \
+} while (0)
+
+/*
+   concatenate two lists - putting all elements of the 2nd list at the
+   end of the first list.
+*/
+#define DLIST_CONCATENATE(list1, list2) \
+do { \
+       if (!(list1)) { \
+               (list1) = (list2); \
+       } else { \
+               (list1)->prev->next = (list2); \
+               if (list2) { \
+                       void *_tmplist = (void *)(list1)->prev; \
+                       (list1)->prev = (list2)->prev; \
+                       (list2)->prev = _tmplist; \
+               } \
+       } \
+} while (0)
+
+#endif /* _DLINKLIST_H */
index 128c231e25cfac3d7580ad72eb708e5cc0caeefb..a7bba0bb269cf554b95a07bbe31bc68477a4ddaf 100644 (file)
 /*
    Unix SMB/CIFS implementation.
 
-   Copyright (C) Andrew Tridgell 1998-2010
+   Copyright (C) Andrew Tridgell 2005
    Copyright (C) Jelmer Vernooij 2005
 
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
-
-/* To use these macros you must have a structure containing a next and
-   prev pointer */
-
-#ifndef _DLINKLIST_H
-#define _DLINKLIST_H
-
-/*
-  February 2010 - changed list format to have a prev pointer from the
-  list head. This makes DLIST_ADD_END() O(1) even though we only have
-  one list pointer.
-
-  The scheme is as follows:
-
-     1) with no entries in the list:
-          list_head == NULL
-
-     2) with 1 entry in the list:
-          list_head->next == NULL
-          list_head->prev == list_head
-
-     3) with 2 entries in the list:
-          list_head->next == element2
-          list_head->prev == element2
-         element2->prev == list_head
-         element2->next == NULL
-
-     4) with N entries in the list:
-          list_head->next == element2
-          list_head->prev == elementN
-         elementN->prev == element{N-1}
-         elementN->next == NULL
-
-  This allows us to find the tail of the list by using
-  list_head->prev, which means we can add to the end of the list in
-  O(1) time
- */
-
-
-/*
-   add an element at the front of a list
-*/
-#define DLIST_ADD(list, p) \
-do { \
-        if (!(list)) { \
-               (p)->prev = (list) = (p);  \
-               (p)->next = NULL; \
-       } else { \
-               (p)->prev = (list)->prev; \
-               (list)->prev = (p); \
-               (p)->next = (list); \
-               (list) = (p); \
-       } \
-} while (0)
-
-/*
-   remove an element from a list
-   Note that the element doesn't have to be in the list. If it
-   isn't then this is a no-op
-*/
-#define DLIST_REMOVE(list, p) \
-do { \
-       if ((p) == (list)) { \
-               if ((p)->next) (p)->next->prev = (p)->prev; \
-               (list) = (p)->next; \
-       } else if ((p)->prev && (list) && (p) == (list)->prev) {        \
-               (p)->prev->next = NULL; \
-               (list)->prev = (p)->prev; \
-       } else { \
-               if ((p)->prev) (p)->prev->next = (p)->next; \
-               if ((p)->next) (p)->next->prev = (p)->prev; \
-       } \
-       if ((p) != (list)) (p)->next = (p)->prev = NULL;        \
-} while (0)
-
-/*
-   find the head of the list given any element in it.
-   Note that this costs O(N), so you should avoid this macro
-   if at all possible!
-*/
-#define DLIST_HEAD(p, result_head) \
-do { \
-       (result_head) = (p); \
-       while (DLIST_PREV(result_head)) (result_head) = (result_head)->prev; \
-} while(0)
-
-/* return the last element in the list */
-#define DLIST_TAIL(list) ((list)?(list)->prev:NULL)
-
-/* return the previous element in the list. */
-#define DLIST_PREV(p) (((p)->prev && (p)->prev->next != NULL)?(p)->prev:NULL)
-
-/* insert 'p' after the given element 'el' in a list. If el is NULL then
-   this is the same as a DLIST_ADD() */
-#define DLIST_ADD_AFTER(list, p, el) \
-do { \
-        if (!(list) || !(el)) { \
-               DLIST_ADD(list, p); \
-       } else { \
-               (p)->prev = (el);   \
-               (p)->next = (el)->next;         \
-               (el)->next = (p);               \
-               if ((p)->next) (p)->next->prev = (p);   \
-               if ((list)->prev == (el)) (list)->prev = (p); \
-       }\
-} while (0)
+     ** NOTE! The following LGPL license applies to the tevent
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
 
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
 
-/*
-   add to the end of a list.
-*/
-#define DLIST_ADD_END(list, p) \
-do { \
-       if (!(list)) { \
-               DLIST_ADD(list, p); \
-       } else { \
-               DLIST_ADD_AFTER(list, p, (list)->prev); \
-       } \
-} while (0)
-
-/* promote an element to the front of a list */
-#define DLIST_PROMOTE(list, p) \
-do { \
-          DLIST_REMOVE(list, p); \
-          DLIST_ADD(list, p); \
-} while (0)
-
-/*
-   demote an element to the end of a list.
-*/
-#define DLIST_DEMOTE(list, p) \
-do { \
-       DLIST_REMOVE(list, p); \
-       DLIST_ADD_END(list, p); \
-} while (0)
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
 
-/*
-   concatenate two lists - putting all elements of the 2nd list at the
-   end of the first list.
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
-#define DLIST_CONCATENATE(list1, list2) \
-do { \
-       if (!(list1)) { \
-               (list1) = (list2); \
-       } else { \
-               (list1)->prev->next = (list2); \
-               if (list2) { \
-                       void *_tmplist = (void *)(list1)->prev; \
-                       (list1)->prev = (list2)->prev; \
-                       (list2)->prev = _tmplist; \
-               } \
-       } \
-} while (0)
-
-#endif /* _DLINKLIST_H */
 
 int ev_set_blocking(int fd, bool set);
 bool ev_set_close_on_exec(int fd);
 
-/* Defined here so we can build against older talloc versions that don't
- * have this define yet. */
-#ifndef TALLOC_FREE
-#define TALLOC_FREE(ctx) do { talloc_free(ctx); ctx=NULL; } while(0)
-#endif
+#include "tevent_dlinklist.h"