public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] list: add list_last_entry macros
@ 2011-02-27 10:46 Jiri Pirko
  2011-02-28 19:33 ` Paul E. McKenney
  0 siblings, 1 reply; 2+ messages in thread
From: Jiri Pirko @ 2011-02-27 10:46 UTC (permalink / raw)
  To: linux-kernel

Handy to get last item in a list. Also corrected parameter name for
list_first_entry.

Signed-off-by: Jiri Pirko <jpirko@redhat.com>

diff --git a/include/linux/list.h b/include/linux/list.h
index 3a54266..e10862e 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -352,14 +352,25 @@ static inline void list_splice_tail_init(struct list_head *list,
 
 /**
  * list_first_entry - get the first element from a list
- * @ptr:	the list head to take the element from.
+ * @head:	the list head to take the element from.
  * @type:	the type of the struct this is embedded in.
  * @member:	the name of the list_struct within the struct.
  *
  * Note, that list is expected to be not empty.
  */
-#define list_first_entry(ptr, type, member) \
-	list_entry((ptr)->next, type, member)
+#define list_first_entry(head, type, member) \
+	list_entry((head)->next, type, member)
+
+/**
+ * list_last_entry - get the last element from a list
+ * @head:	the list head to take the element from.
+ * @type:	the type of the struct this is embedded in.
+ * @member:	the name of the list_struct within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_last_entry(head, type, member) \
+	list_entry((head)->prev, type, member)
 
 /**
  * list_for_each	-	iterate over a list
diff --git a/include/linux/rculist.h b/include/linux/rculist.h
index 2dea94f..2cfa99f 100644
--- a/include/linux/rculist.h
+++ b/include/linux/rculist.h
@@ -229,7 +229,7 @@ static inline void list_splice_init_rcu(struct list_head *list,
 
 /**
  * list_first_entry_rcu - get the first element from a list
- * @ptr:        the list head to take the element from.
+ * @head:       the list head to take the element from.
  * @type:       the type of the struct this is embedded in.
  * @member:     the name of the list_struct within the struct.
  *
@@ -238,8 +238,23 @@ static inline void list_splice_init_rcu(struct list_head *list,
  * This primitive may safely run concurrently with the _rcu list-mutation
  * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
  */
-#define list_first_entry_rcu(ptr, type, member) \
-	list_entry_rcu((ptr)->next, type, member)
+#define list_first_entry_rcu(head, type, member) \
+	list_entry_rcu((head)->next, type, member)
+
+/**
+ * list_last_entry_rcu - get the last element from a list
+ * @head:       the list head to take the element from.
+ * @type:       the type of the struct this is embedded in.
+ * @member:     the name of the list_struct within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ *
+ * This primitive may safely run concurrently with the _rcu list-mutation
+ * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
+ */
+
+#define list_last_entry_rcu(head, type, member) \
+	list_entry_rcu((head)->prev, type, member)
 
 /**
  * list_for_each_entry_rcu	-	iterate over rcu list of given type

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] list: add list_last_entry macros
  2011-02-27 10:46 [PATCH] list: add list_last_entry macros Jiri Pirko
@ 2011-02-28 19:33 ` Paul E. McKenney
  0 siblings, 0 replies; 2+ messages in thread
From: Paul E. McKenney @ 2011-02-28 19:33 UTC (permalink / raw)
  To: Jiri Pirko; +Cc: linux-kernel

On Sun, Feb 27, 2011 at 11:46:21AM +0100, Jiri Pirko wrote:
> Handy to get last item in a list. Also corrected parameter name for
> list_first_entry.
> 
> Signed-off-by: Jiri Pirko <jpirko@redhat.com>
> 
> diff --git a/include/linux/list.h b/include/linux/list.h
> index 3a54266..e10862e 100644
> --- a/include/linux/list.h
> +++ b/include/linux/list.h
> @@ -352,14 +352,25 @@ static inline void list_splice_tail_init(struct list_head *list,
> 
>  /**
>   * list_first_entry - get the first element from a list
> - * @ptr:	the list head to take the element from.
> + * @head:	the list head to take the element from.
>   * @type:	the type of the struct this is embedded in.
>   * @member:	the name of the list_struct within the struct.
>   *
>   * Note, that list is expected to be not empty.
>   */
> -#define list_first_entry(ptr, type, member) \
> -	list_entry((ptr)->next, type, member)
> +#define list_first_entry(head, type, member) \
> +	list_entry((head)->next, type, member)
> +
> +/**
> + * list_last_entry - get the last element from a list
> + * @head:	the list head to take the element from.
> + * @type:	the type of the struct this is embedded in.
> + * @member:	the name of the list_struct within the struct.
> + *
> + * Note, that list is expected to be not empty.
> + */
> +#define list_last_entry(head, type, member) \
> +	list_entry((head)->prev, type, member)
> 
>  /**
>   * list_for_each	-	iterate over a list
> diff --git a/include/linux/rculist.h b/include/linux/rculist.h
> index 2dea94f..2cfa99f 100644
> --- a/include/linux/rculist.h
> +++ b/include/linux/rculist.h
> @@ -229,7 +229,7 @@ static inline void list_splice_init_rcu(struct list_head *list,
> 
>  /**
>   * list_first_entry_rcu - get the first element from a list
> - * @ptr:        the list head to take the element from.
> + * @head:       the list head to take the element from.
>   * @type:       the type of the struct this is embedded in.
>   * @member:     the name of the list_struct within the struct.
>   *
> @@ -238,8 +238,23 @@ static inline void list_splice_init_rcu(struct list_head *list,
>   * This primitive may safely run concurrently with the _rcu list-mutation
>   * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
>   */
> -#define list_first_entry_rcu(ptr, type, member) \
> -	list_entry_rcu((ptr)->next, type, member)
> +#define list_first_entry_rcu(head, type, member) \
> +	list_entry_rcu((head)->next, type, member)
> +
> +/**
> + * list_last_entry_rcu - get the last element from a list
> + * @head:       the list head to take the element from.
> + * @type:       the type of the struct this is embedded in.
> + * @member:     the name of the list_struct within the struct.
> + *
> + * Note, that list is expected to be not empty.
> + *
> + * This primitive may safely run concurrently with the _rcu list-mutation
> + * primitives such as list_add_rcu() as long as it's guarded by rcu_read_lock().
> + */
> +
> +#define list_last_entry_rcu(head, type, member) \
> +	list_entry_rcu((head)->prev, type, member)

But won't a concurrent list_del_rcu() on head, which removes the full
list, poison the ->prev pointer?

Do you have a definite use case for list_last_entry_rcu()?

							Thanx, Paul

>  /**
>   * list_for_each_entry_rcu	-	iterate over rcu list of given type
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-02-28 19:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-27 10:46 [PATCH] list: add list_last_entry macros Jiri Pirko
2011-02-28 19:33 ` Paul E. McKenney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox