Eugeniu Rosca | d57dd94 | 2018-05-19 14:13:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 2 | #ifndef LIST_H |
| 3 | #define LIST_H |
| 4 | |
| 5 | /* |
| 6 | * Copied from include/linux/... |
| 7 | */ |
| 8 | |
| 9 | #undef offsetof |
| 10 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 11 | |
| 12 | /** |
| 13 | * container_of - cast a member of a structure out to the containing structure |
| 14 | * @ptr: the pointer to the member. |
| 15 | * @type: the type of the container struct this is embedded in. |
| 16 | * @member: the name of the member within the struct. |
| 17 | * |
| 18 | */ |
| 19 | #define container_of(ptr, type, member) ({ \ |
| 20 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 21 | (type *)( (char *)__mptr - offsetof(type,member) );}) |
| 22 | |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 23 | struct list_head { |
| 24 | struct list_head *next, *prev; |
| 25 | }; |
| 26 | |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 27 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 28 | |
| 29 | #define LIST_HEAD(name) \ |
| 30 | struct list_head name = LIST_HEAD_INIT(name) |
| 31 | |
| 32 | /** |
| 33 | * list_entry - get the struct for this entry |
| 34 | * @ptr: the &struct list_head pointer. |
| 35 | * @type: the type of the struct this is embedded in. |
Masahiro Yamada | 331cec3 | 2015-07-05 01:56:54 +0900 | [diff] [blame] | 36 | * @member: the name of the list_head within the struct. |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 37 | */ |
| 38 | #define list_entry(ptr, type, member) \ |
| 39 | container_of(ptr, type, member) |
| 40 | |
| 41 | /** |
| 42 | * list_for_each_entry - iterate over list of given type |
| 43 | * @pos: the type * to use as a loop cursor. |
| 44 | * @head: the head for your list. |
Masahiro Yamada | 331cec3 | 2015-07-05 01:56:54 +0900 | [diff] [blame] | 45 | * @member: the name of the list_head within the struct. |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 46 | */ |
| 47 | #define list_for_each_entry(pos, head, member) \ |
| 48 | for (pos = list_entry((head)->next, typeof(*pos), member); \ |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 49 | &pos->member != (head); \ |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 50 | pos = list_entry(pos->member.next, typeof(*pos), member)) |
| 51 | |
| 52 | /** |
| 53 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
| 54 | * @pos: the type * to use as a loop cursor. |
| 55 | * @n: another type * to use as temporary storage |
| 56 | * @head: the head for your list. |
Masahiro Yamada | 331cec3 | 2015-07-05 01:56:54 +0900 | [diff] [blame] | 57 | * @member: the name of the list_head within the struct. |
Masahiro Yamada | ed16f5a | 2014-07-30 14:08:13 +0900 | [diff] [blame] | 58 | */ |
| 59 | #define list_for_each_entry_safe(pos, n, head, member) \ |
| 60 | for (pos = list_entry((head)->next, typeof(*pos), member), \ |
| 61 | n = list_entry(pos->member.next, typeof(*pos), member); \ |
| 62 | &pos->member != (head); \ |
| 63 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) |
| 64 | |
| 65 | /** |
| 66 | * list_empty - tests whether a list is empty |
| 67 | * @head: the list to test. |
| 68 | */ |
| 69 | static inline int list_empty(const struct list_head *head) |
| 70 | { |
| 71 | return head->next == head; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Insert a new entry between two known consecutive entries. |
| 76 | * |
| 77 | * This is only for internal list manipulation where we know |
| 78 | * the prev/next entries already! |
| 79 | */ |
| 80 | static inline void __list_add(struct list_head *_new, |
| 81 | struct list_head *prev, |
| 82 | struct list_head *next) |
| 83 | { |
| 84 | next->prev = _new; |
| 85 | _new->next = next; |
| 86 | _new->prev = prev; |
| 87 | prev->next = _new; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * list_add_tail - add a new entry |
| 92 | * @new: new entry to be added |
| 93 | * @head: list head to add it before |
| 94 | * |
| 95 | * Insert a new entry before the specified head. |
| 96 | * This is useful for implementing queues. |
| 97 | */ |
| 98 | static inline void list_add_tail(struct list_head *_new, struct list_head *head) |
| 99 | { |
| 100 | __list_add(_new, head->prev, head); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Delete a list entry by making the prev/next entries |
| 105 | * point to each other. |
| 106 | * |
| 107 | * This is only for internal list manipulation where we know |
| 108 | * the prev/next entries already! |
| 109 | */ |
| 110 | static inline void __list_del(struct list_head *prev, struct list_head *next) |
| 111 | { |
| 112 | next->prev = prev; |
| 113 | prev->next = next; |
| 114 | } |
| 115 | |
| 116 | #define LIST_POISON1 ((void *) 0x00100100) |
| 117 | #define LIST_POISON2 ((void *) 0x00200200) |
| 118 | /** |
| 119 | * list_del - deletes entry from list. |
| 120 | * @entry: the element to delete from the list. |
| 121 | * Note: list_empty() on entry does not return true after this, the entry is |
| 122 | * in an undefined state. |
| 123 | */ |
| 124 | static inline void list_del(struct list_head *entry) |
| 125 | { |
| 126 | __list_del(entry->prev, entry->next); |
| 127 | entry->next = (struct list_head*)LIST_POISON1; |
| 128 | entry->prev = (struct list_head*)LIST_POISON2; |
| 129 | } |
| 130 | #endif |