4 * Copyright (C) 2002 Alexandre Julliard
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.
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.
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
21 #ifndef __WINE_SERVER_LIST_H
22 #define __WINE_SERVER_LIST_H
30 /* Define a list like so:
34 * struct list entry; <-- doesn't have to be the first item in the struct
38 * static struct list global_gadgets = LIST_INIT( global_gadgets );
42 * struct some_global_thing
44 * struct list gadgets;
47 * list_init( &some_global_thing->gadgets );
49 * Manipulate it like this:
51 * list_add_head( &global_gadgets, &new_gadget->entry );
52 * list_remove( &new_gadget->entry );
53 * list_add_after( &some_random_gadget->entry, &new_gadget->entry );
55 * And to iterate over it:
57 * struct gadget *gadget;
58 * LIST_FOR_EACH_ENTRY( gadget, &global_gadgets, struct gadget, entry )
65 /* add an element after the specified one */
66 inline static void list_add_after( struct list *elem, struct list *to_add )
68 to_add->next = elem->next;
70 elem->next->prev = to_add;
74 /* add an element before the specified one */
75 inline static void list_add_before( struct list *elem, struct list *to_add )
78 to_add->prev = elem->prev;
79 elem->prev->next = to_add;
83 /* add element at the head of the list */
84 inline static void list_add_head( struct list *list, struct list *elem )
86 list_add_after( list, elem );
89 /* add element at the tail of the list */
90 inline static void list_add_tail( struct list *list, struct list *elem )
92 list_add_before( list, elem );
95 /* remove an element from its list */
96 inline static void list_remove( struct list *elem )
98 elem->next->prev = elem->prev;
99 elem->prev->next = elem->next;
102 /* get the next element */
103 inline static struct list *list_next( struct list *list, struct list *elem )
105 struct list *ret = elem->next;
106 if (elem->next == list) ret = NULL;
110 /* get the previous element */
111 inline static struct list *list_prev( struct list *list, struct list *elem )
113 struct list *ret = elem->prev;
114 if (elem->prev == list) ret = NULL;
118 /* get the first element */
119 inline static struct list *list_head( struct list *list )
121 return list_next( list, list );
124 /* get the last element */
125 inline static struct list *list_tail( struct list *list )
127 return list_prev( list, list );
130 /* check if a list is empty */
131 inline static int list_empty( struct list *list )
133 return list->next == list;
136 /* initialize a list */
137 inline static void list_init( struct list *list )
139 list->next = list->prev = list;
142 /* move all elements from src to the tail of dst */
143 inline static void list_move_tail( struct list *dst, struct list *src )
145 if (list_empty(src)) return;
147 dst->prev->next = src->next;
148 src->next->prev = dst->prev;
149 dst->prev = src->prev;
150 src->prev->next = dst;
154 /* move all elements from src to the head of dst */
155 inline static void list_move_head( struct list *dst, struct list *src )
157 if (list_empty(src)) return;
159 dst->next->prev = src->prev;
160 src->prev->next = dst->next;
161 dst->next = src->next;
162 src->next->prev = dst;
166 /* iterate through the list */
167 #define LIST_FOR_EACH(cursor,list) \
168 for ((cursor) = (list)->next; (cursor) != (list); (cursor) = (cursor)->next)
170 /* iterate through the list, with safety against removal */
171 #define LIST_FOR_EACH_SAFE(cursor, cursor2, list) \
172 for ((cursor) = (list)->next, (cursor2) = (cursor)->next; \
173 (cursor) != (list); \
174 (cursor) = (cursor2), (cursor2) = (cursor)->next)
176 /* iterate through the list using a list entry */
177 #define LIST_FOR_EACH_ENTRY(elem, list, type, field) \
178 for ((elem) = LIST_ENTRY((list)->next, type, field); \
179 &(elem)->field != (list); \
180 (elem) = LIST_ENTRY((elem)->field.next, type, field))
182 /* iterate through the list using a list entry, with safety against removal */
183 #define LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, list, type, field) \
184 for ((cursor) = LIST_ENTRY((list)->next, type, field), \
185 (cursor2) = LIST_ENTRY((cursor)->field.next, type, field); \
186 &(cursor)->field != (list); \
187 (cursor) = (cursor2), \
188 (cursor2) = LIST_ENTRY((cursor)->field.next, type, field))
190 /* macros for statically initialized lists */
191 #define LIST_INIT(list) { &(list), &(list) }
193 /* get pointer to object containing list element */
194 #define LIST_ENTRY(elem, type, field) \
195 ((type *)((char *)(elem) - (unsigned int)(&((type *)0)->field)))
197 #endif /* __WINE_SERVER_LIST_H */