diff options
author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2017-03-03 06:59:20 +0100 |
---|---|---|
committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2018-10-30 12:42:36 +0100 |
commit | c3fc97285416da124059433c9edb91796ee5e010 (patch) | |
tree | c1eb94bec559e5dd505f517e6446885333afa1ba | |
parent | 36477322d06817191639c0d4b12b67dc64352757 (diff) | |
download | sparse-c3fc97285416da124059433c9edb91796ee5e010.tar.gz |
ptrlist: add ptr_list_nth_entry()
Usually ptr lists are accessed iteratively via the FOR/END macros
but in few case we may need to access a given element in a list,
like for example when accessing a given argument of a function.
Create an helper doing that instead of open coding it.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r-- | ptrlist.c | 22 | ||||
-rw-r--r-- | ptrlist.h | 1 |
2 files changed, 23 insertions, 0 deletions
@@ -115,6 +115,28 @@ void *last_ptr_list(struct ptr_list *head) } /// +// get the nth element of a ptrlist +// @head: the head of the list +// @return: the nth element of the list or ``NULL`` if the list is too short. +void *ptr_list_nth_entry(struct ptr_list *list, unsigned int idx) +{ + struct ptr_list *head = list; + + if (!head) + return NULL; + + do { + unsigned int nr = list->nr; + + if (idx < nr) + return list->list[idx]; + else + idx -= nr; + } while ((list = list->next) != head); + return NULL; +} + +/// // linearize the entries of a list // // @head: the list to be linearized @@ -44,6 +44,7 @@ extern bool ptr_list_multiple(const struct ptr_list *head); extern int linearize_ptr_list(struct ptr_list *, void **, int); extern void *first_ptr_list(struct ptr_list *); extern void *last_ptr_list(struct ptr_list *); +extern void *ptr_list_nth_entry(struct ptr_list *, unsigned int idx); extern void pack_ptr_list(struct ptr_list **); /* |