Skip to contentSkip to conversation

[2.5] Single linked lists for Linux,v2

PATCH In review Lightweight Patch Manager 4 participants 8 messages Sep 25, 2002 – Sep 26, 2002

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 [2.5] Single linked lists for Linux,v2

Again single linked lists...

include/linux/slist.h new file +109 -0
@@ -0,0 +1,109 @@
+#ifdef __KERNEL__
+#ifndef _LINUX_SLIST_H
+#define _LINUX_SLIST_H
+
+#include <asm/processor.h>
+
+/*
+ * Type-safe single linked list helper-functions.
+ * (originally taken from list.h)
+ *
+ * Thomas 'Dent' Mirlacher, Daniel Phillips,
+ * Andreas Borgk, Thunder from the hill
+ */
+
+#define INIT_SLIST_HEAD(name)			\
+	(name->next = name)
+
+#define SLIST_HEAD(type,name)			\
+	typeof(type) name = INIT_SLIST_HEAD(name)
+
+/**
+ * slist_add_front - add a new entry at the first slot, moving the old head
+ *		     to the second slot
+ * @new:	new entry to be added
+ * @head:	head of the single linked list
+ *
+ * Insert a new entry before the specified head.
+ * This is good for implementing stacks.
+ */
+
+#define slist_add_front(_new, _head)		\
+do {						\
+	(_new)->next = (_head);			\
+	(_head) = (_new);			\
+} while (0)
+
+
+
+/**
+ * slist_add - add a new entry
+ * @new:       new entry to be added
+ * @head:      head of the single linked list
+ *
+ * Insert a new entry before the specified head.
+ * This is good for implementing stacks.
+ *
+ * Careful: if you do this concurrently, _head
+ * might get into nirvana...
+ */
+#define slist_add(_new, _head)			\
+do {						\
+	(_new)->next = (_head)->next;		\
+	(_head)->next = (_new);			\
+	(_new) = (_head);			\
+} while (0)
+
+/**
+ * slist_del -	remove an entry from list
+ * @head:	head to remove it from
+ * @entry:	entry to be removed
+ */
+#define slist_del(_head, _entry)		\
+do {						\
+	(_head)->next = (_entry)->next;		\
+	(_entry)->next = NULL;			\
+} while (0)
+
+/**
+ * slist_del_single -	untag a list from an entry
+ * @list:	list entry to be untagged
+ */
+#define slist_del_single(_list)			\
+	((_list)->next = NULL)
+
+/**
+ * slist_pop	-	pop out list entry
+ * @list:	entry to be popped out
+ *
+ * Pop out an entry from a list.
+ */
+#define slist_pop(_list) ({			\
+	typeof(_list) _NODE_ = _list;		\
+	if (_list) {				\
+	    (_list) = (_list)->next;		\
+	    _NODE_->next = NULL;		\
+	}					\
+	_NODE_; })
+
+/**
+ * slist_for_each	-	iterate over a list
+ * @pos:	the pointer to use as a loop counter.
+ * @head:	the head for your list (this is also the first entry).
+ */
+#define slist_for_each(pos, head)				\
+	for (pos = head; pos && ({ prefetch(pos->next); 1; });	\
+	    pos = pos->next)
+
+/**
+ * slist_for_each_del	-	iterate over a list, popping off entries
+ * @pos:       the pointer to use as a loop counter.
+ * @head:      the head for your list (this is also the first entry).
+ */
+#define slist_for_each_del(pos, head)			\
+	for (pos = slist_pop(head); pos &&		\
+    	    ({ prefetch(pos->next); 1; });		\
+	    pos = slist_pop(head))
+
+#endif /* _LINUX_SLIST_H */
+#endif /* __KERNEL__ */
Signature · 2 lines
Lightweight Patch Manager, without pine.
If you have any objections (apart from who I am), tell me
#1 lore raw

Hi,

Quoted 1 line · Lightweight Patch Manager

+ * Andreas Borgk, Thunder from the hill

That poor guy's name is Andreas Bogk, for chrissake...

Thunder

