内核链表实现及编程

内核链表

         -------Dream

头文件:include/linux/list.h

 

内核链表数据结构:

struct list_head

{

       struct list_head *next,*prev;

};  //内核的链表具备双链表功能,是双向循环链表

 

链表操作

1.初始化链表头

INIT_LIST_HEAD(struct list_head *list);

 

/*

static inline void INIT_LIST_HEAD(struct list_head *list)

{

       list->next = list;

       list->prev = list;

}

*/

 

2.插入节点

list_add(struct list_head *new,struct list_head_head *head);

list_add_tail(struct list_head *new,struct list_head *head);

 

/*

static inline void list_add(struct list_head *new, struct list_head *head)

{

       __list_add(new, head, head->next);

}

static inline void __list_add(struct list_head *new,

                           struct list_head *prev, struct list_head *next)

{

       next->prev = new;

       new->next = next;

       new->prev = prev;

       prev->next = new;

}

*/

 

3.删除节点

list_del(struct list_head *entry);

 

/*

static inline void list_del(struct list_head *entry)

{

       __list_del(entry->prev, entry->next);

       entry->next = (void *)0xDEADBEEF;

       entry->prev = (void *)0xBEEFDEAD;

}

static inline void __list_del(struct list_head *prev, struct list_head *next)

{

       next->prev = prev;

       prev->next = next;

}

*/

 

4.提取数据结构

list_entry(ptr,type,member); //ptr已知数据结构节点的指针,member所对应结构的成员名

 

/*

#define list_entry(ptr, type, member) \

       container_of(ptr, type, member)

*/

 

5.链表的遍历

list_for_each(struct list_head *pos,struct list_head *head);

 

/*

#define list_for_each(pos, head) \

       for (pos = (head)->next; prefetch(pos->next), pos != (head); \

              pos = pos->next)

例如:

struct list_head *entry;

struct list_head cs46xx_devs;  //链表头

list_for_each(entry,&cs46xx_devs){

       card=list_entry(entry,struct cs_cadr,list);

       if(card->dev==minor)

              break;

}

*/

 

 

内核链表操作实例:

#include <linux/list.h>

MODULE_LICENST("GPL");

 

struct student

{

       char name[100];

       int num;

       struct list_head list;

};

 

struct student *pstudent;

struct student *tmp_student;

struct list_head student_list;

struct list_head *pos;

 

int mylist_init()

{

 

       int i=0;

       INIT_LIST_HEAD(&student_list);  //初始化内核链表头

 

       pstudent=kmalloc(sizeof(struct student)*5,GFP_KERNEL); //为这个结构体分配存储空间

       memset(pstudent,0,sizeof(sturct student)*5);  //清空

 

       for(int i=0;i<5;i++)

       {

              sprintf(pstuent[i].name,"student%d",i+1);

              pstudent[i].num=i+1;

              list_add(&(pstudent[i].list),&student_list);  //student_list中插入节点

       }

 

       list_for_each(pos,&student_list)  //遍历链表

       {

              tmp_student=list_entry(pos,struct student,list);

              printk("student %d name:%s\n",tmp_student->num,tmp_student->name);

       }

 

       return 0;

}

 

void mylist_exit()

{

       int i;

       for(i=0;i<5;i++)

       {

              list_del(&(pstudent[i].list));  //删除内核链表

       }

       kfree(pstudent);  //释放节点

}

 

module_init(mylist_init);

module_exit(mylist_exit);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值