Linux内核链表及list_entry解析

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

 

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.   
  3. #define LIST_HEAD(name) \  
  4.         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. * This is only for internal list manipulation where we know 
  4. * the prev/next entries already! 
  5. */  
  6. static inline void __list_add(struct list_head *new,  
  7.                               struct list_head *prev,  
  8.                               struct list_head *next)  
  9. {  
  10.         next->prev = new;  
  11.         new->next = next;  
  12.         new->prev = prev;  
  13.         prev->next = new;  
  14. }  
  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. * Insert a new entry after the specified head. 
  21. * This is good for implementing stacks. 
  22. */  
  23. static inline void list_add(struct list_head *new, struct list_head *head)  
  24. {  
  25.         __list_add(new, head, head->next);  
  26. }  
  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. * Insert a new entry before the specified head. 
  33. * This is useful for implementing queues. 
  34. */  
  35. static inline void list_add_tail(struct list_head *new, struct list_head *head)  
  36. {  
  37.         __list_add(new, head->prev, head);  
  38. }  
  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.   
  46. static inline void list_del(struct list_head *entry)  
  47. {  
  48.         __list_del(entry->prev, entry->next);  
  49.         entry->next = LIST_POISON1;  
  50.         entry->prev = LIST_POISON2;  
  51. }  
  52.   
  53. static inline void list_del_init(struct list_head *entry)  
  54. {  
  55.         __list_del(entry->prev, entry->next);  
  56.         INIT_LIST_HEAD(entry);  
  57. }  
  58.   
  59. static inline void list_move(struct list_head *list, struct list_head *head)  
  60. {  
  61.         __list_del(list->prev, list->next);  
  62.         list_add(list, head);  
  63. }  
  64.   
  65. static inline void list_move_tail(struct list_head *list,  
  66.                                   struct list_head *head)  
  67. {  
  68.         __list_del(list->prev, list->next);  
  69.         list_add_tail(list, head);  
  70. }  
  71.   
  72. static inline int list_empty(const struct list_head *head)  
  73. {  
  74.         return head->next == head;  
  75. }  

 

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. #define container_of(ptr, type, member) ({                      \  
  8.         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \  
  9.         (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源码,有很详细的说明。

 

转自:http://dsea.iteye.com/blog/1689390

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值