list_head解析

list_head结构体

2.1、简介

原型:
[objc]  view plain  copy
  1. struct list_head {  
  2.     struct list_head *next, *prev;  
  3. };  

此结构体所构成的链表如上面的双向循环链表图示
此结构体在linux内核中被大量的引用,几乎所有内核当中需要构成链表结构的地方都用到了这个结构体。例如内核的总线设备就用到了这个结构体

2.2、此结构体的作用

例如现在有任意给定的一个结构体需要组成一个双向链表,则可以在此结构体当中加入list_head结构体用来进行链表的构建,例如结构体原型如下
[objc]  view plain  copy
  1. struct test_list  
  2. {  
  3.     unsigned charchar *name;  
  4.     struct list_head test;  
  5.     int value;  
  6. };  

下面是根据此结构体以及list_head文件中的函数编写的测试文件,使用GCC4.8.1进行编译
[objc]  view plain  copy
  1. #include <stdio.h>  
  2. //#include "list.h"  
  3.   
  4. struct list_head {  
  5.     struct list_head *next, *prev;  
  6. };  
  7.   
  8. struct test_list  
  9. {  
  10.     unsigned charchar *name;  
  11.     struct list_head test;  
  12.     int value;  
  13. };  
  14.   
  15. static struct test_list lists[] = {  
  16.     [0] = {  
  17.         .value = 0,  
  18.         .name = "hehe"  
  19.     },  
  20.     [1] = {  
  21.         .value = 1,  
  22.     },  
  23.     [2] = {  
  24.         .value = 2,  
  25.     },  
  26.     [3] = {  
  27.         .value = 3,  
  28.         .name = "test"  
  29.     },  
  30.     [4] = {  
  31.         .value = 4,  
  32.     }     
  33. };  
  34.   
  35. struct list_head head = {  
  36.     &(head), &(head)  
  37. };  
  38.   
  39. static void __list_add_tail(struct list_head *newstruct list_head *prev, struct list_head *next)  
  40. {  
  41.     new->next = next;  
  42.     new->prev = prev;  
  43.     prev->next = new;  
  44.     next->prev = new;  
  45. }  
  46.   
  47. void mlist_add_tail(struct list_head *newstruct list_head *head)  
  48. {  
  49.     __list_add_tail(new, head->prev, head);  
  50. }  
  51.   
  52. #define mlist_entry(list_head, type, member_name) \  
  53.             (type *)((unsigned int)list_head - (unsigned int)(&(((type*)(0))->member_name)))  
  54.   
  55.   
  56. #define mlist_for_each(pos, head) \  
  57.             for(pos = (head)->prev; pos != head; pos = pos->prev)  
  58.   
  59. int main(int argv, charchar *argc[])  
  60. {  
  61.     struct list_head *mod;  
  62.     unsigned int i = 0;  
  63.       
  64.     mlist_add_tail(&lists[0].test, &head);  
  65.     mlist_add_tail(&lists[1].test, &head);  
  66.     mlist_add_tail(&lists[2].test, &head);  
  67.     mlist_add_tail(&lists[3].test, &head);  
  68.     mlist_add_tail(&lists[4].test, &head);  
  69.       
  70.     mlist_for_each(mod, &head)  
  71.     {  
  72.         struct test_list *inode;  
  73.           
  74.         inode = mlist_entry(mod, struct test_list, test);  
  75.         printf("%d %s\n", inode->value, inode->name);  
  76.     }         
  77.           
  78.     getchar();  
  79.       
  80.     return 0;  
  81. }  
上述代码经过编译之后运行的结果如下
4 (null)
3 test
2 (null)
1 (null)
0 hehe

后经修改
[objc]  view plain  copy
  1. #define mlist_for_each(pos, head) \  
  2.             for(pos = (head)->prev; pos != head; pos = pos->prev)  

[objc]  view plain  copy
  1. #define mlist_for_each(pos, head) \  
  2.             for(pos = (head)->next; pos != head; pos = pos->next)  
运行结果如下
0 hehe
1 (null)
2 (null)
3 test
4 (null)

由此可以得知此链表的结构为一个双向链表。

2.3、实现方法

首先是头结点的创建以及初始化
[objc]  view plain  copy
  1. struct list_head head = {  
  2.     &(head), &(head)  
  3. };  

此时head的结构体成员均指向自己,形如

链表的添加过程
[objc]  view plain  copy
  1. static void __list_add_tail(struct list_head *newstruct list_head *prev, struct list_head *next)  
  2. {  
  3.     new->next = next;  
  4.     new->prev = prev;  
  5.     prev->next = new;  
  6.     next->prev = new;  
  7. }  


由结构体内部的list_head结构体得到结构体所在的位置
[objc]  view plain  copy
  1. #define mlist_entry(list_head, type, member_name) \  
  2.             (type *)((unsigned int)list_head - (unsigned int)(&(((type*)(0))->member_name)))  

反映到代码 mlist_entry(mod, struct test_list, test) 得到
(struct test_list *)((unsigned int)mod - (unsigned int)(&((( struct test_list *)(0))-> test)))
分层次解析
((( struct test_list *)(0))-> test)  定义一个test_list结构体变量0,使其指向其中的test成员。
(&((( struct test_list *)(0))-> test)))  取此成员所在的地址,由于起始为0地址,所以现在获得的地址就是test_list结构体开始到其test成员的偏移值,将其强制转换为unsigned int型变量
((unsigned int)mod - (unsigned int)(&((( struct test_list *)(0))-> test))) 拿取得的list_head结构体mod的位置减去偏移值得到当前mod所在的test_list结构体的起始位置,最后将这个值强制转换为test_list结构体指针,之后就得到一个test_list的地址,也就是当前list_head结构体所在的那个test_list。
其余诸如链表的删除等等操作相对容易理解,不再赘述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值