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