linuxカーネルlist_splice関数
原文に誤りがあり,既に修正した.
/*********************************
:
1. ( )
2. (head 2 3) + (add 5 6)
==> (head 5 6 2 3) // add
*********************************/
/* Join two lists. */
static inline void list_splice (list_t *add, list_t *head)
{
/* Do nothing if the list which gets added is empty. */
if (add != add->next)
{
add->next->prev = head; // 5 head
add->prev->next = head->next; // 6 2
head->next->prev = add->prev; // 2 6
head->next = add->next; // head 5
}
}
/* : list_head list_head ! list_splice() head ! */
#include "list.h"
#include
static LIST_HEAD (head);
static LIST_HEAD (head2);
typedef struct T
{ char *name;
list_t member; // pre, next
}type;
int main(int argc, char **argv)
{
type a,b,c,d;
list_t *ptr; //
type *t;
list_t *tmp = NULL;
a.name = "a";
b.name = "b";
c.name = "c";
d.name = "d";
list_add(&a.member, &head); //
list_add(&b.member, &head); //
list_add(&c.member, &head2); //
list_add(&d.member, &head2); //
list_splice(&head2, &head);
#if 0
//list_for_each_prev(ptr, &head) safe
list_for_each_prev_safe(ptr, tmp, &head)
{ t = list_entry(ptr, type, member);
printf("%s
", t->name);
list_del(ptr);
}
#endif
/* :
book@gui_hua_shu:~/test$ ./a.out
a
b
c
d
*/
#if 1
//list_for_each(ptr, &head) safe
list_for_each_safe(ptr, tmp, &head)
{ t = list_entry(ptr, type, member);
printf("%s
", t->name);
list_del(ptr);
}
#endif
/* :
book@gui_hua_shu:~/test$ ./a.out
d
c
b
a
*/
return 0;
}