diff mbox

[v2,10/27] add ptr_list_nth_entry()

Message ID 20170311090706.17171-11-luc.vanoostenryck@gmail.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Luc Van Oostenryck March 11, 2017, 9:06 a.m. UTC
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>
---
 ptrlist.c | 18 ++++++++++++++++++
 ptrlist.h |  1 +
 2 files changed, 19 insertions(+)
diff mbox

Patch

diff --git a/ptrlist.c b/ptrlist.c
index 5dc1117c5..29aafac7d 100644
--- a/ptrlist.c
+++ b/ptrlist.c
@@ -246,3 +246,21 @@  void __free_ptr_list(struct ptr_list **listp)
 
 	*listp = NULL;
 }
+
+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;
+}
diff --git a/ptrlist.h b/ptrlist.h
index d09be2f51..bf171f89f 100644
--- a/ptrlist.h
+++ b/ptrlist.h
@@ -44,6 +44,7 @@  extern void concat_ptr_list(struct ptr_list *a, struct ptr_list **b);
 extern void __free_ptr_list(struct ptr_list **);
 extern int ptr_list_size(struct ptr_list *);
 extern int linearize_ptr_list(struct ptr_list *, void **, int);
+extern void *ptr_list_nth_entry(struct ptr_list *, unsigned int idx);
 
 /*
  * Hey, who said that you can't do overloading in C?