Signature · 1 line
assert(typeof((fool)->next) == typeof(fool));	/* wrong */
#2 lore raw
In reply to Lightweight Patch Manager
Quoted 1 line · Lightweight Patch Manager

Again single linked lists...

And again with errors ;)

include/linux/slist.h line 19

Quoted 5 lines
include/linux/slist.h new file +5 -0
+#define INIT_SLIST_HEAD(name)			\
+	(name->next = name)
+
+#define SLIST_HEAD(type,name)			\
+	typeof(type) name = INIT_SLIST_HEAD(name)

Fun, so the list head points to itself ...

include/linux/slist.h line 96

Quoted 3 lines
include/linux/slist.h new file +3 -0
+#define slist_for_each(pos, head)				\
+	for (pos = head; pos && ({ prefetch(pos->next); 1; });	\
+	    pos = pos->next)

... imagine what that would do in combination with this macro.

Rik

Signature · 5 lines
Bravely reimplemented by the knights who say "NIH".

http://www.surriel.com/		http://distro.conectiva.com/

Spamtraps of the month:  [email protected] [email protected]
#3 lore raw

Hi,

Quoted 8 lines · Rik van Riel

include/linux/slist.h line 19

Quoted 5 lines
include/linux/slist.h new file +5 -0
+#define INIT_SLIST_HEAD(name)			\
+	(name->next = name)
+
+#define SLIST_HEAD(type,name)			\
+	typeof(type) name = INIT_SLIST_HEAD(name)

Fun, so the list head points to itself ...

Quoted 5 lines

include/linux/slist.h line 96

Quoted 3 lines
include/linux/slist.h new file +3 -0
+#define slist_for_each(pos, head)				\
+	for (pos = head; pos && ({ prefetch(pos->next); 1; });	\
+	    pos = pos->next)

... imagine what that would do in combination with this macro.

I'm aware of that possibility. What would you initialize it to, if not the list itself? (And BTW, anyone have a solution for slist_add()?)

We could set it to NULL, but where would we end?

#define INIT_SLIST_HEAD(name)				\
	(name->next = NULL)

#define SLIST_HEAD_INIT(name) name

Thunder

Signature · 1 line
assert(typeof((fool)->next) == typeof(fool));	/* wrong */
#4 lore raw
Quoted 2 lines · Thunder from the hill

I'm aware of that possibility. What would you initialize it to, if not the list itself? (And BTW, anyone have a solution for slist_add()?)

If I were you, I'd take a large piece of paper and make a drawing of what the data structure looks like and what the various macros/functions are supposed to do.

That should make things easier.

regards,

Rik

Signature · 4 lines
A: No.
Q: Should I include quotations after my reply?

http://www.surriel.com/		http://distro.conectiva.com/
#5 lore raw

Hi,

Quoted 3 lines · Rik van Riel

If I were you, I'd take a large piece of paper and make a drawing of what the data structure looks like and what the various macros/functions are supposed to do.

Well, I know what they _should_ do. But I don't know what I should initialize an empty list entry to. That's not got much spam in it...

Thunder

Signature · 1 line
assert(typeof((fool)->next) == typeof(fool));	/* wrong */
#6 lore raw
Quoted 9 lines · Thunder from the hill

Hi,

Quoted 3 lines · Rik van Riel

If I were you, I'd take a large piece of paper and make a drawing of what the data structure looks like and what the various macros/functions are supposed to do.

Well, I know what they _should_ do. But I don't know what I should initialize an empty list entry to. That's not got much spam in it...

Try with NULL.

Nicolas

#7 lore raw
In reply to Thunder from the hill
Quoted 7 lines · Thunder from the hill
Quoted 3 lines · Rik van Riel

If I were you, I'd take a large piece of paper and make a drawing of what the data structure looks like and what the various macros/functions are supposed to do.

Well, I know what they _should_ do. But I don't know what I should initialize an empty list entry to.

And now you contradict yourself ;)

Please make a detailed picture of the data structure on a piece of paper so you can check the code against the picture and see if it really is doing the right thing.

Rik

Signature · 4 lines
A: No.
Q: Should I include quotations after my reply?

http://www.surriel.com/		http://distro.conectiva.com/