代码学习inux内核驱动(一)
list_head
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/types.h>
MODULE_AUTHOR("xyzeng");
MODULE_LICENSE("Dual BSD/GPL");
struct hello_st {
char str[32];
int order;
struct list_head node;
};
LIST_HEAD(hello_head);
static int code_case_list_init(void)
{
struct list_head *p = NULL;
struct list_head *ps = NULL;
struct hello_st *phello = NULL;
struct hello_st hello1 = {.str="hello1",.order = 0};
struct hello_st hello2 = {.str="hello2",.order = 1};
struct hello_st hello3 = {.str="hello3",.order = 2};
struct hello_st hello4 = {.str="hello4",.order = 3};
struct hello_st hello5 = {.str="hello5",.order = 4};
struct hello_st hello6 = {.str="hello6",.order = 5};
list_add(&hello1.node,&hello_head);
list_add_tail(&hello2.node,&hello_head);
list_add_tail(&hello3.node,&hello_head);
list_add_tail(&hello4.node,&hello_head);
list_add(&hello5.node,&hello_head);
phello = list_next_entry(&hello1,node);
printk("zeng0 list_next_entry:str=%s,order=%d\n",phello->str,phello->order);
phello = list_first_entry(&hello_head,struct hello_st,node);
printk("zeng1 list_first_entry:str=%s,order=%d\n",phello->str,phello->order);
list_for_each(p,&hello_head){
phello = list_entry(p,struct hello_st , node);
printk("zeng2 :str=%s,order=%d\n",phello->str,phello->order);
}
list_for_each_entry(phello,&hello_head,node){
printk("zeng3 :str=%s,order=%d\n",phello->str,phello->order);
}
list_del(&hello3.node);
list_for_each_safe(p,ps,&hello_head){
phello = list_entry(p,struct hello_st , node);
printk("zeng4 :str=%s,order=%d\n",phello->str,phello->order);
}
list_replace(&hello4.node,&hello6.node);
list_for_each_entry(phello,&hello_head,node){
printk("zeng5 :str=%s,order=%d\n",phello->str,phello->order);
}
int i=0;
list_for_each(p,&hello_head){
printk("zeng66 :%d\n",i++);
if(list_is_last(p,&hello_head)){
printk("zeng6, is last!\n");
break;
}
phello = list_entry(p,struct hello_st , node);
printk("zeng66 :str=%s,order=%d\n",phello->str,phello->order);
}
list_for_each_safe(p,ps,&hello_head){
phello = list_entry(p,struct hello_st , node);
printk("zeng7 :str=%s,order=%d\n",phello->str,phello->order);
list_del(p);
}
if( list_empty(&hello_head) ) printk("zeng8,hello_head is empty!\n");
return 0;
}
static void code_case_list_exit(void)
{
}
module_init(code_case_list_init);
module_exit(code_case_list_exit);