[2.5] Single linked lists for Linux, overly complicated v2
PATCH 1/1 [2.5] Single linked lists for Linux, overly complicated v2
This time I've taken care of the various comments. It's not exactly been cool, but I saw where my details conflicted. NULL is supposed to be NULL.
One more for: slist_for_each_round()
slist_del*() is still overly complicated. Shall I resume the easy version of it?
include/linux/slist.h new file +154 -0
@@ -0,0 +1,154 @@ +#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 Bogk, Thunder from the hill +*/ + +#define INIT_SLIST_HEAD (name) \ + (name->next = name) + +#define SLIST_HEAD_INIT (name) \ + { .next = NULL; } + +#define SLIST_HEAD (type,name) \ + typeof(type) name = SLIST_HEAD_INIT(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_in, _head_in) \ +do { \ + typeof(_head_in) _head = _head_in, \ + _new = _new_in; \ + _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_in, _head_in) \ +do { \ + typeof(_head_in) _head = (_head_in), \ + _new = (_new_in); \ + _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 (_entry_in) \ +({ \ + typeof(_entry_in) _entry = (_entry_in), _head = \ + kmalloc(sizeof(_entry), GFP_KERNEL), _free; \ + if (_head) { \ + memcpy(_head, (_entry), sizeof(_entry)); \ + _free = (_entry); \ + (_entry) = (_entry)->next; \ + kfree(_free); \ + _head->next = NULL; \ + _head; \ + } else \ + NULL; \ +}) + +/** +* slist_del_init - remove an entry from list and initialize it +* @head: head to remove it from +* @entry: entry to be removed +*/ +#define slist_del_init (_entry) \ +({ \ + typeof(_entry_in) _entry = (_entry_in), _head = \ + kmalloc(sizeof(_entry), GFP_KERNEL), _free; \ + if (_head) { \ + memcpy(_head, (_entry), sizeof(_entry)); \ + _free = (_entry); \ + (_entry) = (_entry)->next; \ + kfree(_free); \ + _head->next = _head; \ + _head; \ + } else \ + NULL; \ +}) + +/** +* 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_in) ({ \ + typeof(_list_in) _list = (_list_in), \ + _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_round - iterate over a round 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 && pos != head && \ + ({ 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
include/linux/slist.h line 22
Quoted 8 lines · Andreas Räcker
include/linux/slist.h new file +8
-0+#define INIT_SLIST_HEAD (name) \ + (name->next = name) + +#define SLIST_HEAD_INIT (name) \ + { .next = NULL; } + +#define SLIST_HEAD (type,name) \ + typeof(type) name = SLIST_HEAD_INIT(name)
INIT_SLIST_HEAD still has the old behaviour...
include/linux/slist.h line 53
Quoted 3 lines
include/linux/slist.h new file +2
-0+#define slist_add_front (_new_in, _head_in) \ +#define slist_add(_new_in, _head_in) \
These two seem to be exactly the same, surely you only need one ?
Quoted 1 line
+#define
slist_del(_entry_in)\
And what happens when you try to remove an entry from the middle of the list ?
Also, how do you know which list the entry is removed from ?
include/linux/slist.h line 131
Quoted 8 lines
include/linux/slist.h new file +8
-0+/** +* 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)
Are you sure the list head _can_ be the first entry ?
Not having the head of the list in a known place (ie. a fixed list head) can make a list very hard to find.
include/linux/slist.h line 138
Quoted 6 lines
include/linux/slist.h new file +6
-0+/** +* slist_for_each_round - iterate over a round 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) \
You forgot to rename this define.
Quoted 3 lines
--
Lightweight Patch Manager, without pine.
If you have any objections (apart from who I am), tell me
^^^^^^^^^^^^^^^^^^^
I guess that's why we have whois ;)
cheers,
Rik
Signature · 4 lines
A: No. Q: Should I include quotations after my reply? http://www.surriel.com/ http://distro.conectiva.com/
Hi,
Quoted 1 line · Rik van Riel
INIT_SLIST_HEADstill has the old behaviour...
I'm now after both behaviors...
#define INIT_SLIST_HEAD(name) \
(name->next = NULL)#define INIT_SLIST_LOOP(name) \ (name->next = name)
Quoted 5 lines
include/linux/slist.h line 53
Quoted 3 lines
include/linux/slist.h new file +2
-0+#define slist_add_front (_new_in, _head_in) \ +#define slist_add(_new_in, _head_in) \These two seem to be exactly the same, surely you only need one ?
No, they're not.
(tab-width=8)
slist_add
|-------------------------------| | head -> after | | | | new | |-------------------------------| new->next = head->next | head -> after | | ^ | | new | |-------------------------------| head->next = new | head -> new -> after | |-------------------------------|
slist_add_front
|-------------------------------| | head -> after | | | | new | |-------------------------------| new->next = head | new -> head -> after | |-------------------------------| head = new | head -> next -> after | |-------------------------------|
(Just to have something drawn...)
Quoted 4 lines
Quoted 1 line
+#define
slist_del(_entry_in)\And what happens when you try to remove an entry from the middle of the list ?
Well, I can only try to preserve the pointer target, since I don't have a previous entry. (Thus the overly complicated slist_del.)
Quoted 1 line
Also, how do you know which list the entry is removed from ?
It's the one which previously contained it...
I don't know whether I should like the list header aproach.
It's not bad for either circular lists or such which will have to be gone through only once, as using slist_pop().
Quoted 2 lines
Not having the head of the list in a known place (ie. a fixed list head) can make a list very hard to find.
But you see we have the problem that there is no such thing as a predeclared structure for it. The only thing we can rely on is a chain of structures which alltogether have a ->next field pointing to another structure of presumably the same type.
Quoted 1 line
You forgot to rename this define.
Yes, I've forgotten two things there. They are fixed in my file, which I won't post right now (in order not to pollute the list too much with patches. It's that fix plus a forgotten _in.)
Quoted 3 lines
Quoted 1 line
If you have any objections (apart from who I am), tell me
^^^^^^^^^^^^^^^^^^^I guess that's why we have whois ;)
Oh, that was just for Jes Soerensen, who kept asking.
Thunder
Signature · 1 line
assert(typeof((fool)->next) == typeof(fool)); /* wrong */