五.用链表外的结构体地址来进行遍历,而不用哈希链表的地址进行遍历
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)
