链表是一些包含数据的独立数据结构的集合,链表中的每一个节点通过链或者指针连接在一起。程序通过指针访问链表中的节点。链表一般分为单链表和双链表。

1.单链表

单链表中,每个节点包含指向下一个节点的指针。链表最有一个节点的指针字段值为NULL,表明链表后面不再有其它节点。下面是一张单链表的图:

对应的数据结构为:

C代码  
  1. typedef struct NODE
  2. {
  3. int value;
  4. struct NODE *next;
  5. }Node;

2.双链表

在一个双链表中,每个节点都包含两个指针——指向前一个节点的指针和指向后一个节点的指针。这样的好处是我们可以从任何方向遍历双链表。

对应的节点数据类型为:

C代码  
  1. typedef struct NODE
  2. {
  3. int value;
  4. struct NODE *fwd;
  5. struct NODE *bwd;
  6. }Node;

3.linux内核链表

此处以2.6.31.13内核版本作为分析基础。不同版本之间的区别不大。链表结构定义为(节选自include/linux/list.h):

C代码  
  1. struct list_head {
  2. struct list_head *next, *prev;
  3. };

内核链表包含指向next和prev的指针,是一个双链表,不过不同于一般的双链表,内核链表不包含数据域,通常被用作双循环链表,当需要用到十字链表时,使用内核链表也很方便。

3.1 声明和初始化

linux内核提供了两种方式初始化链表。一种是使用LIST_HEAD()这个宏:

C代码  
  1. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  2. #define LIST_HEAD(name) \
  3. struct list_head name = LIST_HEAD_INIT(name)

另外有一个内联函数用于运行时初始化:

C代码  
  1. static inline void INIT_LIST_HEAD(struct list_head *list)
  2. {
  3. list->next = list;
  4. list->prev = list;
  5. }

3.2 添加、删除

下面都是些很基本的操作,只要弄清楚了链表的原理,都很容易理解。

C代码  
  1. /*
  2. * Insert a new entry between two known consecutive entries.
  3. *
  4. * This is only for internal list manipulation where we know
  5. * the prev/next entries already!
  6. */
  7. static inline void __list_add(struct list_head *new,
  8. struct list_head *prev,
  9. struct list_head *next)
  10. {
  11. next->prev = new;
  12. new->next = next;
  13. new->prev = prev;
  14. prev->next = new;
  15. }
  16. /**
  17. * list_add - add a new entry
  18. * @new: new entry to be added
  19. * @head: list head to add it after
  20. *
  21. * Insert a new entry after the specified head.
  22. * This is good for implementing stacks.
  23. */
  24. static inline void list_add(struct list_head *newstruct list_head *head)
  25. {
  26. __list_add(new, head, head->next);
  27. }
  28. /**
  29. * list_add_tail - add a new entry
  30. * @new: new entry to be added
  31. * @head: list head to add it before
  32. *
  33. * Insert a new entry before the specified head.
  34. * This is useful for implementing queues.
  35. */
  36. static inline void list_add_tail(struct list_head *newstruct list_head *head)
  37. {
  38. __list_add(new, head->prev, head);
  39. }
  40. static inline void __list_del(struct list_head * prev, struct list_head * next)
  41. {
  42. next->prev = prev;
  43. prev->next = next;
  44. }
  45. static inline void list_del(struct list_head *entry)
  46. {
  47. __list_del(entry->prev, entry->next);
  48. entry->next = LIST_POISON1;
  49. entry->prev = LIST_POISON2;
  50. }
  51. static inline void list_del_init(struct list_head *entry)
  52. {
  53. __list_del(entry->prev, entry->next);
  54. INIT_LIST_HEAD(entry);
  55. }
  56. static inline void list_move(struct list_head *list, struct list_head *head)
  57. {
  58. __list_del(list->prev, list->next);
  59. list_add(list, head);
  60. }
  61. static inline void list_move_tail(struct list_head *list,
  62. struct list_head *head)
  63. {
  64. __list_del(list->prev, list->next);
  65. list_add_tail(list, head);
  66. }
  67. static inline int list_empty(const struct list_head *head)
  68. {
  69. return head->next == head;
  70. }

3.3 获取链表节点

linux链表中仅保存了list_head成员变量的地址,那么我们如何通过这个list_head的成员访问到它所有者节点的数据呢?linux提供了list_entry这个宏,ptr是指向该数据中list_head成员的指针,type是节点的类型,member是节点类型中list_head成员的变量名。

C代码  
  1. /**
  2. * list_entry - get the struct for this entry
  3. * @ptr:        the &struct list_head pointer.
  4. * @type:       the type of the struct this is embedded in.
  5. * @member:     the name of the list_struct within the struct.
  6. */
  7. #define list_entry(ptr, type, member) \
  8. container_of(ptr, type, member)

container_of宏定义在include/linux/kernel.h

