Skip to contentSkip to conversation

Introduce list_get() and list_get_tail()

RFC/PATCH Dmitry Torokhov 7 participants 8 messages Jul 8, 2006 – Jul 11, 2006

lore
Share & tools
lore
#0 lore raw

From: Dmitry Torokhov <[email protected]>

Add primitives to access first and last elements of a list instead of accessng pointers directly.

---

We have primitives to iterate over lists and to add/delete elements, why not for accessing head/tail?

 include/linux/list.h |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+)
include/linux/list.h +18 -0
Index: work/include/linux/list.h
===================================================================
@@ -571,6 +571,24 @@ static inline void list_splice_init(stru
 		prefetch(rcu_dereference((pos))->next), (pos) != (head); \
         	(pos) = (pos)->next)
 
+/**
+ * list_get - get first element in a list
+ * @head: the head of your list
+ */
+static inline struct list_head *list_get(struct list_head *head)
+{
+	return head->next;
+}
+
+/**
+ * list_get_tail - get last element in a list
+ * @head: the head of your list
+ */
+static inline struct list_head *list_get_tail(struct list_head *head)
+{
+	return head->prev;
+}
+
 /*
  * Double linked lists with a single pointer list head.
  * Mostly useful for hash tables where the two pointer list head is
#1 lore raw
Quoted 4 lines · Dmitry Torokhov

From: Dmitry Torokhov <[email protected]>

Add primitives to access first and last elements of a list instead of accessng pointers directly.

Wouldn't that be beter named list_first() and list_last() then? _get is like _do and usually not very descriptive.

-Andi

#2 lore raw
Quoted 9 lines · Andi Kleen
Quoted 4 lines · Dmitry Torokhov

From: Dmitry Torokhov <[email protected]>

Add primitives to access first and last elements of a list instead of accessng pointers directly.

Wouldn't that be beter named list_first() and list_last() then? _get is like _do and usually not very descriptive.

and _get tends to imply a reference count as well; I'm with Andi on this.. list_first() and list_last()

#3 lore raw

Le Sat, 08 Jul 2006 16:16:25 +0200, Arjan van de Ven a écrit :

Quoted 5 lines · Andi Kleen
Quoted 4 lines
Quoted 2 lines · Dmitry Torokhov

From: Dmitry Torokhov <[email protected]>

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Add primitives to access first and last elements of a list instead of accessng pointers directly.

Wouldn't that be beter named list_first() and list_last() then? _get is like _do and usually not very descriptive.

and _get tends to imply a reference count as well; I'm with Andi on this.. list_first() and list_last()

Yes from the name I would expect they remove the entry from the list...

#7 lore raw
In reply to Dmitry Torokhov

Am Saturday 08 July 2006 07:24 schrieb Dmitry Torokhov:

include/linux/list.h static inline void list_splice_init(stru · line 581

Quoted 8 lines
include/linux/list.h +8 -0
+/**
+ * list_get - get first element in a list
+ * @head: the head of your list
+ */
+static inline struct list_head *list_get(struct list_head *head)
+{
+       return head->next;
+}

I would expect it to be more useful when combined with list_entry(), something like

#define list_first_entry(list, type, member) \
	container_of((list)->next, type, member)
#define list_last_entry(list, type, member) \
	container_of((list)->prev, type, member)

	Arnd <><
#4 lore raw
In reply to Arjan van de Ven
Quoted 5 lines · Arjan van de Ven
Quoted 4 lines · Andi Kleen
Quoted 2 lines · Dmitry Torokhov

From: Dmitry Torokhov <[email protected]>

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Add primitives to access first and last elements of a list instead of accessng pointers directly.

Wouldn't that be beter named list_first() and list_last() then? _get is like _do and usually not very descriptive.

and _get tends to imply a reference count as well; I'm with Andi on this.. list_first() and list_last()

OK, so what about the following:

Subject: Introduce primitives to get first and last list elements

Introduce list_first() and list_last(); list_first_entry() and list_last_entry().

---

 include/linux/list.h |   40 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 40 insertions(+)
include/linux/list.h +40 -0
Index: work/include/linux/list.h
===================================================================
@@ -343,6 +343,26 @@ static inline void list_splice_init(stru
 	container_of(ptr, type, member)
 
 /**
+ * list_first_entry - get the struct for the first entry
+ * @head:	the &struct list_head pointer.
+ * @type:	the type of the struct this is embedded in.
+ * @member:	the name of the list_struct within the struct.
+ */
+#define list_first_entry(head, type, member) \
+	list_entry((head)->next, type, member)
+#define list_next_entry list_first_entry
+
+/**
+ * list_last_entry - get the struct for the last entry
+ * @head:	the &struct list_head pointer.
+ * @type:	the type of the struct this is embedded in.
+ * @member:	the name of the list_struct within the struct.
+ */
+#define list_last_entry(head, type, member) \
+	list_entry((head)->prev, type, member)
+#define list_prev_entry list_last_entry
+
+/**
  * list_for_each	-	iterate over a list
  * @pos:	the &struct list_head to use as a loop cursor.
  * @head:	the head for your list.
@@ -571,6 +591,26 @@ static inline void list_splice_init(stru
 		prefetch(rcu_dereference((pos))->next), (pos) != (head); \
         	(pos) = (pos)->next)
 
+/**
+ * list_first - get first element in a list
+ * @head: the head of your list
+ */
+#define list_next list_first
+static inline struct list_head *list_first(struct list_head *head)
+{
+	return head->next;
+}
+
+/**
+ * list_last - get last element in a list
+ * @head: the head of your list
+ */
+#define list_prev list_last
+static inline struct list_head *list_last(struct list_head *head)
+{
+	return head->prev;
+}
+
 /*
  * Double linked lists with a single pointer list head.
  * Mostly useful for hash tables where the two pointer list head is
#5 lore raw

On Sat, Jul 08, 2006 at 11:28:47PM -0400, Dmitry Torokhov wrote:
...

Quoted 1 line

+#define list_next_entry list_first_entry

list_next_entry, that sounds almost as something which given any list_head will give you the next entry. That's all fine until you give it the last list_head in the list - you'll try to use list_entry on a list_head that's not part of a struct but is the head of the list instead.

Quoted 1 line

+#define list_prev_entry list_last_entry

Ditto.

Jeff.

Signature · 1 line
Note 96.3% of all statistics are fiction.
#6 lore raw
Quoted 4 lines · Josef Sipek

On Sat, Jul 08, 2006 at 11:28:47PM -0400, Dmitry Torokhov wrote:
...

Quoted 1 line

+#define list_next_entry list_first_entry

Quoted 8 lines

list_next_entry, that sounds almost as something which given any list_head will give you the next entry. That's all fine until you give it the last list_head in the list - you'll try to use list_entry on a list_head that's not part of a struct but is the head of the list instead.

Quoted 1 line

+#define list_prev_entry list_last_entry

Ditto.

The same could be said about regular list_entry(), however we do have it and it is pretty useful.

Signature · 1 line
Dmitry