summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2017-03-03 06:59:20 +0100
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-10-30 12:42:36 +0100
commitc3fc97285416da124059433c9edb91796ee5e010 (patch)
treec1eb94bec559e5dd505f517e6446885333afa1ba
parent36477322d06817191639c0d4b12b67dc64352757 (diff)
downloadsparse-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.c22
-rw-r--r--ptrlist.h1
2 files changed, 23 insertions, 0 deletions
diff --git a/ptrlist.c b/ptrlist.c
index 3677a347..0fb28127 100644
--- a/ptrlist.c
+++ b/ptrlist.c
@@ -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
diff --git a/ptrlist.h b/ptrlist.h
index 2f023478..2411e745 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 **);
/*