RE: [RFC PATCH 1/2] add list_for_each_entry_del()

From: David Laight
Date: Sat Oct 21 2023 - 06:00:09 EST


From: Miklos Szeredi
> Sent: 20 October 2023 11:29
>
> Add a new list iteration macro that always takes the first element of the
> list and removes it, until the list is empty.
...
> +/**
> + * list_for_each_entry_del - iterate list and remove elements
> + * @pos: the type * to use as a loop cursor.
> + * @head: the head for your list.
> + * @member: the name of the list_head within the struct.
> + *
> + * Delete first element from list before the iteration. Iterate until
> + * the list is empty.
> + */
> +#define list_for_each_entry_del(pos, head, member) \
> + while (!list_empty(head) && \
> + ({ pos = list_first_entry(head, typeof(*(pos)), member);\
> + list_del(&(pos)->member); 1; }))

That pattern is actually pretty obvious.
Add another wrapper and people end up having to read the
header to find out what it does.

I can't help feeling that if you look inside list_empty(),
list_first_entry() and list_del() there is a lot of
replicated code and tests.

Adding a list_del_first() function that returns the deleted
item or NULL to optimise it might make more sense.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)