代码学习inux内核驱动(一)

    技术2026-03-18  7

    代码学习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> /****************************************************** LIST_HEAD(name) void INIT_LIST_HEAD(struct list_head *list) void list_add(struct list_head *new, struct list_head *head) void list_add_tail(struct list_head *new, struct list_head *head) list_del(struct list_head *entry) list_replace(struct list_head *old,struct list_head *new) void list_move(struct list_head *list, struct list_head *head) list_move_tail(struct list_head *list, struct list_head *head) list_is_last(const struct list_head *list,const struct list_head *head) int list_empty(const struct list_head *head) #define list_entry(ptr, type, member) container_of(ptr, type, member) #define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member) #define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member) #define list_next_entry(pos, member) list_entry((pos)->member.next, typeof(*(pos)), member) #define list_prev_entry(pos, member) list_entry((pos)->member.prev, typeof(*(pos)), member) #define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next) #define list_for_each_safe(pos, n, head) for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next) #define list_for_each_entry(pos, head, member) ******************************************************/ 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_del(p); 下次循环报错 } 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);
    Processed: 0.009, SQL: 9