C代码  
  1. /**
  2. * container_of - cast a member of a structure out to the containing structure
  3. * @ptr:        the pointer to the member.
  4. * @type:       the type of the container struct this is embedded in.
  5. * @member:     the name of the member within the struct.
  6. *
  7. */
  8. #define container_of(ptr, type, member) ({                      \
  9. const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  10. (type *)( (char *)__mptr - offsetof(type,member) );})

offsetof在include/linux/stddef.h中

C代码  
  1. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

获得节点对象指针的原理图如下所示:

((type *)0)->member,它将0地址强制转换为type结构的指针,再访问到type结构中的member成员。offsetof取得list_head成员msg_node相对于结构体的偏移量。将指向当前节点对象member的地址减去偏移量,就可以得到节点地址,再将它转成指向节点结构类型的指针。

linux链表的基本操作已经完成了,其它如链表遍历的操作可查看list.h源码,有很详细的说明。

Linux内核链表及list_entry解析相关推荐

  1. linux内核链表使用例,linux内核链表的使用例子

    linux内核链表的使用例子 #include #include #include #include #include #include MODULE_LICENSE("GPL") ...

  2. linux内核链表以及list_entry--linux内核数据结构(一)

    传统的链表实现 之前我们前面提到的链表都是在我们原数据结构的基础上增加指针域next(或者prev),从而使各个节点能否链接在一起, 比如如下的结构信息 typedef struct fox { un ...

  3. linux内核链表分析

    一.常用的链表和内核链表的区别 1.1  常规链表结构        通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...

  4. Linux内核链表交换节点,[笔记]Linux内核链表:结点的插入、删除以及链表的遍历...

    Linux内核链表:结点的插入.删除以及链表的遍历 1. Linux内核链表的核心思想是:在用户自定义的结构A中声明list_head类型的成员p,这样每个结构类型为A的变量a中,都拥有同样的成员p, ...

  5. 深入分析 Linux 内核链表--转

    引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...

  6. linux内核链表的使用

    linux内核链表: 链表通常包括两个域:数据域和指针域. struct list_head{ struct list_head *next,*prev; }; include/linux/list. ...

  7. Linux 内核链表 【转】

    原文:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据的数 ...

  8. Linux驱动编程 step-by-step (十) Linux 内核链表

    终于可以清闲下来打理一下我的blog了,台资企业真的事情很多很烦-- 前几篇文章对字符设备有个简单介绍,并以简单的一个字符设备驱动作结尾,其实linux上大部分驱动程序都是字符设备程序,Linux源码 ...

  9. Linux 内核链表剖析(二十)

    上节博客中,我们讲到了 Linux 中的宏定义 offsetof 与 container_of 宏.那么本节我们的课程目标就是一直 Linux 内核链表,使其适用于非 GNU 编译器,分析 Linux ...

  10. Linux内核链表的移植与使用

    一.Linux内核链表为双向循环链表,和数据结构中所学链表类似,具体不再细讲.由于在内核中所实现的函数十分经典,所以移植出来方便后期应用程序中的使用. /*********************** ...

最新文章

  1. Node 深入Stream(2)
  2. 7、字典和string的用法
  3. oracle有 哪些常用视图,oracle常用视图
  4. linux git gui使用教程,跨平台最好用的Git GUI工具gitkraken
  5. java 反编译 类名_java javassist创建类和反编译类
  6. C/C++传递二维数组[转载]
  7. 9203学生工具页-建议放桌面
  8. java 短信验证码===随机数
  9. Windows XP解决显示桌面图标消失的问题
  10. JAVA开发第一步——JDK 安装
  11. JAVA面向对象的总结(类的创建与成员局部变量的对比)
  12. python备份文件
  13. 动态瑜伽 静态瑜伽 初学者_使用计算机视觉对瑜伽姿势进行评分
  14. 如何在linux上运行asp网站,linux上搭建asp网站
  15. 16种常用的数据分析方法-相关分析
  16. ecc升级s4后金额字段负号提前,导出excel负号却在后面
  17. DigiCert和GlobalSign单域名OV SSL证书对比评测
  18. LaTeX 各种写法
  19. 【机器学习基础】线性基函数模型
  20. 学习c/c++ 推荐学习什么书籍?

热门文章

  1. iOS NSString的常用用法
  2. IO-同步、异步、阻塞、非阻塞
  3. 用 intellij idea 创建一个Java web项目
  4. c语言链表死循环,单项循环链表解决Joseph 问题,死循环了,求帮忙
  5. python中正确的赋值语句_在Python中使用赋值表达式时,如何完成赋值语句“x=y:=f(x)”?...
  6. 各种说明方法的答题格式_各种轴承安装方法说明及注意事项,避免这些坑提高轴承寿命...
  7. Hadoop YARN配置参数剖析(5)—Capacity Scheduler相关参数
  8. Kebernetes 学习总结(8) statefulset
  9. chaos-monkey-spring-boot小试牛刀
  10. JS编程建议——72:惰性载入函数