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 | /*
|
---|
22 | * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
23 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
24 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
25 | * a choice of LGPL license versions is made available with the language indicating
|
---|
26 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
27 | * of the LGPL is applied is otherwise unspecified.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef __WINE_SERVER_LIST_H
|
---|
31 | #define __WINE_SERVER_LIST_H
|
---|
32 |
|
---|
33 | #ifdef inline
|
---|
34 | #undef inline
|
---|
35 | #endif
|
---|
36 | #define inline __inline
|
---|
37 |
|
---|
38 | struct list
|
---|
39 | {
|
---|
40 | struct list *next;
|
---|
41 | struct list *prev;
|
---|
42 | };
|
---|
43 |
|
---|
44 | /* Define a list like so:
|
---|
45 | *
|
---|
46 | * struct gadget
|
---|
47 | * {
|
---|
48 | * struct list entry; <-- doesn't have to be the first item in the struct
|
---|
49 | * int a, b;
|
---|
50 | * };
|
---|
51 | *
|
---|
52 | * static struct list global_gadgets = LIST_INIT( global_gadgets );
|
---|
53 | *
|
---|
54 | * or
|
---|
55 | *
|
---|
56 | * struct some_global_thing
|
---|
57 | * {
|
---|
58 | * struct list gadgets;
|
---|
59 | * };
|
---|
60 | *
|
---|
61 | * list_init( &some_global_thing->gadgets );
|
---|
62 | *
|
---|
63 | * Manipulate it like this:
|
---|
64 | *
|
---|
65 | * list_add_head( &global_gadgets, &new_gadget->entry );
|
---|
66 | * list_remove( &new_gadget->entry );
|
---|
67 | * list_add_after( &some_random_gadget->entry, &new_gadget->entry );
|
---|
68 | *
|
---|
69 | * And to iterate over it:
|
---|
70 | *
|
---|
71 | * struct gadget *gadget;
|
---|
72 | * LIST_FOR_EACH_ENTRY( gadget, &global_gadgets, struct gadget, entry )
|
---|
73 | * {
|
---|
74 | * ...
|
---|
75 | * }
|
---|
76 | *
|
---|
77 | */
|
---|
78 |
|
---|
79 | /* add an element after the specified one */
|
---|
80 | static inline void list_add_after( struct list *elem, struct list *to_add )
|
---|
81 | {
|
---|
82 | to_add->next = elem->next;
|
---|
83 | to_add->prev = elem;
|
---|
84 | elem->next->prev = to_add;
|
---|
85 | elem->next = to_add;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /* add an element before the specified one */
|
---|
89 | static inline void list_add_before( struct list *elem, struct list *to_add )
|
---|
90 | {
|
---|
91 | to_add->next = elem;
|
---|
92 | to_add->prev = elem->prev;
|
---|
93 | elem->prev->next = to_add;
|
---|
94 | elem->prev = to_add;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* add element at the head of the list */
|
---|
98 | static inline void list_add_head( struct list *list, struct list *elem )
|
---|
99 | {
|
---|
100 | list_add_after( list, elem );
|
---|
101 | }
|
---|
102 |
|
---|
103 | /* add element at the tail of the list */
|
---|
104 | static inline void list_add_tail( struct list *list, struct list *elem )
|
---|
105 | {
|
---|
106 | list_add_before( list, elem );
|
---|
107 | }
|
---|
108 |
|
---|
109 | /* remove an element from its list */
|
---|
110 | static inline void list_remove( struct list *elem )
|
---|
111 | {
|
---|
112 | elem->next->prev = elem->prev;
|
---|
113 | elem->prev->next = elem->next;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /* get the next element */
|
---|
117 | static inline struct list *list_next( const struct list *list, const struct list *elem )
|
---|
118 | {
|
---|
119 | struct list *ret = elem->next;
|
---|
120 | if (elem->next == list) ret = NULL;
|
---|
121 | return ret;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* get the previous element */
|
---|
125 | static inline struct list *list_prev( const struct list *list, const struct list *elem )
|
---|
126 | {
|
---|
127 | struct list *ret = elem->prev;
|
---|
128 | if (elem->prev == list) ret = NULL;
|
---|
129 | return ret;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* get the first element */
|
---|
133 | static inline struct list *list_head( const struct list *list )
|
---|
134 | {
|
---|
135 | return list_next( list, list );
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* get the last element */
|
---|
139 | static inline struct list *list_tail( const struct list *list )
|
---|
140 | {
|
---|
141 | return list_prev( list, list );
|
---|
142 | }
|
---|
143 |
|
---|
144 | /* check if a list is empty */
|
---|
145 | static inline int list_empty( const struct list *list )
|
---|
146 | {
|
---|
147 | return list->next == list;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /* initialize a list */
|
---|
151 | static inline void list_init( struct list *list )
|
---|
152 | {
|
---|
153 | list->next = list->prev = list;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* count the elements of a list */
|
---|
157 | static inline unsigned int list_count( const struct list *list )
|
---|
158 | {
|
---|
159 | unsigned count = 0;
|
---|
160 | const struct list *ptr;
|
---|
161 | for (ptr = list->next; ptr != list; ptr = ptr->next) count++;
|
---|
162 | return count;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* move all elements from src to the tail of dst */
|
---|
166 | static inline void list_move_tail( struct list *dst, struct list *src )
|
---|
167 | {
|
---|
168 | if (list_empty(src)) return;
|
---|
169 |
|
---|
170 | dst->prev->next = src->next;
|
---|
171 | src->next->prev = dst->prev;
|
---|
172 | dst->prev = src->prev;
|
---|
173 | src->prev->next = dst;
|
---|
174 | list_init(src);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /* move all elements from src to the head of dst */
|
---|
178 | static inline void list_move_head( struct list *dst, struct list *src )
|
---|
179 | {
|
---|
180 | if (list_empty(src)) return;
|
---|
181 |
|
---|
182 | dst->next->prev = src->prev;
|
---|
183 | src->prev->next = dst->next;
|
---|
184 | dst->next = src->next;
|
---|
185 | src->next->prev = dst;
|
---|
186 | list_init(src);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /* iterate through the list */
|
---|
190 | #define LIST_FOR_EACH(cursor,list) \
|
---|
191 | for ((cursor) = (list)->next; (cursor) != (list); (cursor) = (cursor)->next)
|
---|
192 |
|
---|
193 | /* iterate through the list, with safety against removal */
|
---|
194 | #define LIST_FOR_EACH_SAFE(cursor, cursor2, list) \
|
---|
195 | for ((cursor) = (list)->next, (cursor2) = (cursor)->next; \
|
---|
196 | (cursor) != (list); \
|
---|
197 | (cursor) = (cursor2), (cursor2) = (cursor)->next)
|
---|
198 |
|
---|
199 | /* iterate through the list using a list entry */
|
---|
200 | #define LIST_FOR_EACH_ENTRY(elem, list, type, field) \
|
---|
201 | for ((elem) = LIST_ENTRY((list)->next, type, field); \
|
---|
202 | &(elem)->field != (list); \
|
---|
203 | (elem) = LIST_ENTRY((elem)->field.next, type, field))
|
---|
204 |
|
---|
205 | /* iterate through the list using a list entry, with safety against removal */
|
---|
206 | #define LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, list, type, field) \
|
---|
207 | for ((cursor) = LIST_ENTRY((list)->next, type, field), \
|
---|
208 | (cursor2) = LIST_ENTRY((cursor)->field.next, type, field); \
|
---|
209 | &(cursor)->field != (list); \
|
---|
210 | (cursor) = (cursor2), \
|
---|
211 | (cursor2) = LIST_ENTRY((cursor)->field.next, type, field))
|
---|
212 |
|
---|
213 | /* iterate through the list in reverse order */
|
---|
214 | #define LIST_FOR_EACH_REV(cursor,list) \
|
---|
215 | for ((cursor) = (list)->prev; (cursor) != (list); (cursor) = (cursor)->prev)
|
---|
216 |
|
---|
217 | /* iterate through the list in reverse order, with safety against removal */
|
---|
218 | #define LIST_FOR_EACH_SAFE_REV(cursor, cursor2, list) \
|
---|
219 | for ((cursor) = (list)->prev, (cursor2) = (cursor)->prev; \
|
---|
220 | (cursor) != (list); \
|
---|
221 | (cursor) = (cursor2), (cursor2) = (cursor)->prev)
|
---|
222 |
|
---|
223 | /* iterate through the list in reverse order using a list entry */
|
---|
224 | #define LIST_FOR_EACH_ENTRY_REV(elem, list, type, field) \
|
---|
225 | for ((elem) = LIST_ENTRY((list)->prev, type, field); \
|
---|
226 | &(elem)->field != (list); \
|
---|
227 | (elem) = LIST_ENTRY((elem)->field.prev, type, field))
|
---|
228 |
|
---|
229 | /* iterate through the list in reverse order using a list entry, with safety against removal */
|
---|
230 | #define LIST_FOR_EACH_ENTRY_SAFE_REV(cursor, cursor2, list, type, field) \
|
---|
231 | for ((cursor) = LIST_ENTRY((list)->prev, type, field), \
|
---|
232 | (cursor2) = LIST_ENTRY((cursor)->field.prev, type, field); \
|
---|
233 | &(cursor)->field != (list); \
|
---|
234 | (cursor) = (cursor2), \
|
---|
235 | (cursor2) = LIST_ENTRY((cursor)->field.prev, type, field))
|
---|
236 |
|
---|
237 | /* macros for statically initialized lists */
|
---|
238 | #undef LIST_INIT
|
---|
239 | #define LIST_INIT(list) { &(list), &(list) }
|
---|
240 |
|
---|
241 | /* get pointer to object containing list element */
|
---|
242 | #undef LIST_ENTRY
|
---|
243 | #define LIST_ENTRY(elem, type, field) \
|
---|
244 | ((type *)((char *)(elem) - (uintptr_t)(&((type *)0)->field)))
|
---|
245 |
|
---|
246 | #endif /* __WINE_SERVER_LIST_H */
|
---|