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

代码学习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);


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值