atl100: Added AtlGetPerUserRegistration stub implementation.
[wine] / include / wine / list.h
1 /*
2  * Linked lists support
3  *
4  * Copyright (C) 2002 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #ifndef __WINE_SERVER_LIST_H
22 #define __WINE_SERVER_LIST_H
23
24 #include <stddef.h>
25
26 struct list
27 {
28     struct list *next;
29     struct list *prev;
30 };
31
32 /* Define a list like so:
33  *
34  *   struct gadget
35  *   {
36  *       struct list  entry;   <-- doesn't have to be the first item in the struct
37  *       int          a, b;
38  *   };
39  *
40  *   static struct list global_gadgets = LIST_INIT( global_gadgets );
41  *
42  * or
43  *
44  *   struct some_global_thing
45  *   {
46  *       struct list gadgets;
47  *   };
48  *
49  *   list_init( &some_global_thing->gadgets );
50  *
51  * Manipulate it like this:
52  *
53  *   list_add_head( &global_gadgets, &new_gadget->entry );
54  *   list_remove( &new_gadget->entry );
55  *   list_add_after( &some_random_gadget->entry, &new_gadget->entry );
56  *
57  * And to iterate over it:
58  *
59  *   struct gadget *gadget;
60  *   LIST_FOR_EACH_ENTRY( gadget, &global_gadgets, struct gadget, entry )
61  *   {
62  *       ...
63  *   }
64  *
65  */
66
67 /* add an element after the specified one */
68 static inline void list_add_after( struct list *elem, struct list *to_add )
69 {
70     to_add->next = elem->next;
71     to_add->prev = elem;
72     elem->next->prev = to_add;
73     elem->next = to_add;
74 }
75
76 /* add an element before the specified one */
77 static inline void list_add_before( struct list *elem, struct list *to_add )
78 {
79     to_add->next = elem;
80     to_add->prev = elem->prev;
81     elem->prev->next = to_add;
82     elem->prev = to_add;
83 }
84
85 /* add element at the head of the list */
86 static inline void list_add_head( struct list *list, struct list *elem )
87 {
88     list_add_after( list, elem );
89 }
90
91 /* add element at the tail of the list */
92 static inline void list_add_tail( struct list *list, struct list *elem )
93 {
94     list_add_before( list, elem );
95 }
96
97 /* remove an element from its list */
98 static inline void list_remove( struct list *elem )
99 {
100     elem->next->prev = elem->prev;
101     elem->prev->next = elem->next;
102 }
103
104 /* get the next element */
105 static inline struct list *list_next( const struct list *list, const struct list *elem )
106 {
107     struct list *ret = elem->next;
108     if (elem->next == list) ret = NULL;
109     return ret;
110 }
111
112 /* get the previous element */
113 static inline struct list *list_prev( const struct list *list, const struct list *elem )
114 {
115     struct list *ret = elem->prev;
116     if (elem->prev == list) ret = NULL;
117     return ret;
118 }
119
120 /* get the first element */
121 static inline struct list *list_head( const struct list *list )
122 {
123     return list_next( list, list );
124 }
125
126 /* get the last element */
127 static inline struct list *list_tail( const struct list *list )
128 {
129     return list_prev( list, list );
130 }
131
132 /* check if a list is empty */
133 static inline int list_empty( const struct list *list )
134 {
135     return list->next == list;
136 }
137
138 /* initialize a list */
139 static inline void list_init( struct list *list )
140 {
141     list->next = list->prev = list;
142 }
143
144 /* count the elements of a list */
145 static inline unsigned int list_count( const struct list *list )
146 {
147     unsigned count = 0;
148     const struct list *ptr;
149     for (ptr = list->next; ptr != list; ptr = ptr->next) count++;
150     return count;
151 }
152
153 /* move all elements from src to the tail of dst */
154 static inline void list_move_tail( struct list *dst, struct list *src )
155 {
156     if (list_empty(src)) return;
157
158     dst->prev->next = src->next;
159     src->next->prev = dst->prev;
160     dst->prev = src->prev;
161     src->prev->next = dst;
162     list_init(src);
163 }
164
165 /* move all elements from src to the head of dst */
166 static inline void list_move_head( struct list *dst, struct list *src )
167 {
168     if (list_empty(src)) return;
169
170     dst->next->prev = src->prev;
171     src->prev->next = dst->next;
172     dst->next = src->next;
173     src->next->prev = dst;
174     list_init(src);
175 }
176
177 /* iterate through the list */
178 #define LIST_FOR_EACH(cursor,list) \
179     for ((cursor) = (list)->next; (cursor) != (list); (cursor) = (cursor)->next)
180
181 /* iterate through the list, with safety against removal */
182 #define LIST_FOR_EACH_SAFE(cursor, cursor2, list) \
183     for ((cursor) = (list)->next, (cursor2) = (cursor)->next; \
184          (cursor) != (list); \
185          (cursor) = (cursor2), (cursor2) = (cursor)->next)
186
187 /* iterate through the list using a list entry */
188 #define LIST_FOR_EACH_ENTRY(elem, list, type, field) \
189     for ((elem) = LIST_ENTRY((list)->next, type, field); \
190          &(elem)->field != (list); \
191          (elem) = LIST_ENTRY((elem)->field.next, type, field))
192
193 /* iterate through the list using a list entry, with safety against removal */
194 #define LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, list, type, field) \
195     for ((cursor) = LIST_ENTRY((list)->next, type, field), \
196          (cursor2) = LIST_ENTRY((cursor)->field.next, type, field); \
197          &(cursor)->field != (list); \
198          (cursor) = (cursor2), \
199          (cursor2) = LIST_ENTRY((cursor)->field.next, type, field))
200
201 /* iterate through the list in reverse order */
202 #define LIST_FOR_EACH_REV(cursor,list) \
203     for ((cursor) = (list)->prev; (cursor) != (list); (cursor) = (cursor)->prev)
204
205 /* iterate through the list in reverse order, with safety against removal */
206 #define LIST_FOR_EACH_SAFE_REV(cursor, cursor2, list) \
207     for ((cursor) = (list)->prev, (cursor2) = (cursor)->prev; \
208          (cursor) != (list); \
209          (cursor) = (cursor2), (cursor2) = (cursor)->prev)
210
211 /* iterate through the list in reverse order using a list entry */
212 #define LIST_FOR_EACH_ENTRY_REV(elem, list, type, field) \
213     for ((elem) = LIST_ENTRY((list)->prev, type, field); \
214          &(elem)->field != (list); \
215          (elem) = LIST_ENTRY((elem)->field.prev, type, field))
216
217 /* iterate through the list in reverse order using a list entry, with safety against removal */
218 #define LIST_FOR_EACH_ENTRY_SAFE_REV(cursor, cursor2, list, type, field) \
219     for ((cursor) = LIST_ENTRY((list)->prev, type, field), \
220          (cursor2) = LIST_ENTRY((cursor)->field.prev, type, field); \
221          &(cursor)->field != (list); \
222          (cursor) = (cursor2), \
223          (cursor2) = LIST_ENTRY((cursor)->field.prev, type, field))
224
225 /* macros for statically initialized lists */
226 #undef LIST_INIT
227 #define LIST_INIT(list)  { &(list), &(list) }
228
229 /* get pointer to object containing list element */
230 #undef LIST_ENTRY
231 #define LIST_ENTRY(elem, type, field) \
232     ((type *)((char *)(elem) - offsetof(type, field)))
233
234 #endif  /* __WINE_SERVER_LIST_H */