2 * Copyright © 2010 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
30 * @file Classic doubly-link circular list implementation.
31 * For real usage examples of the linked list, see the file test/list.c
34 * We need to keep a list of struct foo in the parent struct bar, i.e. what
35 * we want is something like this.
39 * struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{}
43 * We need one list head in bar and a list element in all list_of_foos (both are of
44 * data type 'struct list').
48 * struct list list_of_foos;
58 * Now we initialize the list head:
62 * list_init(&bar.list_of_foos);
64 * Then we create the first element and add it to this list:
66 * struct foo *foo = malloc(...);
68 * list_add(&foo->entry, &bar.list_of_foos);
70 * Repeat the above for each element you want to add to the list. Deleting
71 * works with the element itself.
72 * list_del(&foo->entry);
75 * Note: calling list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty
78 * Looping through the list requires a 'struct foo' as iterator and the
79 * name of the field the subnodes use.
81 * struct foo *iterator;
82 * list_foreach(iterator, &bar.list_of_foos, entry) {
83 * if (iterator->something == ...)
87 * Note: You must not call list_del() on the iterator if you continue the
88 * loop. You need to run the safe for-each loop instead:
90 * struct foo *iterator, *next;
91 * list_foreach_safe(iterator, next, &bar.list_of_foos, entry) {
93 * list_del(&iterator->entry);
99 * The linkage struct for list nodes. This struct must be part of your
100 * to-be-linked struct. struct list is required for both the head of the
101 * list and for each list node.
103 * Position and name of the struct list field is irrelevant.
104 * There are no requirements that elements of a list are of the same type.
105 * There are no requirements for a list head, any struct list can be a list
109 struct list *next, *prev;
113 * Initialize the list as an empty list.
116 * list_init(&bar->list_of_foos);
118 * @param The list to initialized.
121 list_init(struct list *list)
123 list->next = list->prev = list;
127 __list_add(struct list *entry,
128 struct list *prev, struct list *next)
137 * Insert a new element after the given list head. The new element does not
138 * need to be initialised as empty list.
139 * The list changes from:
140 * head → some element → ...
142 * head → new element → older element → ...
145 * struct foo *newfoo = malloc(...);
146 * list_add(&newfoo->entry, &bar->list_of_foos);
148 * @param entry The new element to prepend to the list.
149 * @param head The existing list.
152 list_add(struct list *entry, struct list *head)
154 __list_add(entry, head, head->next);
158 * Append a new element to the end of the list given with this list head.
160 * The list changes from:
161 * head → some element → ... → lastelement
163 * head → some element → ... → lastelement → new element
166 * struct foo *newfoo = malloc(...);
167 * list_append(&newfoo->entry, &bar->list_of_foos);
169 * @param entry The new element to prepend to the list.
170 * @param head The existing list.
173 list_append(struct list *entry, struct list *head)
175 __list_add(entry, head->prev, head);
179 __list_del(struct list *prev, struct list *next)
186 * Remove the element from the list it is in. Using this function will reset
187 * the pointers to/from this element so it is removed from the list. It does
188 * NOT free the element itself or manipulate it otherwise.
190 * Using list_del on a pure list head (like in the example at the top of
191 * this file) will NOT remove the first element from
192 * the list but rather reset the list as empty list.
195 * list_del(&foo->entry);
197 * @param entry The element to remove.
200 list_del(struct list *entry)
202 __list_del(entry->prev, entry->next);
207 * Check if the list is empty.
210 * list_empty(&bar->list_of_foos);
212 * @return True if the list contains one or more elements or False otherwise.
215 list_empty(struct list *head)
217 return head->next == head;
221 * Replace a list element by another one. This can also be used to replace
222 * the head of an existing list by another list head.
225 * list_replace(&object_foo->entry, &object_bar->entry);
226 * list_replace(&from->list_head, &into->list_head);
228 * @param old The element being replaced.
229 * @param new The element to replace @old with.
232 list_replace(struct list *old, struct list *new)
234 new->next = old->next;
235 new->next->prev = new;
236 new->prev = old->prev;
237 new->prev->next = new;
241 * Returns a pointer to the container of this list element.
245 * f = container_of(&foo->entry, struct foo, entry);
248 * @param ptr Pointer to the struct list.
249 * @param type Data type of the list element.
250 * @param member Member name of the struct list field in the list element.
251 * @return A pointer to the data struct containing the list head.
254 #define container_of(ptr, type, member) \
255 (type *)((char *)(ptr) - (char *) &((type *)0)->member)
259 * Alias of container_of
261 #define list_entry(ptr, type, member) \
262 container_of(ptr, type, member)
265 * Retrieve the first list entry for the given list pointer.
269 * first = list_first_entry(&bar->list_of_foos, struct foo, list_of_foos);
271 * @param ptr The list head
272 * @param type Data type of the list element to retrieve
273 * @param member Member name of the struct list field in the list element.
274 * @return A pointer to the first list element.
276 #define list_first_entry(ptr, type, member) \
277 list_entry((ptr)->next, type, member)
280 * Retrieve the last list entry for the given listpointer.
284 * first = list_last_entry(&bar->list_of_foos, struct foo, list_of_foos);
286 * @param ptr The list head
287 * @param type Data type of the list element to retrieve
288 * @param member Member name of the struct list field in the list element.
289 * @return A pointer to the last list element.
291 #define list_last_entry(ptr, type, member) \
292 list_entry((ptr)->prev, type, member)
294 #define __container_of(ptr, sample, member) \
295 (void *)((char *)(ptr) \
296 - offsetof(__typeof(*sample), member))
298 * Loop through the list given by head and set pos to struct in the list.
301 * struct foo *iterator;
302 * list_foreach(iterator, &bar->list_of_foos, entry) {
306 * This macro is not safe for node deletion. Use list_foreach_safe
309 * @param pos Iterator variable of the type of the list elements.
310 * @param head List head
311 * @param member Member name of the struct list in the list elements.
314 #define list_foreach(pos, head, member) \
315 for (pos = __container_of((head)->next, pos, member); \
316 &pos->member != (head); \
317 pos = __container_of(pos->member.next, pos, member))
320 * Loop through the list, keeping a backup pointer to the element. This
321 * macro allows for the deletion of a list element while looping through the
324 * See list_foreach for more details.
326 #define list_foreach_safe(pos, tmp, head, member) \
327 for (pos = __container_of((head)->next, pos, member), \
328 tmp = __container_of(pos->member.next, pos, member); \
329 &pos->member != (head); \
330 pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
332 /* NULL-Terminated List Interface
334 * The interface below does _not_ use the struct list as described above.
335 * It is mainly for legacy structures that cannot easily be switched to
338 * This interface is for structs like
345 * The position and field name of "next" are arbitrary.
349 * Init the element as null-terminated list.
352 * struct foo *list = malloc();
353 * nt_list_init(list, next);
355 * @param list The list element that will be the start of the list
356 * @param member Member name of the field pointing to next struct
358 #define nt_list_init(_list, _member) \
359 (_list)->_member = NULL
362 * Returns the next element in the list or NULL on termination.
365 * struct foo *element = list;
366 * while ((element = nt_list_next(element, next)) { }
368 * This macro is not safe for node deletion. Use list_foreach_safe
371 * @param list The list or current element.
372 * @param member Member name of the field pointing to next struct.
374 #define nt_list_next(_list, _member) \
378 * Iterate through each element in the list.
381 * struct foo *iterator;
382 * nt_list_foreach(iterator, list, next) {
386 * @param entry Assigned to the current list element
387 * @param list The list to iterate through.
388 * @param member Member name of the field pointing to next struct.
390 #define nt_list_foreach(_entry, _list, _member) \
391 for (_entry = _list; _entry; _entry = (_entry)->_member)
394 * Iterate through each element in the list, keeping a backup pointer to the
395 * element. This macro allows for the deletion of a list element while
396 * looping through the list.
398 * See nt_list_foreach for more details.
400 * @param entry Assigned to the current list element
401 * @param tmp The pointer to the next element
402 * @param list The list to iterate through.
403 * @param member Member name of the field pointing to next struct.
405 #define nt_list_foreach_safe(_entry, _tmp, _list, _member) \
406 for (_entry = _list, _tmp = (_entry) ? (_entry)->_member : NULL;\
408 _entry = _tmp, _tmp = (_tmp) ? (_tmp)->_member: NULL)
411 * Append the element to the end of the list. This macro may be used to
415 * struct foo *elem = malloc(...);
416 * nt_list_init(elem, next)
417 * nt_list_append(elem, list, struct foo, next);
419 * Resulting list order:
420 * list_item_0 -> list_item_1 -> ... -> elem_item_0 -> elem_item_1 ...
422 * @param entry An entry (or list) to append to the list
423 * @param list The list to append to. This list must be a valid list, not
425 * @param type The list type
426 * @param member Member name of the field pointing to next struct
428 #define nt_list_append(_entry, _list, _type, _member) \
430 _type *__iterator = _list; \
431 while (__iterator->_member) { __iterator = __iterator->_member;}\
432 __iterator->_member = _entry; \
436 * Insert the element at the next position in the list. This macro may be
437 * used to insert a list into a list.
439 * struct foo *elem = malloc(...);
440 * nt_list_init(elem, next)
441 * nt_list_insert(elem, list, struct foo, next);
443 * Resulting list order:
444 * list_item_0 -> elem_item_0 -> elem_item_1 ... -> list_item_1 -> ...
446 * @param entry An entry (or list) to append to the list
447 * @param list The list to insert to. This list must be a valid list, not
449 * @param type The list type
450 * @param member Member name of the field pointing to next struct
452 #define nt_list_insert(_entry, _list, _type, _member) \
454 nt_list_append((_list)->_member, _entry, _type, _member); \
455 (_list)->_member = _entry; \
459 * Delete the entry from the list by iterating through the list and
460 * removing any reference from the list to the entry.
463 * struct foo *elem = <assign to right element>
464 * nt_list_del(elem, list, struct foo, next);
466 * @param entry The entry to delete from the list. entry is always
467 * re-initialized as a null-terminated list.
468 * @param list The list containing the entry, set to the new list without
470 * @param type The list type
471 * @param member Member name of the field pointing to the next entry
473 #define nt_list_del(_entry, _list, _type, _member) \
475 _type *__e = _entry; \
476 if (__e == NULL || _list == NULL) break; \
477 if ((_list) == __e) { \
478 _list = __e->_member; \
480 _type *__prev = _list; \
481 while (__prev->_member && __prev->_member != __e) \
482 __prev = nt_list_next(__prev, _member); \
483 if (__prev->_member) \
484 __prev->_member = __e->_member; \
486 nt_list_init(__e, _member); \