Linux内核设备驱动之内核中链表的使用笔记整理
作者:Engineer-Bruce_Yang 时间:2021-09-15 10:24:32
/********************
* 内核中链表的应用
********************/
(1)介绍
在Linux内核中使用了大量的链表结构来组织数据,包括设备列表以及各种功能模块中的数据组织。这些链表大多采用在include/linux/list.h实现的一个相当精彩的链表数据结构。
链表数据结构的定义很简单:
struct list_head {
struct list_head *next, *prev;
};
list_head结构包含两个指向list_head结构的指针prev和next,内核的数据结构通常组织成双循环链表。
和以前介绍的双链表结构模型不同,这里的list_head没有数据域。在Linux内核链表中,不是在链表结构中包含数据,而是在数据结构中包含链表节点。如:
struct my_struct{
struct list_head list;
unsigned long dog;
void *cat;
};
linux中的链表没有固定的表头,从任何元素开始访问都可以。遍历链表仅仅需要从某个节点开始,沿指针访问下一个节点,直到又重新回到最初这个节点就可以了。每个独立的节点都可以被称作是链表头。
(2)链表的初始化
a.静态
如果在编译时静态创建链表,并且直接引用它,如下:
struct my_struct mine={
.lost = LIST_HEAD_INIT(mine.list);
.dog = 0,
.cat = NULL
};
//或
static LIST_HEAD(fox);
/*等于struct list_head fox = LIST_HEAD_INIT(fox); */
b.动态
struct my_struct *p;
p = kmalloc(GFP_KERNEL, sizeof(my_struct));
p->dog = 0;
p->cat = NULL;
INIT_LIST_HEAD(&p->list);
(3)操作链表
内核提供了一组函数来操作链表。
注意!这些函数都使用一个或多个list_head结构体指针作参数。定义在<linux/list.h>
a.增加节点
list_add(struct list_head *new,
struct list_head *head);
//向指定链表的head节点后面插入new节点
b.把节点增加到链表尾
list_add_tail(struct list_head *new,
struct list_head *head);
//向指定链表的head节点前面插入new节点
c.从链表删除一个节点
list_del(struct list_head *entry);
//将entry从链表中移走
d.把节点从一个链表移到另一个链表
list_move(struct list_head *list,
struct list_head *head);
从一个链表中摘除list项,然后将其插入head的后面
e.list_empty(struct list_head *head);
链表为空返回非0值,否则返回0
f.合并链表
list_splice(struct list_head *list,
struct list_head *head);
//注意!新的链表不包括list节点
(4)遍历链表
链表本身不重要,访问到那个包含链表的结构体才重要
a.从链表指针获得包含该链表的结构体的指针
list_entry(struct list_head *ptr,
type_of_struct,
field_name);
ptr: list_head指针
type_of_struct: 包含ptr的结构体类型
field_name: 结构体中链表字段的名字
如:
my_struct *p = (list_head *ptr, my_struct, list);
b.遍历链表
list_for_each(struct list_head *cursor,
struct list_head *list);
//常常和list_entry配套使用
//注意!用list_for_each遍历时,不包括头节点
c.遍历的同时获得大结构体指针
list_for_each_entry(type *cursor,
struct list_head *list,
member);
d.遍历链表的同时释放每个被遍历到的节点
list_for_each_entry_safe(type *cursor,
type *tmp;
struct list_head *list,
member);
来源:https://blog.csdn.net/morixinguan/article/details/79706265
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
QQ网站登录的RSA加密传输缺陷分析
聊聊kubernetes1.20用containerd替换docker(shim)的问题
![](https://img.aspxhome.com/file/2023/9/127979_0s.png)
巨人网络低调推出SNS网站 再度布局社交领域
康盛创想Discuz! 7.1更新 后继版本功能预告发布
三大搜索引擎对英文站收录的策略
dedecms 5.6 完美实现伪静态的方法附打包文件下载
详解Google Adsense单价波动的原因
做站长的十大好处
Docker镜像的创建、存出、载入的方法
选择邮件服务器十二要素(3)
品牌博客成功的三进三出 作者博客同提高
关于Linux操作系统下C语言编程注意事项
既生瑜何生亮 地方网站与百度贴吧的博弈
SNS网站发展之势:在取舍中凸显自我价值
SEO的垃圾链接的流言蜚语
博客评论不仅仅是用来获取流量的
CentOS/RedHat 6.5 离线安装Docker
PJBlog 转换到 WordPress 的最佳方案
![](https://img.aspxhome.com/file/UploadPic/200711/28/20071128195547382s.jpg)
Exchange Server 2010正式发布 试用版下载
![](https://img.aspxhome.com/file/UploadPic/200911/2009112492225235s.jpg)
山寨网站冒充央视骗钱 民警提醒市民要警惕
![](https://img.aspxhome.com/file/UploadPic/200910/200910188150674.jpg)