xref: /OK3568_Linux_fs/external/xserver/include/list.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright © 2010 Intel Corporation
3*4882a593Smuzhiyun  * Copyright © 2010 Francisco Jerez <currojerez@riseup.net>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
6*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
7*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
8*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
10*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the next
13*4882a593Smuzhiyun  * paragraph) shall be included in all copies or substantial portions of the
14*4882a593Smuzhiyun  * Software.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19*4882a593Smuzhiyun  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*4882a593Smuzhiyun  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21*4882a593Smuzhiyun  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22*4882a593Smuzhiyun  * IN THE SOFTWARE.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #ifndef _XORG_LIST_H_
27*4882a593Smuzhiyun #define _XORG_LIST_H_
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include <stddef.h> /* offsetof() */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /**
32*4882a593Smuzhiyun  * @file Classic doubly-link circular list implementation.
33*4882a593Smuzhiyun  * For real usage examples of the linked list, see the file test/list.c
34*4882a593Smuzhiyun  *
35*4882a593Smuzhiyun  * Example:
36*4882a593Smuzhiyun  * We need to keep a list of struct foo in the parent struct bar, i.e. what
37*4882a593Smuzhiyun  * we want is something like this.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  *     struct bar {
40*4882a593Smuzhiyun  *          ...
41*4882a593Smuzhiyun  *          struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
42*4882a593Smuzhiyun  *          ...
43*4882a593Smuzhiyun  *     }
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * We need one list head in bar and a list element in all list_of_foos (both are of
46*4882a593Smuzhiyun  * data type 'struct xorg_list').
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  *     struct bar {
49*4882a593Smuzhiyun  *          ...
50*4882a593Smuzhiyun  *          struct xorg_list list_of_foos;
51*4882a593Smuzhiyun  *          ...
52*4882a593Smuzhiyun  *     }
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  *     struct foo {
55*4882a593Smuzhiyun  *          ...
56*4882a593Smuzhiyun  *          struct xorg_list entry;
57*4882a593Smuzhiyun  *          ...
58*4882a593Smuzhiyun  *     }
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * Now we initialize the list head:
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  *     struct bar bar;
63*4882a593Smuzhiyun  *     ...
64*4882a593Smuzhiyun  *     xorg_list_init(&bar.list_of_foos);
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  * Then we create the first element and add it to this list:
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  *     struct foo *foo = malloc(...);
69*4882a593Smuzhiyun  *     ....
70*4882a593Smuzhiyun  *     xorg_list_add(&foo->entry, &bar.list_of_foos);
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * Repeat the above for each element you want to add to the list. Deleting
73*4882a593Smuzhiyun  * works with the element itself.
74*4882a593Smuzhiyun  *      xorg_list_del(&foo->entry);
75*4882a593Smuzhiyun  *      free(foo);
76*4882a593Smuzhiyun  *
77*4882a593Smuzhiyun  * Note: calling xorg_list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
78*4882a593Smuzhiyun  * list again.
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  * Looping through the list requires a 'struct foo' as iterator and the
81*4882a593Smuzhiyun  * name of the field the subnodes use.
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * struct foo *iterator;
84*4882a593Smuzhiyun  * xorg_list_for_each_entry(iterator, &bar.list_of_foos, entry) {
85*4882a593Smuzhiyun  *      if (iterator->something == ...)
86*4882a593Smuzhiyun  *             ...
87*4882a593Smuzhiyun  * }
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * Note: You must not call xorg_list_del() on the iterator if you continue the
90*4882a593Smuzhiyun  * loop. You need to run the safe for-each loop instead:
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * struct foo *iterator, *next;
93*4882a593Smuzhiyun  * xorg_list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) {
94*4882a593Smuzhiyun  *      if (...)
95*4882a593Smuzhiyun  *              xorg_list_del(&iterator->entry);
96*4882a593Smuzhiyun  * }
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /**
101*4882a593Smuzhiyun  * The linkage struct for list nodes. This struct must be part of your
102*4882a593Smuzhiyun  * to-be-linked struct. struct xorg_list is required for both the head of the
103*4882a593Smuzhiyun  * list and for each list node.
104*4882a593Smuzhiyun  *
105*4882a593Smuzhiyun  * Position and name of the struct xorg_list field is irrelevant.
106*4882a593Smuzhiyun  * There are no requirements that elements of a list are of the same type.
107*4882a593Smuzhiyun  * There are no requirements for a list head, any struct xorg_list can be a list
108*4882a593Smuzhiyun  * head.
109*4882a593Smuzhiyun  */
110*4882a593Smuzhiyun struct xorg_list {
111*4882a593Smuzhiyun     struct xorg_list *next, *prev;
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun  * Initialize the list as an empty list.
116*4882a593Smuzhiyun  *
117*4882a593Smuzhiyun  * Example:
118*4882a593Smuzhiyun  * xorg_list_init(&bar->list_of_foos);
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * @param list The list to initialize
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun static inline void
xorg_list_init(struct xorg_list * list)123*4882a593Smuzhiyun xorg_list_init(struct xorg_list *list)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun     list->next = list->prev = list;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun static inline void
__xorg_list_add(struct xorg_list * entry,struct xorg_list * prev,struct xorg_list * next)129*4882a593Smuzhiyun __xorg_list_add(struct xorg_list *entry,
130*4882a593Smuzhiyun                 struct xorg_list *prev, struct xorg_list *next)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun     next->prev = entry;
133*4882a593Smuzhiyun     entry->next = next;
134*4882a593Smuzhiyun     entry->prev = prev;
135*4882a593Smuzhiyun     prev->next = entry;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /**
139*4882a593Smuzhiyun  * Insert a new element after the given list head. The new element does not
140*4882a593Smuzhiyun  * need to be initialised as empty list.
141*4882a593Smuzhiyun  * The list changes from:
142*4882a593Smuzhiyun  *      head → some element → ...
143*4882a593Smuzhiyun  * to
144*4882a593Smuzhiyun  *      head → new element → older element → ...
145*4882a593Smuzhiyun  *
146*4882a593Smuzhiyun  * Example:
147*4882a593Smuzhiyun  * struct foo *newfoo = malloc(...);
148*4882a593Smuzhiyun  * xorg_list_add(&newfoo->entry, &bar->list_of_foos);
149*4882a593Smuzhiyun  *
150*4882a593Smuzhiyun  * @param entry The new element to prepend to the list.
151*4882a593Smuzhiyun  * @param head The existing list.
152*4882a593Smuzhiyun  */
153*4882a593Smuzhiyun static inline void
xorg_list_add(struct xorg_list * entry,struct xorg_list * head)154*4882a593Smuzhiyun xorg_list_add(struct xorg_list *entry, struct xorg_list *head)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun     __xorg_list_add(entry, head, head->next);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /**
160*4882a593Smuzhiyun  * Append a new element to the end of the list given with this list head.
161*4882a593Smuzhiyun  *
162*4882a593Smuzhiyun  * The list changes from:
163*4882a593Smuzhiyun  *      head → some element → ... → lastelement
164*4882a593Smuzhiyun  * to
165*4882a593Smuzhiyun  *      head → some element → ... → lastelement → new element
166*4882a593Smuzhiyun  *
167*4882a593Smuzhiyun  * Example:
168*4882a593Smuzhiyun  * struct foo *newfoo = malloc(...);
169*4882a593Smuzhiyun  * xorg_list_append(&newfoo->entry, &bar->list_of_foos);
170*4882a593Smuzhiyun  *
171*4882a593Smuzhiyun  * @param entry The new element to prepend to the list.
172*4882a593Smuzhiyun  * @param head The existing list.
173*4882a593Smuzhiyun  */
174*4882a593Smuzhiyun static inline void
xorg_list_append(struct xorg_list * entry,struct xorg_list * head)175*4882a593Smuzhiyun xorg_list_append(struct xorg_list *entry, struct xorg_list *head)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun     __xorg_list_add(entry, head->prev, head);
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun static inline void
__xorg_list_del(struct xorg_list * prev,struct xorg_list * next)181*4882a593Smuzhiyun __xorg_list_del(struct xorg_list *prev, struct xorg_list *next)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun     next->prev = prev;
184*4882a593Smuzhiyun     prev->next = next;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /**
188*4882a593Smuzhiyun  * Remove the element from the list it is in. Using this function will reset
189*4882a593Smuzhiyun  * the pointers to/from this element so it is removed from the list. It does
190*4882a593Smuzhiyun  * NOT free the element itself or manipulate it otherwise.
191*4882a593Smuzhiyun  *
192*4882a593Smuzhiyun  * Using xorg_list_del on a pure list head (like in the example at the top of
193*4882a593Smuzhiyun  * this file) will NOT remove the first element from
194*4882a593Smuzhiyun  * the list but rather reset the list as empty list.
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * Example:
197*4882a593Smuzhiyun  * xorg_list_del(&foo->entry);
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  * @param entry The element to remove.
200*4882a593Smuzhiyun  */
201*4882a593Smuzhiyun static inline void
xorg_list_del(struct xorg_list * entry)202*4882a593Smuzhiyun xorg_list_del(struct xorg_list *entry)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun     __xorg_list_del(entry->prev, entry->next);
205*4882a593Smuzhiyun     xorg_list_init(entry);
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun /**
209*4882a593Smuzhiyun  * Check if the list is empty.
210*4882a593Smuzhiyun  *
211*4882a593Smuzhiyun  * Example:
212*4882a593Smuzhiyun  * xorg_list_is_empty(&bar->list_of_foos);
213*4882a593Smuzhiyun  *
214*4882a593Smuzhiyun  * @return True if the list is empty or False if the list contains one or more
215*4882a593Smuzhiyun  * elements.
216*4882a593Smuzhiyun  */
217*4882a593Smuzhiyun static inline int
xorg_list_is_empty(struct xorg_list * head)218*4882a593Smuzhiyun xorg_list_is_empty(struct xorg_list *head)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun     return head->next == head;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun /**
224*4882a593Smuzhiyun  * Returns a pointer to the container of this list element.
225*4882a593Smuzhiyun  *
226*4882a593Smuzhiyun  * Example:
227*4882a593Smuzhiyun  * struct foo* f;
228*4882a593Smuzhiyun  * f = container_of(&foo->entry, struct foo, entry);
229*4882a593Smuzhiyun  * assert(f == foo);
230*4882a593Smuzhiyun  *
231*4882a593Smuzhiyun  * @param ptr Pointer to the struct xorg_list.
232*4882a593Smuzhiyun  * @param type Data type of the list element.
233*4882a593Smuzhiyun  * @param member Member name of the struct xorg_list field in the list element.
234*4882a593Smuzhiyun  * @return A pointer to the data struct containing the list head.
235*4882a593Smuzhiyun  */
236*4882a593Smuzhiyun #ifndef container_of
237*4882a593Smuzhiyun #define container_of(ptr, type, member) \
238*4882a593Smuzhiyun     (type *)((char *)(ptr) - offsetof(type, member))
239*4882a593Smuzhiyun #endif
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun /**
242*4882a593Smuzhiyun  * Alias of container_of
243*4882a593Smuzhiyun  */
244*4882a593Smuzhiyun #define xorg_list_entry(ptr, type, member) \
245*4882a593Smuzhiyun     container_of(ptr, type, member)
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun /**
248*4882a593Smuzhiyun  * Retrieve the first list entry for the given list pointer.
249*4882a593Smuzhiyun  *
250*4882a593Smuzhiyun  * Example:
251*4882a593Smuzhiyun  * struct foo *first;
252*4882a593Smuzhiyun  * first = xorg_list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
253*4882a593Smuzhiyun  *
254*4882a593Smuzhiyun  * @param ptr The list head
255*4882a593Smuzhiyun  * @param type Data type of the list element to retrieve
256*4882a593Smuzhiyun  * @param member Member name of the struct xorg_list field in the list element.
257*4882a593Smuzhiyun  * @return A pointer to the first list element.
258*4882a593Smuzhiyun  */
259*4882a593Smuzhiyun #define xorg_list_first_entry(ptr, type, member) \
260*4882a593Smuzhiyun     xorg_list_entry((ptr)->next, type, member)
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /**
263*4882a593Smuzhiyun  * Retrieve the last list entry for the given listpointer.
264*4882a593Smuzhiyun  *
265*4882a593Smuzhiyun  * Example:
266*4882a593Smuzhiyun  * struct foo *first;
267*4882a593Smuzhiyun  * first = xorg_list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
268*4882a593Smuzhiyun  *
269*4882a593Smuzhiyun  * @param ptr The list head
270*4882a593Smuzhiyun  * @param type Data type of the list element to retrieve
271*4882a593Smuzhiyun  * @param member Member name of the struct xorg_list field in the list element.
272*4882a593Smuzhiyun  * @return A pointer to the last list element.
273*4882a593Smuzhiyun  */
274*4882a593Smuzhiyun #define xorg_list_last_entry(ptr, type, member) \
275*4882a593Smuzhiyun     xorg_list_entry((ptr)->prev, type, member)
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun #ifdef HAVE_TYPEOF
278*4882a593Smuzhiyun #define __container_of(ptr, sample, member)			\
279*4882a593Smuzhiyun     container_of(ptr, typeof(*sample), member)
280*4882a593Smuzhiyun #else
281*4882a593Smuzhiyun /* This implementation of __container_of has undefined behavior according
282*4882a593Smuzhiyun  * to the C standard, but it works in many cases.  If your compiler doesn't
283*4882a593Smuzhiyun  * support typeof() and fails with this implementation, please try a newer
284*4882a593Smuzhiyun  * compiler.
285*4882a593Smuzhiyun  */
286*4882a593Smuzhiyun #define __container_of(ptr, sample, member)                            \
287*4882a593Smuzhiyun     (void *)((char *)(ptr)                                             \
288*4882a593Smuzhiyun             - ((char *)&(sample)->member - (char *)(sample)))
289*4882a593Smuzhiyun #endif
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun /**
292*4882a593Smuzhiyun  * Loop through the list given by head and set pos to struct in the list.
293*4882a593Smuzhiyun  *
294*4882a593Smuzhiyun  * Example:
295*4882a593Smuzhiyun  * struct foo *iterator;
296*4882a593Smuzhiyun  * xorg_list_for_each_entry(iterator, &bar->list_of_foos, entry) {
297*4882a593Smuzhiyun  *      [modify iterator]
298*4882a593Smuzhiyun  * }
299*4882a593Smuzhiyun  *
300*4882a593Smuzhiyun  * This macro is not safe for node deletion. Use xorg_list_for_each_entry_safe
301*4882a593Smuzhiyun  * instead.
302*4882a593Smuzhiyun  *
303*4882a593Smuzhiyun  * @param pos Iterator variable of the type of the list elements.
304*4882a593Smuzhiyun  * @param head List head
305*4882a593Smuzhiyun  * @param member Member name of the struct xorg_list in the list elements.
306*4882a593Smuzhiyun  *
307*4882a593Smuzhiyun  */
308*4882a593Smuzhiyun #define xorg_list_for_each_entry(pos, head, member)			\
309*4882a593Smuzhiyun     for (pos = NULL,                                                    \
310*4882a593Smuzhiyun          pos = __container_of((head)->next, pos, member);		\
311*4882a593Smuzhiyun 	 &pos->member != (head);					\
312*4882a593Smuzhiyun 	 pos = __container_of(pos->member.next, pos, member))
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun /**
315*4882a593Smuzhiyun  * Loop through the list, keeping a backup pointer to the element. This
316*4882a593Smuzhiyun  * macro allows for the deletion of a list element while looping through the
317*4882a593Smuzhiyun  * list.
318*4882a593Smuzhiyun  *
319*4882a593Smuzhiyun  * See xorg_list_for_each_entry for more details.
320*4882a593Smuzhiyun  */
321*4882a593Smuzhiyun #define xorg_list_for_each_entry_safe(pos, tmp, head, member)		\
322*4882a593Smuzhiyun     for (pos = NULL,                                                    \
323*4882a593Smuzhiyun          pos = __container_of((head)->next, pos, member),		\
324*4882a593Smuzhiyun 	 tmp = __container_of(pos->member.next, pos, member);		\
325*4882a593Smuzhiyun 	 &pos->member != (head);					\
326*4882a593Smuzhiyun 	 pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun /* NULL-Terminated List Interface
329*4882a593Smuzhiyun  *
330*4882a593Smuzhiyun  * The interface below does _not_ use the struct xorg_list as described above.
331*4882a593Smuzhiyun  * It is mainly for legacy structures that cannot easily be switched to
332*4882a593Smuzhiyun  * struct xorg_list.
333*4882a593Smuzhiyun  *
334*4882a593Smuzhiyun  * This interface is for structs like
335*4882a593Smuzhiyun  *      struct foo {
336*4882a593Smuzhiyun  *          [...]
337*4882a593Smuzhiyun  *          struct foo *next;
338*4882a593Smuzhiyun  *           [...]
339*4882a593Smuzhiyun  *      };
340*4882a593Smuzhiyun  *
341*4882a593Smuzhiyun  * The position and field name of "next" are arbitrary.
342*4882a593Smuzhiyun  */
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /**
345*4882a593Smuzhiyun  * Init the element as null-terminated list.
346*4882a593Smuzhiyun  *
347*4882a593Smuzhiyun  * Example:
348*4882a593Smuzhiyun  * struct foo *list = malloc();
349*4882a593Smuzhiyun  * nt_list_init(list, next);
350*4882a593Smuzhiyun  *
351*4882a593Smuzhiyun  * @param list The list element that will be the start of the list
352*4882a593Smuzhiyun  * @param member Member name of the field pointing to next struct
353*4882a593Smuzhiyun  */
354*4882a593Smuzhiyun #define nt_list_init(_list, _member) \
355*4882a593Smuzhiyun 	(_list)->_member = NULL
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun /**
358*4882a593Smuzhiyun  * Returns the next element in the list or NULL on termination.
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  * Example:
361*4882a593Smuzhiyun  * struct foo *element = list;
362*4882a593Smuzhiyun  * while ((element = nt_list_next(element, next)) { }
363*4882a593Smuzhiyun  *
364*4882a593Smuzhiyun  * This macro is not safe for node deletion. Use nt_list_for_each_entry_safe
365*4882a593Smuzhiyun  * instead.
366*4882a593Smuzhiyun  *
367*4882a593Smuzhiyun  * @param list The list or current element.
368*4882a593Smuzhiyun  * @param member Member name of the field pointing to next struct.
369*4882a593Smuzhiyun  */
370*4882a593Smuzhiyun #define nt_list_next(_list, _member) \
371*4882a593Smuzhiyun 	(_list)->_member
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /**
374*4882a593Smuzhiyun  * Iterate through each element in the list.
375*4882a593Smuzhiyun  *
376*4882a593Smuzhiyun  * Example:
377*4882a593Smuzhiyun  * struct foo *iterator;
378*4882a593Smuzhiyun  * nt_list_for_each_entry(iterator, list, next) {
379*4882a593Smuzhiyun  *      [modify iterator]
380*4882a593Smuzhiyun  * }
381*4882a593Smuzhiyun  *
382*4882a593Smuzhiyun  * @param entry Assigned to the current list element
383*4882a593Smuzhiyun  * @param list The list to iterate through.
384*4882a593Smuzhiyun  * @param member Member name of the field pointing to next struct.
385*4882a593Smuzhiyun  */
386*4882a593Smuzhiyun #define nt_list_for_each_entry(_entry, _list, _member)			\
387*4882a593Smuzhiyun 	for (_entry = _list; _entry; _entry = (_entry)->_member)
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun /**
390*4882a593Smuzhiyun  * Iterate through each element in the list, keeping a backup pointer to the
391*4882a593Smuzhiyun  * element. This macro allows for the deletion of a list element while
392*4882a593Smuzhiyun  * looping through the list.
393*4882a593Smuzhiyun  *
394*4882a593Smuzhiyun  * See nt_list_for_each_entry for more details.
395*4882a593Smuzhiyun  *
396*4882a593Smuzhiyun  * @param entry Assigned to the current list element
397*4882a593Smuzhiyun  * @param tmp The pointer to the next element
398*4882a593Smuzhiyun  * @param list The list to iterate through.
399*4882a593Smuzhiyun  * @param member Member name of the field pointing to next struct.
400*4882a593Smuzhiyun  */
401*4882a593Smuzhiyun #define nt_list_for_each_entry_safe(_entry, _tmp, _list, _member)	\
402*4882a593Smuzhiyun 	for (_entry = _list, _tmp = (_entry) ? (_entry)->_member : NULL;\
403*4882a593Smuzhiyun 		_entry;							\
404*4882a593Smuzhiyun 		_entry = _tmp, _tmp = (_tmp) ? (_tmp)->_member: NULL)
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun /**
407*4882a593Smuzhiyun  * Append the element to the end of the list. This macro may be used to
408*4882a593Smuzhiyun  * merge two lists.
409*4882a593Smuzhiyun  *
410*4882a593Smuzhiyun  * Example:
411*4882a593Smuzhiyun  * struct foo *elem = malloc(...);
412*4882a593Smuzhiyun  * nt_list_init(elem, next)
413*4882a593Smuzhiyun  * nt_list_append(elem, list, struct foo, next);
414*4882a593Smuzhiyun  *
415*4882a593Smuzhiyun  * Resulting list order:
416*4882a593Smuzhiyun  * list_item_0 -> list_item_1 -> ... -> elem_item_0 -> elem_item_1 ...
417*4882a593Smuzhiyun  *
418*4882a593Smuzhiyun  * @param entry An entry (or list) to append to the list
419*4882a593Smuzhiyun  * @param list The list to append to. This list must be a valid list, not
420*4882a593Smuzhiyun  * NULL.
421*4882a593Smuzhiyun  * @param type The list type
422*4882a593Smuzhiyun  * @param member Member name of the field pointing to next struct
423*4882a593Smuzhiyun  */
424*4882a593Smuzhiyun #define nt_list_append(_entry, _list, _type, _member)		        \
425*4882a593Smuzhiyun     do {								\
426*4882a593Smuzhiyun 	_type *__iterator = _list;					\
427*4882a593Smuzhiyun 	while (__iterator->_member) { __iterator = __iterator->_member;}\
428*4882a593Smuzhiyun 	__iterator->_member = _entry;					\
429*4882a593Smuzhiyun     } while (0)
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun /**
432*4882a593Smuzhiyun  * Insert the element at the next position in the list. This macro may be
433*4882a593Smuzhiyun  * used to insert a list into a list.
434*4882a593Smuzhiyun  *
435*4882a593Smuzhiyun  * struct foo *elem = malloc(...);
436*4882a593Smuzhiyun  * nt_list_init(elem, next)
437*4882a593Smuzhiyun  * nt_list_insert(elem, list, struct foo, next);
438*4882a593Smuzhiyun  *
439*4882a593Smuzhiyun  * Resulting list order:
440*4882a593Smuzhiyun  * list_item_0 -> elem_item_0 -> elem_item_1 ... -> list_item_1 -> ...
441*4882a593Smuzhiyun  *
442*4882a593Smuzhiyun  * @param entry An entry (or list) to append to the list
443*4882a593Smuzhiyun  * @param list The list to insert to. This list must be a valid list, not
444*4882a593Smuzhiyun  * NULL.
445*4882a593Smuzhiyun  * @param type The list type
446*4882a593Smuzhiyun  * @param member Member name of the field pointing to next struct
447*4882a593Smuzhiyun  */
448*4882a593Smuzhiyun #define nt_list_insert(_entry, _list, _type, _member)			\
449*4882a593Smuzhiyun     do {								\
450*4882a593Smuzhiyun 	nt_list_append((_list)->_member, _entry, _type, _member);	\
451*4882a593Smuzhiyun 	(_list)->_member = _entry;					\
452*4882a593Smuzhiyun     } while (0)
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun /**
455*4882a593Smuzhiyun  * Delete the entry from the list by iterating through the list and
456*4882a593Smuzhiyun  * removing any reference from the list to the entry.
457*4882a593Smuzhiyun  *
458*4882a593Smuzhiyun  * Example:
459*4882a593Smuzhiyun  * struct foo *elem = <assign to right element>
460*4882a593Smuzhiyun  * nt_list_del(elem, list, struct foo, next);
461*4882a593Smuzhiyun  *
462*4882a593Smuzhiyun  * @param entry The entry to delete from the list. entry is always
463*4882a593Smuzhiyun  * re-initialized as a null-terminated list.
464*4882a593Smuzhiyun  * @param list The list containing the entry, set to the new list without
465*4882a593Smuzhiyun  * the removed entry.
466*4882a593Smuzhiyun  * @param type The list type
467*4882a593Smuzhiyun  * @param member Member name of the field pointing to the next entry
468*4882a593Smuzhiyun  */
469*4882a593Smuzhiyun #define nt_list_del(_entry, _list, _type, _member)		\
470*4882a593Smuzhiyun 	do {							\
471*4882a593Smuzhiyun 		_type *__e = _entry;				\
472*4882a593Smuzhiyun 		if (__e == NULL || _list == NULL) break;        \
473*4882a593Smuzhiyun 		if ((_list) == __e) {				\
474*4882a593Smuzhiyun 		    _list = __e->_member;			\
475*4882a593Smuzhiyun 		} else {					\
476*4882a593Smuzhiyun 		    _type *__prev = _list;			\
477*4882a593Smuzhiyun 		    while (__prev->_member && __prev->_member != __e)	\
478*4882a593Smuzhiyun 			__prev = nt_list_next(__prev, _member);	\
479*4882a593Smuzhiyun 		    if (__prev->_member)			\
480*4882a593Smuzhiyun 			__prev->_member = __e->_member;		\
481*4882a593Smuzhiyun 		}						\
482*4882a593Smuzhiyun 		nt_list_init(__e, _member);			\
483*4882a593Smuzhiyun 	} while(0)
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun /**
486*4882a593Smuzhiyun  * DO NOT USE THIS.
487*4882a593Smuzhiyun  * This is a remainder of the xfree86 DDX attempt of having a set of generic
488*4882a593Smuzhiyun  * list functions. Unfortunately, the xf86OptionRec uses it and we can't
489*4882a593Smuzhiyun  * easily get rid of it. Do not use for new code.
490*4882a593Smuzhiyun  */
491*4882a593Smuzhiyun typedef struct generic_list_rec {
492*4882a593Smuzhiyun     void *next;
493*4882a593Smuzhiyun } GenericListRec, *GenericListPtr, *glp;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun #endif
496