您的位置:首页 > 运维架构 > Linux

linux内核链表的使用

2015-08-13 20:58 549 查看
linux内核链表:
链表通常包括两个域:数据域和指针域。
struct list_head{
struct list_head *next,*prev;
};
include/linux/list.h中实现了一套精彩的链表数据结构。
传统的链表指针指向下一个节点的头部。linux链表指针指向下一个指针list_head结构(*next),双向循环。不会随着外部数据的变化而变化,使它具有通用性。?

-------------------------------------------------------------------

linux内核提供的链表主要操作:
1.初始化链表头:
INIT_LIST_HEAD(list_head *head):将*head,*prev指向list本身。
2.插入节点
list_add(struct list_head *new,struct list_head *head)
list_add_tail(struct list_head *new,struct list_head *head)
3.删除节点
list_del(struct list_head *entry:一个指针结构体)
4.提取数据结构
list_entry(ptr(指向list_head的指针),type(外部结构的类型),member(struct list_head对应的成员名))
已知数据结构中的节点指针ptr,找出数据结构,例
list_entry(aup,struct,autofs,list)
5.遍历
list_for_each(struct list_head *pos,struct list_head *head(你所需要遍历的链表的链表头))
如:
struct list_head *entry;
struct list_head cs46xx_devs;//链表头
list_for_each(entry,&cs46xx_devs){
card = list_entry(entry,struct cs_card,list);
if(card->dev_midi == minor)
break;
}
看内核代码看list_entry是如何实现的???

----------------------------------------------------------

内核链表的一个具体实现实例:

#include<linux/list.h>

#include<linux/module.h>

#include<linux/kernel.h>

#include<linux/init.h>

#include<linux/slab.h> //kfree kmalloc

MODULE_LICENSE("GPL");//(general public license)
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(struct student)*5);//初始化
for(i=0;i<5;i++){
sprintf(pstudent[i].name,"student%d",i+1);
pstudent[i].num=i+1;
list_add(&(pstudent[i].list),&student_list);

}//循环插入学生信息
list_for_each(pos,&student_list)
{
tmp_student = list_entry(pos,struct student,list);
printk("<0>student%d name:%s\n",tmp_student->num,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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: