Re: [PATCH v2] list: Add a macro to test if entry is pointing to the head

From: Rojewski, Cezary
Date: Tue Sep 29 2020 - 10:40:33 EST


On 2020-09-29 3:43 PM, Andy Shevchenko wrote:
> Add a macro to test if entry is pointing to the head of the list
> which is useful in cases like:
>
> list_for_each_entry(pos, &head, member) {
> if (cond)
> break;
> }
> if (list_entry_is_head(pos, &head, member))
> return -ERRNO;
>
> that allows to avoid additional variable to be added to track if loop
> has not been stopped in the middle.
>
> While here, convert list_for_each_entry*() family of macros to use a new one.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
> ---
> v2: converted users inside list.h, dropped ambiguous description

Reviewed-by: Cezary Rojewski <cezary.rojewski@xxxxxxxxx>

This is a good addition to the list of helper macros found in list.h.

When looking at below:

> /**
> * list_for_each_entry - iterate over list of given type
> * @pos: the type * to use as a loop cursor.
> @@ -617,7 +626,7 @@ static inline void list_splice_tail_init(struct list_head *list,
> */
> #define list_for_each_entry(pos, head, member) \
> for (pos = list_first_entry(head, typeof(*pos), member); \
> - &pos->member != (head); \
> + !list_entry_is_head(pos, head, member); \
> pos = list_next_entry(pos, member))
>

it seems such helper should have been here a long time ago (notice the
usage of helpers for initial assignment and cursor update while the
loop-exit check was devoid of such).

Czarek