Initial import
[samba] / source / 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /* To use these macros you must have a structure containing a next and
22    prev pointer */
23
24
25 /* hook into the front of the list */
26 #define DLIST_ADD(list, p) \
27 { \
28         if (!(list)) { \
29                 (list) = (p); \
30                 (p)->next = (p)->prev = NULL; \
31         } else { \
32                 (list)->prev = (p); \
33                 (p)->next = (list); \
34                 (p)->prev = NULL; \
35                 (list) = (p); \
36         }\
37 }
38
39 /* remove an element from a list - element doesn't have to be in list. */
40 #define DLIST_REMOVE(list, p) \
41 { \
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 }
51
52 /* promote an element to the top of the list */
53 #define DLIST_PROMOTE(list, p) \
54 { \
55           DLIST_REMOVE(list, p) \
56           DLIST_ADD(list, p) \
57 }
58
59 /* hook into the end of the list - needs a tmp pointer */
60 #define DLIST_ADD_END(list, p, tmp) \
61 { \
62                 if (!(list)) { \
63                         (list) = (p); \
64                         (p)->next = (p)->prev = NULL; \
65                 } else { \
66                         for ((tmp) = (list); (tmp)->next; (tmp) = (tmp)->next) ; \
67                         (tmp)->next = (p); \
68                         (p)->next = NULL; \
69                         (p)->prev = (tmp); \
70                 } \
71 }
72
73 /* insert 'p' after the given element 'el' in a list. If el is NULL then
74    this is the same as a DLIST_ADD() */
75 #define DLIST_ADD_AFTER(list, p, el) \
76 do { \
77         if (!(list) || !(el)) { \
78                 DLIST_ADD(list, p); \
79         } else { \
80                 p->prev = el; \
81                 p->next = el->next; \
82                 el->next = p; \
83                 if (p->next) p->next->prev = p; \
84         }\
85 } while (0)
86
87 /* demote an element to the top of the list, needs a tmp pointer */
88 #define DLIST_DEMOTE(list, p, tmp) \
89 { \
90                 DLIST_REMOVE(list, p) \
91                 DLIST_ADD_END(list, p, tmp) \
92 }