小知识:Linux内核设备驱动之内核中链表的使用笔记整理

?
1
2
3
/********************
* 内核中链表的应用
********************/

(1)介绍

在Linux内核中使用了大量的链表结构来组织数据,包括设备列表以及各种功能模块中的数据组织。这些链表大多采用在include/linux/list.h实现的一个相当精彩的链表数据结构。

链表数据结构的定义很简单:

?
1
2
3
struct list_head {
struct list_head *next, *prev;
};

list_head结构包含两个指向list_head结构的指针prev和next,内核的数据结构通常组织成双循环链表。

和以前介绍的双链表结构模型不同,这里的list_head没有数据域。在Linux内核链表中,不是在链表结构中包含数据,而是在数据结构中包含链表节点。如:

?
1
2
3
4
5
struct my_struct{
struct list_head list;
unsigned long dog;
void *cat;
};

linux中的链表没有固定的表头,从任何元素开始访问都可以。遍历链表仅仅需要从某个节点开始,沿指针访问下一个节点,直到又重新回到最初这个节点就可以了。每个独立的节点都可以被称作是链表头。

(2)链表的初始化

a.静态

如果在编译时静态创建链表,并且直接引用它,如下:

?
1
2
3
4
5
6
7
8
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.动态

?
1
2
3
4
5
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.增加节点

?
1
2
3
list_add(struct list_head *new,
struct list_head *head);
//向指定链表的head节点后面插入new节点

b.把节点增加到链表尾

?
1
2
3
list_add_tail(struct list_head *new,
struct list_head *head);
//向指定链表的head节点前面插入new节点

c.从链表删除一个节点

?
1
2
list_del(struct list_head *entry);
//将entry从链表中移走

d.把节点从一个链表移到另一个链表

?
1
2
list_move(struct list_head *list,
struct list_head *head);

从一个链表中摘除list项,然后将其插入head的后面

?
1
e.list_empty(struct list_head *head);

链表为空返回非0值,否则返回0

f.合并链表

?
1
2
3
list_splice(struct list_head *list,
struct list_head *head);
//注意!新的链表不包括list节点

(4)遍历链表

链表本身不重要,访问到那个包含链表的结构体才重要

a.从链表指针获得包含该链表的结构体的指针

?
1
2
3
list_entry(struct list_head *ptr,
type_of_struct,
field_name);
ptr: list_head指针 type_of_struct: 包含ptr的结构体类型 field_name: 结构体中链表字段的名字

如:

?
1
my_struct *p = (list_head *ptr, my_struct, list);

b.遍历链表

?
1
2
3
4
list_for_each(struct list_head *cursor,
struct list_head *list);
//常常和list_entry配套使用
//注意!用list_for_each遍历时,不包括头节点

c.遍历的同时获得大结构体指针

?
1
2
3
list_for_each_entry(type *cursor,
struct list_head *list,
member);

d.遍历链表的同时释放每个被遍历到的节点

?
1
2
3
4
list_for_each_entry_safe(type *cursor,
type *tmp;
struct list_head *list,
member);

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/morixinguan/article/details/79706265

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:nginx负载功能+nfs服务器功能解析

2023-3-20 4:40:33

建站知识

小知识:Linux内核设备驱动之proc文件系统笔记整理

2023-3-20 4:50:01

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索