手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux内核哈希表分析与应用

[日期:2013-01-17] 来源:CSDN  作者:tigerjb [字体:大 中 小]

五.用链表外的结构体地址来进行遍历,而不用哈希链表的地址进行遍历
Linux提供了从三种方式进行遍历,一种是从哈希链表第一个哈希结点开始遍历;第二种是从哈希链表中的pos结点的下一个结点开始遍历;第三种是从哈希链表中的当前结点开始进行遍历。

1.从哈希链表第一个哈希结点开始进行遍历
1>function: 从哈希链表的第一个哈希结点开始进行遍历。hlist_for_each_entry在进行遍历时不能删除pos(必须保证pos->next有效),否则会造成SIGSEGV错误。而使用hlist_for_each_entry_safe则可以在遍历时进行删除操作。
2>Linux提供了两个接口来实现从哈希表第一个结点开始进行遍历
hlist_for_each_entry(tpos, pos, head, member)
 tpos: 用于遍历的指针,只是它的数据类型是结构体类型而不是strut hlist_head 类型
 pos: 用于遍历的指针,只是它的数据类型是strut hlist_head 类型
 head:哈希表的头结点
 member: 该数据项类型定义中hlist_head成员的变量名
hlist_for_each_entry_safe(tpos, pos, n, head, member)
 tpos: 用于遍历的指针,只是它的数据类型是结构体类型而不是strut hlist_head 类型
 pos: 用于遍历的指针,只是它的数据类型是strut hlist_head 类型
 n: 临时指针用于占时存储pos的下一个指针,它的数据类型也是struct hlist_list类型
 head: 哈希表的头结点
 member: 该数据项类型定义中hlist_head成员的变量名
3>实现
#define hlist_for_each_entry(tpos,pos,head,member)                  \
        for (pos = (head)->first;                                    \
            pos &&                                                  \
                ({tpos = hlist_entry(pos, typeof(*tpos),member);1;});\
            pos = pos->next)   
   
#define hlist_for_each_entry_safe(tpos, pos, n, head, member)        \
        for (pos = (head)->first;                                    \
            pos && ({ n = pos->next;1;})  &&                        \
            ({tpos = hlist_entry(pos, typeof(*tpos),member);1;});    \
            pos = n) 

2. 从哈希链表中的pos结点的下一个结点开始遍历
1>function:从pos结点的下一个结点进行遍历。
2>函数接口:
hlist_for_each_entry_continue(tpos ,pos, member)
 tpos: 用于遍历的指针,只是它的数据类型是结构体类型而不是strut hlist_head 类型
 pos: 用于遍历的指针,只是它的数据类型是strut hlist_head 类型
 member: 该数据项类型定义中hlist_head成员的变量名
3>函数实现:
#define  hlist_for_each_entry_continue(tpos, pos, member)                \
            for (pos = (pos)->next;                                    \
                  pos &&                                                \
                  ({tpos = hlist_entry(pos,typeof(*tpos),member);1;});    \
                  pos = pos->next)

3.从哈希链表中的pos结点的当前结点开始遍历
1>function:从当前某个结点开始进行遍历。hlist_for_entry_continue是从某个结点之后开始进行遍历。
2>函数接口:
hlist_for_each_entry_from(tpos, pos, member)
 tpos: 用于遍历的指针,只是它的数据类型是结构体类型而不是strut hlist_head 类型
 pos: 用于遍历的指针,只是它的数据类型是strut hlist_head 类型
 member: 该数据项类型定义中hlist_head成员的变量名
3>实现
#define hlist_for_each_entry_from(tpos, pos, member)                \
        for (; pos &&                                              \
                  ({tpos = hlist_entry(pos,typeof(*tpos),member);1;});\
                pos = pos->next)

linux
相关资讯       Linux内核分析  哈希表 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款