c1f01e616ae2ea1147e8370905f024a8f2a0b15f
[ira/wip.git] / lib / util / 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 #ifndef _DLINKLIST_H
24 #define _DLINKLIST_H
25
26
27 /* hook into the front of the list */
28 #define DLIST_ADD(list, p) \
29 do { \
30         if (!(list)) { \
31                 (list) = (p); \
32                 (p)->next = (p)->prev = NULL; \
33         } else { \
34                 (list)->prev = (p); \
35                 (p)->next = (list); \
36                 (p)->prev = NULL; \
37                 (list) = (p); \
38         }\
39 } while (0)
40
41 /* remove an element from a list - element doesn't have to be in list. */
42 #define DLIST_REMOVE(list, p) \
43 do { \
44         if ((p) == (list)) { \
45                 (list) = (p)->next; \
46                 if (list) (list)->prev = NULL; \
47         } else { \
48                 if ((p)->prev) (p)->prev->next = (p)->next; \
49                 if ((p)->next) (p)->next->prev = (p)->prev; \
50         } \
51         if ((p) != (list)) (p)->next = (p)->prev = NULL; \
52 } while (0)
53
54 #define DLIST_HEAD(p, result_head) \
55 do { \
56         (result_head) = (p); \
57         while (DLIST_PREV(result_head)) (result_head) = (result_head)->prev; \
58 } while(0)
59
60 /* return the last element in the list */
61 #define DLIST_TAIL(list, result_tail) \
62 do { \
63         (result_tail) = (list); \
64         while ((result_tail) && (result_tail)->next) (result_tail) = (result_tail)->next; \
65 } while (0);
66
67 /* return the previous element in the list. */
68 #define DLIST_PREV(p) ((p)?(p)->prev:NULL)
69
70 /* promote an element to the top of the list */
71 #define DLIST_PROMOTE(list, p) \
72 do { \
73           DLIST_REMOVE(list, p); \
74           DLIST_ADD(list, p); \
75 } while (0)
76
77 /* hook into the end of the list - needs the entry type */
78 #define DLIST_ADD_END(list, p, type) \
79 do { \
80                 if (!(list)) { \
81                         (list) = (p); \
82                         (p)->next = (p)->prev = NULL; \
83                 } else { \
84                         type tmp; \
85                         for (tmp = (list); tmp->next; tmp = tmp->next) ; \
86                         tmp->next = (p); \
87                         (p)->next = NULL; \
88                         (p)->prev = tmp; \
89                 } \
90 } while (0)
91
92 /* insert 'p' after the given element 'el' in a list. If el is NULL then
93    this is the same as a DLIST_ADD() */
94 #define DLIST_ADD_AFTER(list, p, el) \
95 do { \
96         if (!(list) || !(el)) { \
97                 DLIST_ADD(list, p); \
98         } else { \
99                 p->prev = el; \
100                 p->next = el->next; \
101                 el->next = p; \
102                 if (p->next) p->next->prev = p; \
103         }\
104 } while (0)
105
106 /* demote an element to the end of the list, needs the entry type */
107 #define DLIST_DEMOTE(list, p, type) \
108 do { \
109                 DLIST_REMOVE(list, p); \
110                 DLIST_ADD_END(list, p, type); \
111 } while (0)
112
113 /* concatenate two lists - putting all elements of the 2nd list at the
114    end of the first list */
115 #define DLIST_CONCATENATE(list1, list2, type) \
116 do { \
117                 if (!(list1)) { \
118                         (list1) = (list2); \
119                 } else { \
120                         type tmp; \
121                         for (tmp = (list1); tmp->next; tmp = tmp->next) ; \
122                         tmp->next = (list2); \
123                         if (list2) { \
124                                 (list2)->prev = tmp;    \
125                         } \
126                 } \
127 } while (0)
128
129 #endif /* _DLINKLIST_H */