r23792: convert Samba4 to GPLv3
[amitay/samba.git] / source4 / lib / ldb / include / dlinklist.h
1 /* 
2    Unix SMB/CIFS implementation.
3    some simple double linked list macros
4    Copyright (C) Andrew Tridgell 1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* To use these macros you must have a structure containing a next and
21    prev pointer */
22
23
24 /* hook into the front of the list */
25 #define DLIST_ADD(list, p) \
26 do { \
27         if (!(list)) { \
28                 (list) = (p); \
29                 (p)->next = (p)->prev = NULL; \
30         } else { \
31                 (list)->prev = (p); \
32                 (p)->next = (list); \
33                 (p)->prev = NULL; \
34                 (list) = (p); \
35         }\
36 } while (0)
37
38 /* remove an element from a list - element doesn't have to be in list. */
39 #ifndef DLIST_REMOVE
40 #define DLIST_REMOVE(list, p) \
41 do { \
42         if ((p) == (list)) { \
43                 (list) = (p)->next; \
44                 if (list) (list)->prev = NULL; \
45         } else { \
46                 if ((p)->prev) (p)->prev->next = (p)->next; \
47                 if ((p)->next) (p)->next->prev = (p)->prev; \
48         } \
49         if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
50 } while (0)
51 #endif
52
53 /* promote an element to the top of the list */
54 #define DLIST_PROMOTE(list, p) \
55 do { \
56           DLIST_REMOVE(list, p); \
57           DLIST_ADD(list, p); \
58 } while (0)
59
60 /* hook into the end of the list - needs a tmp pointer */
61 #define DLIST_ADD_END(list, p, type) \
62 do { \
63                 if (!(list)) { \
64                         (list) = (p); \
65                         (p)->next = (p)->prev = NULL; \
66                 } else { \
67                         type tmp; \
68                         for (tmp = (list); tmp->next; tmp = tmp->next) ; \
69                         tmp->next = (p); \
70                         (p)->next = NULL; \
71                         (p)->prev = tmp; \
72                 } \
73 } while (0)
74
75 /* insert 'p' after the given element 'el' in a list. If el is NULL then
76    this is the same as a DLIST_ADD() */
77 #define DLIST_ADD_AFTER(list, p, el) \
78 do { \
79         if (!(list) || !(el)) { \
80                 DLIST_ADD(list, p); \
81         } else { \
82                 p->prev = el; \
83                 p->next = el->next; \
84                 el->next = p; \
85                 if (p->next) p->next->prev = p; \
86         }\
87 } while (0)
88
89 /* demote an element to the end of the list, needs a tmp pointer */
90 #define DLIST_DEMOTE(list, p, tmp) \
91 do { \
92                 DLIST_REMOVE(list, p); \
93                 DLIST_ADD_END(list, p, tmp); \
94 } while (0)
95
96 /* concatenate two lists - putting all elements of the 2nd list at the
97    end of the first list */
98 #define DLIST_CONCATENATE(list1, list2, type) \
99 do { \
100                 if (!(list1)) { \
101                         (list1) = (list2); \
102                 } else { \
103                         type tmp; \
104                         for (tmp = (list1); tmp->next; tmp = tmp->next) ; \
105                         tmp->next = (list2); \
106                         if (list2) { \
107                                 (list2)->prev = tmp;    \
108                         } \
109                 } \
110 } while (0)