[PATCH 1/5] ptrlist: add ptr_list_nth_entry()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>

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.

[ben.dooks@xxxxxxxxxxxxxxx: applied to current working tree]
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 ptrlist.c | 22 ++++++++++++++++++++++
 ptrlist.h |  2 ++
 2 files changed, 24 insertions(+)

diff --git a/ptrlist.c b/ptrlist.c
index 8402f8d..fce185d 100644
--- a/ptrlist.c
+++ b/ptrlist.c
@@ -29,6 +29,28 @@ int ptr_list_size(struct ptr_list *head)
 	return nr;
 }
 
+///
+// 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 up to a total of 'max',
  * and return the nr of entries linearized.
diff --git a/ptrlist.h b/ptrlist.h
index 78625c8..41d3281 100644
--- a/ptrlist.h
+++ b/ptrlist.h
@@ -46,6 +46,8 @@ 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?
  *
-- 
2.19.1




[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux