[PATCH v6 02/11] kbuild: Add list_size, list_at_index, list_for_each_from

Ole Schuerks posted 11 patches 1 year, 3 months ago
[PATCH v6 02/11] kbuild: Add list_size, list_at_index, list_for_each_from
Posted by Ole Schuerks 1 year, 3 months ago
list_size counts the number of entries. list_at_index retrieves the
entry at an index. list_for_each_from iterates over a list from some
entry to the end.

Signed-off-by: Ole Schuerks <ole0811sch@gmail.com>
---
 scripts/include/list.h | 49 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/scripts/include/list.h b/scripts/include/list.h
index 8bdcaadca709..0c50774606c2 100644
--- a/scripts/include/list.h
+++ b/scripts/include/list.h
@@ -219,6 +219,21 @@ static inline int list_empty(const struct list_head *head)
 	return head->next == head;
 }
 
+/**
+ * list_size - counts the number of entries in a list
+ * @head: the list whose entries are counted
+ */
+static inline size_t list_size(const struct list_head *head)
+{
+	size_t ret = 0;
+
+	for (struct list_head *curr = head->next; curr != head;
+	     curr = curr->next)
+		++ret;
+
+	return ret;
+}
+
 /**
  * list_entry - get the struct for this entry
  * @ptr:	the &struct list_head pointer.
@@ -310,6 +325,40 @@ static inline int list_empty(const struct list_head *head)
 	     !list_entry_is_head(pos, head, member);			\
 	     pos = n, n = list_next_entry(n, member))
 
+/**
+ * list_for_each_entry_from - iterate over list of given type starting at a given node
+ * @pos:	the type * to use as a loop cursor.
+ * @start:	the node to start iterating at
+ * @head:	the head for your list.
+ * @member:	the name of the list_head within the struct.
+ */
+#define list_for_each_entry_from(pos, start, head, member)  \
+	for (pos = list_entry(start, typeof(*pos), member); \
+	     !list_entry_is_head(pos, head, member);        \
+	     pos = list_next_entry(pos, member))
+
+/**
+ * list_at_index - retrieve the entry at index i in O(n)
+ * @i:		index of entry to retrieve.
+ * @head:	the head for your list.
+ * @type:	the type of the struct the entries are embedded in.
+ * @member:	the name of the list_head within the struct.
+ */
+#define list_at_index(i, head, type, member)               \
+	({                                                 \
+		type *__pos;                               \
+		size_t __counter = 0;                      \
+		list_for_each_entry(__pos, head, member) { \
+			if (__counter++ == i)              \
+				break;                     \
+			if (__pos->member.next == head) {  \
+				__pos = NULL;              \
+				break;                     \
+			}                                  \
+		}                                          \
+		__pos;                                     \
+	})
+
 /*
  * Double linked lists with a single pointer list head.
  * Mostly useful for hash tables where the two pointer list head is
-- 
2.39.5
Re: [PATCH v6 02/11] kbuild: Add list_size, list_at_index, list_for_each_from
Posted by Luis Chamberlain 1 year, 2 months ago
On Mon, Oct 28, 2024 at 04:49:40AM +0100, Ole Schuerks wrote:
> +/**
> + * list_size - counts the number of entries in a list
> + * @head: the list whose entries are counted
> + */
> +static inline size_t list_size(const struct list_head *head)
> +{
> +	size_t ret = 0;
> +
> +	for (struct list_head *curr = head->next; curr != head;
> +	     curr = curr->next)
> +		++ret;
> +
> +	return ret;

What's wrong with list_count_nodes()?

> @@ -310,6 +325,40 @@ static inline int list_empty(const struct list_head *head)
>  	     !list_entry_is_head(pos, head, member);			\
>  	     pos = n, n = list_next_entry(n, member))
>  
> +/**
> + * list_for_each_entry_from - iterate over list of given type starting at a given node
> + * @pos:	the type * to use as a loop cursor.
> + * @start:	the node to start iterating at
> + * @head:	the head for your list.
> + * @member:	the name of the list_head within the struct.
> + */
> +#define list_for_each_entry_from(pos, start, head, member)  \
> +	for (pos = list_entry(start, typeof(*pos), member); \
> +	     !list_entry_is_head(pos, head, member);        \
> +	     pos = list_next_entry(pos, member))

list_for_each_entry_from() exists on my tree on linux-next added
through commit e229c2fb3370a ("[LIST]: Introduce
list_for_each_entry_from") since v2.6.17, so since 2006.

> +/**
> + * list_at_index - retrieve the entry at index i in O(n)
> + * @i:		index of entry to retrieve.
> + * @head:	the head for your list.
> + * @type:	the type of the struct the entries are embedded in.
> + * @member:	the name of the list_head within the struct.
> + */
> +#define list_at_index(i, head, type, member)               \
> +	({                                                 \
> +		type *__pos;                               \
> +		size_t __counter = 0;                      \
> +		list_for_each_entry(__pos, head, member) { \
> +			if (__counter++ == i)              \
> +				break;                     \
> +			if (__pos->member.next == head) {  \
> +				__pos = NULL;              \
> +				break;                     \
> +			}                                  \
> +		}                                          \
> +		__pos;                                     \
> +	})
> +

Seems like the only thing being added. I'd just keep thsi internal
to your code for now, and later if we really want we can add this
as a generic helper. If you really want this on list.h I'd recommend
to find a few users of this already in kernel and generalize the common
thing to use it first and add it that way. Then you separate *that*
patches series from this one, and address that first, and address
the addition then as a seprate patch series.

If you keep it to the tool itself, just change the namespace to be not
be as generically named of course.

  Luis
Re: [PATCH v6 02/11] kbuild: Add list_size, list_at_index, list_for_each_from
Posted by Ole Schuerks 1 year, 2 months ago
> What's wrong with list_count_nodes()?

> list_for_each_entry_from() exists on my tree on linux-next added
> through commit e229c2fb3370a ("[LIST]: Introduce
> list_for_each_entry_from") since v2.6.17, so since 2006.

These macros only exist in include/linux/list.h, not in
scripts/include/list.h. My assumption was that the build system should only
use scripts/include/list.h.

> I'd just keep thsi internal to your code for now, and later if we really
> want we can add this as a generic helper.

Alright, the additions will be internals in v7.

Best regards,
Ole Schuerks
Re: [PATCH v6 02/11] kbuild: Add list_size, list_at_index, list_for_each_from
Posted by Luis Chamberlain 1 year, 1 month ago
On Mon, Dec 09, 2024 at 02:00:19AM +0100, Ole Schuerks wrote:
> > What's wrong with list_count_nodes()?
> 
> > list_for_each_entry_from() exists on my tree on linux-next added
> > through commit e229c2fb3370a ("[LIST]: Introduce
> > list_for_each_entry_from") since v2.6.17, so since 2006.
> 
> These macros only exist in include/linux/list.h, not in
> scripts/include/list.h. My assumption was that the build system should only
> use scripts/include/list.h.

We can update scripts/include/list.h from include/linux/list.h. So just
say, bleh blah exists in bleh bleh bleh.

  Luis