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@xxxxxxxxx> --- ptrlist.c | 22 ++++++++++++++++++++++ ptrlist.h | 1 + 2 files changed, 23 insertions(+) diff --git a/ptrlist.c b/ptrlist.c index 3677a347c..0fb281274 100644 --- a/ptrlist.c +++ b/ptrlist.c @@ -114,6 +114,28 @@ void *last_ptr_list(struct ptr_list *head) return PTR_ENTRY_NOTAG(list, list->nr-1); } +/// +// 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 // diff --git a/ptrlist.h b/ptrlist.h index 2f0234784..2411e745a 100644 --- a/ptrlist.h +++ b/ptrlist.h @@ -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 **); /* -- 2.19.0