1*677dec6eSriastradh /*	$NetBSD: list.h,v 1.3 2021/12/18 23:45:33 riastradh Exp $	*/
2d350ecf5Sriastradh 
3d350ecf5Sriastradh /*
4d350ecf5Sriastradh  * Copyright © 2010 Intel Corporation
5d350ecf5Sriastradh  * Copyright © 2010 Francisco Jerez <currojerez@riseup.net>
6d350ecf5Sriastradh  *
7d350ecf5Sriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
8d350ecf5Sriastradh  * copy of this software and associated documentation files (the "Software"),
9d350ecf5Sriastradh  * to deal in the Software without restriction, including without limitation
10d350ecf5Sriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11d350ecf5Sriastradh  * and/or sell copies of the Software, and to permit persons to whom the
12d350ecf5Sriastradh  * Software is furnished to do so, subject to the following conditions:
13d350ecf5Sriastradh  *
14d350ecf5Sriastradh  * The above copyright notice and this permission notice (including the next
15d350ecf5Sriastradh  * paragraph) shall be included in all copies or substantial portions of the
16d350ecf5Sriastradh  * Software.
17d350ecf5Sriastradh  *
18d350ecf5Sriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19d350ecf5Sriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20d350ecf5Sriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21d350ecf5Sriastradh  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22d350ecf5Sriastradh  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23d350ecf5Sriastradh  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24d350ecf5Sriastradh  * IN THE SOFTWARE.
25d350ecf5Sriastradh  *
26d350ecf5Sriastradh  */
27d350ecf5Sriastradh 
28d350ecf5Sriastradh /* Modified by Ben Skeggs <bskeggs@redhat.com> to match kernel list APIs */
29d350ecf5Sriastradh 
30d350ecf5Sriastradh #ifndef _XORG_LIST_H_
31d350ecf5Sriastradh #define _XORG_LIST_H_
32d350ecf5Sriastradh 
33d350ecf5Sriastradh /**
34d350ecf5Sriastradh  * @file Classic doubly-link circular list implementation.
35d350ecf5Sriastradh  * For real usage examples of the linked list, see the file test/list.c
36d350ecf5Sriastradh  *
37d350ecf5Sriastradh  * Example:
38d350ecf5Sriastradh  * We need to keep a list of struct foo in the parent struct bar, i.e. what
39d350ecf5Sriastradh  * we want is something like this.
40d350ecf5Sriastradh  *
41d350ecf5Sriastradh  *     struct bar {
42d350ecf5Sriastradh  *          ...
43d350ecf5Sriastradh  *          struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
44d350ecf5Sriastradh  *          ...
45d350ecf5Sriastradh  *     }
46d350ecf5Sriastradh  *
47d350ecf5Sriastradh  * We need one list head in bar and a list element in all list_of_foos (both are of
48d350ecf5Sriastradh  * data type 'struct list_head').
49d350ecf5Sriastradh  *
50d350ecf5Sriastradh  *     struct bar {
51d350ecf5Sriastradh  *          ...
52d350ecf5Sriastradh  *          struct list_head list_of_foos;
53d350ecf5Sriastradh  *          ...
54d350ecf5Sriastradh  *     }
55d350ecf5Sriastradh  *
56d350ecf5Sriastradh  *     struct foo {
57d350ecf5Sriastradh  *          ...
58d350ecf5Sriastradh  *          struct list_head entry;
59d350ecf5Sriastradh  *          ...
60d350ecf5Sriastradh  *     }
61d350ecf5Sriastradh  *
62d350ecf5Sriastradh  * Now we initialize the list head:
63d350ecf5Sriastradh  *
64d350ecf5Sriastradh  *     struct bar bar;
65d350ecf5Sriastradh  *     ...
66d350ecf5Sriastradh  *     INIT_LIST_HEAD(&bar.list_of_foos);
67d350ecf5Sriastradh  *
68d350ecf5Sriastradh  * Then we create the first element and add it to this list:
69d350ecf5Sriastradh  *
70d350ecf5Sriastradh  *     struct foo *foo = malloc(...);
71d350ecf5Sriastradh  *     ....
72d350ecf5Sriastradh  *     list_add(&foo->entry, &bar.list_of_foos);
73d350ecf5Sriastradh  *
74d350ecf5Sriastradh  * Repeat the above for each element you want to add to the list. Deleting
75d350ecf5Sriastradh  * works with the element itself.
76d350ecf5Sriastradh  *      list_del(&foo->entry);
77d350ecf5Sriastradh  *      free(foo);
78d350ecf5Sriastradh  *
79d350ecf5Sriastradh  * Note: calling list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
80d350ecf5Sriastradh  * list again.
81d350ecf5Sriastradh  *
82d350ecf5Sriastradh  * Looping through the list requires a 'struct foo' as iterator and the
83d350ecf5Sriastradh  * name of the field the subnodes use.
84d350ecf5Sriastradh  *
85d350ecf5Sriastradh  * struct foo *iterator;
86d350ecf5Sriastradh  * list_for_each_entry(iterator, &bar.list_of_foos, entry) {
87d350ecf5Sriastradh  *      if (iterator->something == ...)
88d350ecf5Sriastradh  *             ...
89d350ecf5Sriastradh  * }
90d350ecf5Sriastradh  *
91d350ecf5Sriastradh  * Note: You must not call list_del() on the iterator if you continue the
92d350ecf5Sriastradh  * loop. You need to run the safe for-each loop instead:
93d350ecf5Sriastradh  *
94d350ecf5Sriastradh  * struct foo *iterator, *next;
95d350ecf5Sriastradh  * list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) {
96d350ecf5Sriastradh  *      if (...)
97d350ecf5Sriastradh  *              list_del(&iterator->entry);
98d350ecf5Sriastradh  * }
99d350ecf5Sriastradh  *
100d350ecf5Sriastradh  */
101d350ecf5Sriastradh 
102d350ecf5Sriastradh /**
103d350ecf5Sriastradh  * The linkage struct for list nodes. This struct must be part of your
104d350ecf5Sriastradh  * to-be-linked struct. struct list_head is required for both the head of the
105d350ecf5Sriastradh  * list and for each list node.
106d350ecf5Sriastradh  *
107d350ecf5Sriastradh  * Position and name of the struct list_head field is irrelevant.
108d350ecf5Sriastradh  * There are no requirements that elements of a list are of the same type.
109d350ecf5Sriastradh  * There are no requirements for a list head, any struct list_head can be a list
110d350ecf5Sriastradh  * head.
111d350ecf5Sriastradh  */
112d350ecf5Sriastradh struct list_head {
113d350ecf5Sriastradh     struct list_head *next, *prev;
114d350ecf5Sriastradh };
115d350ecf5Sriastradh 
116d350ecf5Sriastradh /**
117d350ecf5Sriastradh  * Initialize the list as an empty list.
118d350ecf5Sriastradh  *
119d350ecf5Sriastradh  * Example:
120d350ecf5Sriastradh  * INIT_LIST_HEAD(&bar->list_of_foos);
121d350ecf5Sriastradh  *
122d350ecf5Sriastradh  * @param The list to initialized.
123d350ecf5Sriastradh  */
124d350ecf5Sriastradh #define LIST_HEAD_INIT(name) { &(name), &(name) }
125d350ecf5Sriastradh 
126d350ecf5Sriastradh #define LIST_HEAD(name) \
127d350ecf5Sriastradh 	struct list_head name = LIST_HEAD_INIT(name)
128d350ecf5Sriastradh 
129d350ecf5Sriastradh static inline void
INIT_LIST_HEAD(struct list_head * list)130d350ecf5Sriastradh INIT_LIST_HEAD(struct list_head *list)
131d350ecf5Sriastradh {
132d350ecf5Sriastradh     list->next = list->prev = list;
133d350ecf5Sriastradh }
134d350ecf5Sriastradh 
135d350ecf5Sriastradh static inline void
__list_add(struct list_head * entry,struct list_head * prev,struct list_head * next)136d350ecf5Sriastradh __list_add(struct list_head *entry,
137d350ecf5Sriastradh                 struct list_head *prev, struct list_head *next)
138d350ecf5Sriastradh {
139d350ecf5Sriastradh     next->prev = entry;
140d350ecf5Sriastradh     entry->next = next;
141d350ecf5Sriastradh     entry->prev = prev;
142d350ecf5Sriastradh     prev->next = entry;
143d350ecf5Sriastradh }
144d350ecf5Sriastradh 
145d350ecf5Sriastradh /**
146d350ecf5Sriastradh  * Insert a new element after the given list head. The new element does not
147d350ecf5Sriastradh  * need to be initialised as empty list.
148d350ecf5Sriastradh  * The list changes from:
149d350ecf5Sriastradh  *      head → some element → ...
150d350ecf5Sriastradh  * to
151d350ecf5Sriastradh  *      head → new element → older element → ...
152d350ecf5Sriastradh  *
153d350ecf5Sriastradh  * Example:
154d350ecf5Sriastradh  * struct foo *newfoo = malloc(...);
155d350ecf5Sriastradh  * list_add(&newfoo->entry, &bar->list_of_foos);
156d350ecf5Sriastradh  *
157d350ecf5Sriastradh  * @param entry The new element to prepend to the list.
158d350ecf5Sriastradh  * @param head The existing list.
159d350ecf5Sriastradh  */
160d350ecf5Sriastradh static inline void
list_add(struct list_head * entry,struct list_head * head)161d350ecf5Sriastradh list_add(struct list_head *entry, struct list_head *head)
162d350ecf5Sriastradh {
163d350ecf5Sriastradh     __list_add(entry, head, head->next);
164d350ecf5Sriastradh }
165d350ecf5Sriastradh 
166d350ecf5Sriastradh /**
167d350ecf5Sriastradh  * Append a new element to the end of the list given with this list head.
168d350ecf5Sriastradh  *
169d350ecf5Sriastradh  * The list changes from:
170d350ecf5Sriastradh  *      head → some element → ... → lastelement
171d350ecf5Sriastradh  * to
172d350ecf5Sriastradh  *      head → some element → ... → lastelement → new element
173d350ecf5Sriastradh  *
174d350ecf5Sriastradh  * Example:
175d350ecf5Sriastradh  * struct foo *newfoo = malloc(...);
176d350ecf5Sriastradh  * list_add_tail(&newfoo->entry, &bar->list_of_foos);
177d350ecf5Sriastradh  *
178d350ecf5Sriastradh  * @param entry The new element to prepend to the list.
179d350ecf5Sriastradh  * @param head The existing list.
180d350ecf5Sriastradh  */
181d350ecf5Sriastradh static inline void
list_add_tail(struct list_head * entry,struct list_head * head)182d350ecf5Sriastradh list_add_tail(struct list_head *entry, struct list_head *head)
183d350ecf5Sriastradh {
184d350ecf5Sriastradh     __list_add(entry, head->prev, head);
185d350ecf5Sriastradh }
186d350ecf5Sriastradh 
187d350ecf5Sriastradh static inline void
__list_del(struct list_head * prev,struct list_head * next)188d350ecf5Sriastradh __list_del(struct list_head *prev, struct list_head *next)
189d350ecf5Sriastradh {
190d350ecf5Sriastradh     next->prev = prev;
191d350ecf5Sriastradh     prev->next = next;
192d350ecf5Sriastradh }
193d350ecf5Sriastradh 
194d350ecf5Sriastradh /**
195d350ecf5Sriastradh  * Remove the element from the list it is in. Using this function will reset
196d350ecf5Sriastradh  * the pointers to/from this element so it is removed from the list. It does
197d350ecf5Sriastradh  * NOT free the element itself or manipulate it otherwise.
198d350ecf5Sriastradh  *
199d350ecf5Sriastradh  * Using list_del on a pure list head (like in the example at the top of
200d350ecf5Sriastradh  * this file) will NOT remove the first element from
201d350ecf5Sriastradh  * the list but rather reset the list as empty list.
202d350ecf5Sriastradh  *
203d350ecf5Sriastradh  * Example:
204d350ecf5Sriastradh  * list_del(&foo->entry);
205d350ecf5Sriastradh  *
206d350ecf5Sriastradh  * @param entry The element to remove.
207d350ecf5Sriastradh  */
208d350ecf5Sriastradh static inline void
list_del(struct list_head * entry)209d350ecf5Sriastradh list_del(struct list_head *entry)
210d350ecf5Sriastradh {
211d350ecf5Sriastradh     __list_del(entry->prev, entry->next);
212d350ecf5Sriastradh }
213d350ecf5Sriastradh 
214d350ecf5Sriastradh static inline void
list_del_init(struct list_head * entry)215d350ecf5Sriastradh list_del_init(struct list_head *entry)
216d350ecf5Sriastradh {
217d350ecf5Sriastradh     __list_del(entry->prev, entry->next);
218d350ecf5Sriastradh     INIT_LIST_HEAD(entry);
219d350ecf5Sriastradh }
220d350ecf5Sriastradh 
list_move_tail(struct list_head * list,struct list_head * head)221d350ecf5Sriastradh static inline void list_move_tail(struct list_head *list,
222d350ecf5Sriastradh 				  struct list_head *head)
223d350ecf5Sriastradh {
224d350ecf5Sriastradh 	__list_del(list->prev, list->next);
225d350ecf5Sriastradh 	list_add_tail(list, head);
226d350ecf5Sriastradh }
227d350ecf5Sriastradh 
228d350ecf5Sriastradh /**
229d350ecf5Sriastradh  * Check if the list is empty.
230d350ecf5Sriastradh  *
231d350ecf5Sriastradh  * Example:
232d350ecf5Sriastradh  * list_empty(&bar->list_of_foos);
233d350ecf5Sriastradh  *
234d350ecf5Sriastradh  * @return True if the list contains one or more elements or False otherwise.
235d350ecf5Sriastradh  */
236d350ecf5Sriastradh static inline bool
list_empty(struct list_head * head)237d350ecf5Sriastradh list_empty(struct list_head *head)
238d350ecf5Sriastradh {
239d350ecf5Sriastradh     return head->next == head;
240d350ecf5Sriastradh }
241d350ecf5Sriastradh 
242d350ecf5Sriastradh /**
243d350ecf5Sriastradh  * Returns a pointer to the container of this list element.
244d350ecf5Sriastradh  *
245d350ecf5Sriastradh  * Example:
246d350ecf5Sriastradh  * struct foo* f;
247d350ecf5Sriastradh  * f = container_of(&foo->entry, struct foo, entry);
248d350ecf5Sriastradh  * assert(f == foo);
249d350ecf5Sriastradh  *
250d350ecf5Sriastradh  * @param ptr Pointer to the struct list_head.
251d350ecf5Sriastradh  * @param type Data type of the list element.
252d350ecf5Sriastradh  * @param member Member name of the struct list_head field in the list element.
253d350ecf5Sriastradh  * @return A pointer to the data struct containing the list head.
254d350ecf5Sriastradh  */
255d350ecf5Sriastradh #ifndef container_of
256d350ecf5Sriastradh #define container_of(ptr, type, member) \
257d350ecf5Sriastradh     (type *)((char *)(ptr) - (char *) &((type *)0)->member)
258d350ecf5Sriastradh #endif
259d350ecf5Sriastradh 
260d350ecf5Sriastradh /**
261d350ecf5Sriastradh  * Alias of container_of
262d350ecf5Sriastradh  */
263d350ecf5Sriastradh #define list_entry(ptr, type, member) \
264d350ecf5Sriastradh     container_of(ptr, type, member)
265d350ecf5Sriastradh 
266d350ecf5Sriastradh /**
267d350ecf5Sriastradh  * Retrieve the first list entry for the given list pointer.
268d350ecf5Sriastradh  *
269d350ecf5Sriastradh  * Example:
270d350ecf5Sriastradh  * struct foo *first;
271d350ecf5Sriastradh  * first = list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
272d350ecf5Sriastradh  *
273d350ecf5Sriastradh  * @param ptr The list head
274d350ecf5Sriastradh  * @param type Data type of the list element to retrieve
275d350ecf5Sriastradh  * @param member Member name of the struct list_head field in the list element.
276d350ecf5Sriastradh  * @return A pointer to the first list element.
277d350ecf5Sriastradh  */
278d350ecf5Sriastradh #define list_first_entry(ptr, type, member) \
279d350ecf5Sriastradh     list_entry((ptr)->next, type, member)
280d350ecf5Sriastradh 
281d350ecf5Sriastradh /**
282d350ecf5Sriastradh  * Retrieve the last list entry for the given listpointer.
283d350ecf5Sriastradh  *
284d350ecf5Sriastradh  * Example:
285d350ecf5Sriastradh  * struct foo *first;
286d350ecf5Sriastradh  * first = list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
287d350ecf5Sriastradh  *
288d350ecf5Sriastradh  * @param ptr The list head
289d350ecf5Sriastradh  * @param type Data type of the list element to retrieve
290d350ecf5Sriastradh  * @param member Member name of the struct list_head field in the list element.
291d350ecf5Sriastradh  * @return A pointer to the last list element.
292d350ecf5Sriastradh  */
293d350ecf5Sriastradh #define list_last_entry(ptr, type, member) \
294d350ecf5Sriastradh     list_entry((ptr)->prev, type, member)
295d350ecf5Sriastradh 
296d350ecf5Sriastradh #define __container_of(ptr, sample, member)				\
297d350ecf5Sriastradh     (void *)container_of((ptr), typeof(*(sample)), member)
298d350ecf5Sriastradh 
299d350ecf5Sriastradh /**
300d350ecf5Sriastradh  * Loop through the list given by head and set pos to struct in the list.
301d350ecf5Sriastradh  *
302d350ecf5Sriastradh  * Example:
303d350ecf5Sriastradh  * struct foo *iterator;
304d350ecf5Sriastradh  * list_for_each_entry(iterator, &bar->list_of_foos, entry) {
305d350ecf5Sriastradh  *      [modify iterator]
306d350ecf5Sriastradh  * }
307d350ecf5Sriastradh  *
308d350ecf5Sriastradh  * This macro is not safe for node deletion. Use list_for_each_entry_safe
309d350ecf5Sriastradh  * instead.
310d350ecf5Sriastradh  *
311d350ecf5Sriastradh  * @param pos Iterator variable of the type of the list elements.
312d350ecf5Sriastradh  * @param head List head
313d350ecf5Sriastradh  * @param member Member name of the struct list_head in the list elements.
314d350ecf5Sriastradh  *
315d350ecf5Sriastradh  */
316d350ecf5Sriastradh #define list_for_each_entry(pos, head, member)				\
317d350ecf5Sriastradh     for (pos = __container_of((head)->next, pos, member);		\
318d350ecf5Sriastradh 	 &pos->member != (head);					\
319d350ecf5Sriastradh 	 pos = __container_of(pos->member.next, pos, member))
320d350ecf5Sriastradh 
321d350ecf5Sriastradh /**
322d350ecf5Sriastradh  * Loop through the list, keeping a backup pointer to the element. This
323d350ecf5Sriastradh  * macro allows for the deletion of a list element while looping through the
324d350ecf5Sriastradh  * list.
325d350ecf5Sriastradh  *
326d350ecf5Sriastradh  * See list_for_each_entry for more details.
327d350ecf5Sriastradh  */
328d350ecf5Sriastradh #define list_for_each_entry_safe(pos, tmp, head, member)		\
329d350ecf5Sriastradh     for (pos = __container_of((head)->next, pos, member),		\
330d350ecf5Sriastradh 	 tmp = __container_of(pos->member.next, pos, member);		\
331d350ecf5Sriastradh 	 &pos->member != (head);					\
332d350ecf5Sriastradh 	 pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
333d350ecf5Sriastradh 
334d350ecf5Sriastradh 
335d350ecf5Sriastradh #define list_for_each_entry_reverse(pos, head, member)			\
336d350ecf5Sriastradh 	for (pos = __container_of((head)->prev, pos, member);		\
337d350ecf5Sriastradh 	     &pos->member != (head);					\
338d350ecf5Sriastradh 	     pos = __container_of(pos->member.prev, pos, member))
339d350ecf5Sriastradh 
340d350ecf5Sriastradh #define list_for_each_entry_continue(pos, head, member)			\
341d350ecf5Sriastradh 	for (pos = __container_of(pos->member.next, pos, member);	\
342d350ecf5Sriastradh 	     &pos->member != (head);					\
343d350ecf5Sriastradh 	     pos = __container_of(pos->member.next, pos, member))
344d350ecf5Sriastradh 
345d350ecf5Sriastradh #define list_for_each_entry_continue_reverse(pos, head, member)		\
346d350ecf5Sriastradh 	for (pos = __container_of(pos->member.prev, pos, member);	\
347d350ecf5Sriastradh 	     &pos->member != (head);					\
348d350ecf5Sriastradh 	     pos = __container_of(pos->member.prev, pos, member))
349d350ecf5Sriastradh 
350d350ecf5Sriastradh #define list_for_each_entry_from(pos, head, member)			\
351d350ecf5Sriastradh 	for (;								\
352d350ecf5Sriastradh 	     &pos->member != (head);					\
353d350ecf5Sriastradh 	     pos = __container_of(pos->member.next, pos, member))
354d350ecf5Sriastradh 
355d350ecf5Sriastradh #endif
356