diff mbox

[1/2] list: add list_del_each_entry

Message ID 20130705204129.GB15943@logfs.org (mailing list archive)
State New, archived
Headers show

Commit Message

Jörn Engel July 5, 2013, 8:41 p.m. UTC
I have seen a lot of boilerplate code that either follows the pattern of
	while (!list_empty(head)) {
		pos = list_entry(head->next, struct foo, list);
		list_del(pos->list);
		...
	}
or some variant thereof.

With this patch in, people can use
	list_del_each_entry(pos, head, list) {
		...
	}

The patch also adds a list_del_each variant, even though I have
only found a single user for that one so far.

Signed-off-by: Joern Engel <joern@logfs.org>
---
 include/linux/list.h |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Filipe Manana July 5, 2013, 10:38 p.m. UTC | #1
On Fri, Jul 5, 2013 at 9:41 PM, Jörn Engel <joern@logfs.org> wrote:
> I have seen a lot of boilerplate code that either follows the pattern of
>         while (!list_empty(head)) {
>                 pos = list_entry(head->next, struct foo, list);
>                 list_del(pos->list);
>                 ...
>         }
> or some variant thereof.
>
> With this patch in, people can use
>         list_del_each_entry(pos, head, list) {
>                 ...
>         }
>
> The patch also adds a list_del_each variant, even though I have
> only found a single user for that one so far.
>
> Signed-off-by: Joern Engel <joern@logfs.org>
> ---
>  include/linux/list.h |   18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
>
> diff --git a/include/linux/list.h b/include/linux/list.h
> index 6a1f8df..ab39c7d 100644
> --- a/include/linux/list.h
> +++ b/include/linux/list.h
> @@ -557,6 +557,24 @@ static inline void list_splice_tail_init(struct list_head *list,
>  #define list_safe_reset_next(pos, n, member)                           \
>         n = list_entry(pos->member.next, typeof(*pos), member)
>
> +/**
> + * list_del_each - removes an entry from the list until it is empty
> + * @pos:       the &struct list_head to use as a loop cursor.
> + * @head:      the head of your list.
> + */
> +#define list_del_each(pos, head) \
> +       while (list_empty(head) ? 0 : (pos = (head)->next, list_del(pos), 1))
> +
> +/**
> + * list_del_each_entry - removes an entry from the list until it is empty
> + * @pos:       the type * to use as loop cursor.
> + * @head:      the head of your list.
> + * @member:    the name of the list_struct within the struct
> + */
> +#define list_del_each_entry(pos, head, member) \
> +       while (list_empty(head) && (pos = list_first_entry((head), \
> +               typeof(*pos), member), list_del((head)->next), 1))
> +

Shouldn't it be while (!list_empty(head) ... ?
(not operator addition)

thanks

>  /*
>   * Double linked lists with a single pointer list head.
>   * Mostly useful for hash tables where the two pointer list head is
> --
> 1.7.10.4
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html



--
Filipe David Manana,

"Reasonable men adapt themselves to the world.
 Unreasonable men adapt the world to themselves.
 That's why all progress depends on unreasonable men."
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jörn Engel July 15, 2013, 5:35 p.m. UTC | #2
On Fri, 5 July 2013 23:38:01 +0100, Filipe David Manana wrote:
>
> > +#define list_del_each_entry(pos, head, member) \
> > +       while (list_empty(head) && (pos = list_first_entry((head), \
> > +               typeof(*pos), member), list_del((head)->next), 1))
> > +
> 
> Shouldn't it be while (!list_empty(head) ... ?
> (not operator addition)

Obviously!  Now where is that brown paperbag...

Will resend in a few days, including the _init variants dchinner asked
for.

Jörn

--
Computer system analysis is like child-rearing; you can do grievous damage,
but you cannot ensure success."
-- Tom DeMarco
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/include/linux/list.h b/include/linux/list.h
index 6a1f8df..ab39c7d 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -557,6 +557,24 @@  static inline void list_splice_tail_init(struct list_head *list,
 #define list_safe_reset_next(pos, n, member)				\
 	n = list_entry(pos->member.next, typeof(*pos), member)
 
+/**
+ * list_del_each - removes an entry from the list until it is empty
+ * @pos:	the &struct list_head to use as a loop cursor.
+ * @head:	the head of your list.
+ */
+#define list_del_each(pos, head) \
+	while (list_empty(head) ? 0 : (pos = (head)->next, list_del(pos), 1))
+
+/**
+ * list_del_each_entry - removes an entry from the list until it is empty
+ * @pos:	the type * to use as loop cursor.
+ * @head:	the head of your list.
+ * @member:	the name of the list_struct within the struct
+ */
+#define list_del_each_entry(pos, head, member) \
+	while (list_empty(head) && (pos = list_first_entry((head), \
+		typeof(*pos), member), list_del((head)->next), 1))
+
 /*
  * Double linked lists with a single pointer list head.
  * Mostly useful for hash tables where the two pointer list head is