list_is_first and list_is_last respectively check whether an entry is the first or last entry of the list. list_size counts the number of entries. list_at_index retrieves the entry at an index. list_for_each_from iterates over a list from some entry to the end. Signed-off-by: Ole Schuerks <ole0811sch@xxxxxxxxx> --- scripts/include/list.h | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/scripts/include/list.h b/scripts/include/list.h index fea1e2b79063..f7aff1749a0b 100644 --- a/scripts/include/list.h +++ b/scripts/include/list.h @@ -169,6 +169,43 @@ static inline int list_empty(const struct list_head *head) return head->next == head; } +/** + * list_is_first - tests whether @list belongs to the first entry + * @list: the entry to test + * @head: the head of the list + */ +static inline int list_is_first(const struct list_head *list, + const struct list_head *head) +{ + return list == head->next; +} + +/** + * list_is_last - tests whether @list belongs to the last entry + * @list: the entry to test + * @head: the head of the list + */ +static inline int list_is_last(const struct list_head *list, + const struct list_head *head) +{ + return list == head->prev; +} + +/** + * list_size - counts the number of entries in a list + * @head: the list whose entries are counted + */ +static inline size_t list_size(const struct list_head *head) +{ + size_t ret = 0; + + for (struct list_head *curr = head->next; curr != head; + curr = curr->next) + ++ret; + + return ret; +} + /** * list_entry - get the struct for this entry * @ptr: the &struct list_head pointer. @@ -260,6 +297,40 @@ static inline int list_empty(const struct list_head *head) !list_entry_is_head(pos, head, member); \ pos = n, n = list_next_entry(n, member)) +/** + * list_for_each_entry_from - iterate over list of given type starting at a given node + * @pos: the type * to use as a loop cursor. + * @start: the node to start iterating at + * @head: the head for your list. + * @member: the name of the list_head within the struct. + */ +#define list_for_each_entry_from(pos, start, head, member) \ + for (pos = list_entry(start, typeof(*pos), member); \ + !list_entry_is_head(pos, head, member); \ + pos = list_next_entry(pos, member)) + +/** + * list_at_index - retrieve the entry at index i in O(n) + * @i: index of entry to retrieve. + * @head: the head for your list. + * @type: the type of the struct the entries are embedded in. + * @member: the name of the list_head within the struct. + */ +#define list_at_index(i, head, type, member) \ + ({ \ + type *__pos; \ + size_t __counter = 0; \ + list_for_each_entry(__pos, head, member) { \ + if (__counter++ == i) \ + break; \ + if (__pos->member.next == head) { \ + __pos = NULL; \ + break; \ + } \ + } \ + __pos; \ + }) + /* * Double linked lists with a single pointer list head. * Mostly useful for hash tables where the two pointer list head is -- 2.39.2