2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2010 Francisco Jerez <currojerez@riseup.net>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #ifndef _INTEL_LIST_H_
27 #define _INTEL_LIST_H_
29 #include <xorgVersion.h>
31 #if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,9,0,0,0) || XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,11,99,903,0)
36 * @file Classic doubly-link circular list implementation.
37 * For real usage examples of the linked list, see the file test/list.c
40 * We need to keep a list of struct foo in the parent struct bar, i.e. what
41 * we want is something like this.
45 * struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
49 * We need one list head in bar and a list element in all list_of_foos (both are of
50 * data type 'struct list').
54 * struct list list_of_foos;
64 * Now we initialize the list head:
68 * list_init(&bar.list_of_foos);
70 * Then we create the first element and add it to this list:
72 * struct foo *foo = malloc(...);
74 * list_add(&foo->entry, &bar.list_of_foos);
76 * Repeat the above for each element you want to add to the list. Deleting
77 * works with the element itself.
78 * list_del(&foo->entry);
81 * Note: calling list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
84 * Looping through the list requires a 'struct foo' as iterator and the
85 * name of the field the subnodes use.
87 * struct foo *iterator;
88 * list_for_each_entry(iterator, &bar.list_of_foos, entry) {
89 * if (iterator->something == ...)
93 * Note: You must not call list_del() on the iterator if you continue the
94 * loop. You need to run the safe for-each loop instead:
96 * struct foo *iterator, *next;
97 * list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) {
99 * list_del(&iterator->entry);
105 * The linkage struct for list nodes. This struct must be part of your
106 * to-be-linked struct. struct list is required for both the head of the
107 * list and for each list node.
109 * Position and name of the struct list field is irrelevant.
110 * There are no requirements that elements of a list are of the same type.
111 * There are no requirements for a list head, any struct list can be a list
115 struct list *next, *prev;
119 * Initialize the list as an empty list.
122 * list_init(&bar->list_of_foos);
124 * @param The list to initialized.
127 list_init(struct list *list)
129 list->next = list->prev = list;
133 __list_add(struct list *entry,
144 * Insert a new element after the given list head. The new element does not
145 * need to be initialised as empty list.
146 * The list changes from:
147 * head → some element → ...
149 * head → new element → older element → ...
152 * struct foo *newfoo = malloc(...);
153 * list_add(&newfoo->entry, &bar->list_of_foos);
155 * @param entry The new element to prepend to the list.
156 * @param head The existing list.
159 list_add(struct list *entry, struct list *head)
161 __list_add(entry, head, head->next);
165 list_add_tail(struct list *entry, struct list *head)
167 __list_add(entry, head->prev, head);
170 static inline void list_replace(struct list *old,
173 new->next = old->next;
174 new->next->prev = new;
175 new->prev = old->prev;
176 new->prev->next = new;
179 #define list_last_entry(ptr, type, member) \
180 list_entry((ptr)->prev, type, member)
182 #define list_for_each(pos, head) \
183 for (pos = (head)->next; pos != (head); pos = pos->next)
186 * Append a new element to the end of the list given with this list head.
188 * The list changes from:
189 * head → some element → ... → lastelement
191 * head → some element → ... → lastelement → new element
194 * struct foo *newfoo = malloc(...);
195 * list_append(&newfoo->entry, &bar->list_of_foos);
197 * @param entry The new element to prepend to the list.
198 * @param head The existing list.
201 list_append(struct list *entry, struct list *head)
203 __list_add(entry, head->prev, head);
208 __list_del(struct list *prev, struct list *next)
210 assert(next->prev == prev->next);
216 _list_del(struct list *entry)
218 assert(entry->prev->next == entry);
219 assert(entry->next->prev == entry);
220 __list_del(entry->prev, entry->next);
224 * Remove the element from the list it is in. Using this function will reset
225 * the pointers to/from this element so it is removed from the list. It does
226 * NOT free the element itself or manipulate it otherwise.
228 * Using list_del on a pure list head (like in the example at the top of
229 * this file) will NOT remove the first element from
230 * the list but rather reset the list as empty list.
233 * list_del(&foo->entry);
235 * @param entry The element to remove.
238 list_del(struct list *entry)
244 static inline void list_move(struct list *list, struct list *head)
246 if (list->prev != head) {
248 list_add(list, head);
252 static inline void list_move_tail(struct list *list, struct list *head)
255 list_add_tail(list, head);
259 * Check if the list is empty.
262 * list_is_empty(&bar->list_of_foos);
264 * @return True if the list contains one or more elements or False otherwise.
267 list_is_empty(struct list *head)
269 return head->next == head;
273 * Alias of container_of
275 #define list_entry(ptr, type, member) \
276 container_of(ptr, type, member)
279 * Retrieve the first list entry for the given list pointer.
283 * first = list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
285 * @param ptr The list head
286 * @param type Data type of the list element to retrieve
287 * @param member Member name of the struct list field in the list element.
288 * @return A pointer to the first list element.
290 #define list_first_entry(ptr, type, member) \
291 list_entry((ptr)->next, type, member)
294 * Retrieve the last list entry for the given listpointer.
298 * first = list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
300 * @param ptr The list head
301 * @param type Data type of the list element to retrieve
302 * @param member Member name of the struct list field in the list element.
303 * @return A pointer to the last list element.
305 #define list_last_entry(ptr, type, member) \
306 list_entry((ptr)->prev, type, member)
308 #define __container_of(ptr, sample, member) \
309 (void *)((char *)(ptr) \
310 - ((char *)&(sample)->member - (char *)(sample)))
312 * Loop through the list given by head and set pos to struct in the list.
315 * struct foo *iterator;
316 * list_for_each_entry(iterator, &bar->list_of_foos, entry) {
320 * This macro is not safe for node deletion. Use list_for_each_entry_safe
323 * @param pos Iterator variable of the type of the list elements.
324 * @param head List head
325 * @param member Member name of the struct list in the list elements.
328 #define list_for_each_entry(pos, head, member) \
329 for (pos = __container_of((head)->next, pos, member); \
330 &pos->member != (head); \
331 pos = __container_of(pos->member.next, pos, member))
333 #define list_for_each_entry_reverse(pos, head, member) \
334 for (pos = __container_of((head)->prev, pos, member); \
335 &pos->member != (head); \
336 pos = __container_of(pos->member.prev, pos, member))
339 * Loop through the list, keeping a backup pointer to the element. This
340 * macro allows for the deletion of a list element while looping through the
343 * See list_for_each_entry for more details.
345 #define list_for_each_entry_safe(pos, tmp, head, member) \
346 for (pos = __container_of((head)->next, pos, member), \
347 tmp = __container_of(pos->member.next, pos, member); \
348 &pos->member != (head); \
349 pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
356 list_add_tail(struct list *entry, struct list *head)
358 __list_add(entry, head->prev, head);
362 _list_del(struct list *entry)
364 assert(entry->prev->next == entry);
365 assert(entry->next->prev == entry);
366 __list_del(entry->prev, entry->next);
369 static inline void list_replace(struct list *old,
372 new->next = old->next;
373 new->next->prev = new;
374 new->prev = old->prev;
375 new->prev->next = new;
378 static inline void list_move(struct list *list, struct list *head)
380 if (list->prev != head) {
382 list_add(list, head);
386 static inline void list_move_tail(struct list *list, struct list *head)
389 list_add_tail(list, head);
392 #define list_last_entry(ptr, type, member) \
393 list_entry((ptr)->prev, type, member)
395 #define list_for_each_entry_reverse(pos, head, member) \
396 for (pos = __container_of((head)->prev, pos, member); \
397 &pos->member != (head); \
398 pos = __container_of(pos->member.prev, pos, member))
403 #define container_of(ptr, type, member) \
404 ((type *)((char *)(ptr) - (char *) &((type *)0)->member))
406 #endif /* _INTEL_LIST_H_ */