温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux内核设备驱动之内核中链表有什么用

发布时间:2021-07-14 13:37:00 来源:亿速云 阅读:210 作者:小新 栏目:服务器

小编给大家分享一下Linux内核设备驱动之内核中链表有什么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

/********************
 * 内核中链表的应用
 ********************/

(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);

以上是“Linux内核设备驱动之内核中链表有什么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI