Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6379289
  • 博文数量: 579
  • 博客积分: 1548
  • 博客等级: 上尉
  • 技术积分: 16634
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-12 15:29
个人简介

http://www.csdn.net/ http://www.arm.com/zh/ https://www.kernel.org/ http://www.linuxpk.com/ http://www.51develop.net/ http://linux.chinaitlab.com/ http://www.embeddedlinux.org.cn http://bbs.pediy.com/

文章分类

全部博文(579)

文章存档

2018年(18)

2015年(91)

2014年(159)

2013年(231)

2012年(80)

分类: C/C++

2013-05-08 17:38:21

(五)hlist的搬移操作
还记得
list_move(struct list_head *list, struct list_head *head)吗?
这个表示将list从一个链表当中删除之后,加入到一新的链表head当中。
在hlist中的搬移指的是,将new指向old所指向的那个链表。搬移之后,如果new的第一个元素不为空,则修改它的pprev值。
  1. static inline void hlist_move_list(struct hlist_head *old,
  2.                  struct hlist_head *new)
  3. {
  4.     new->first = old->first;
  5.     if (new->first)
  6.         new->first->pprev = &new->first;
  7.     old->first = NULL;
  8. }
(六)hlist的宏遍历
关于hlist遍历的宏总共有7个,下面我们一一来分析:
1)
ptr:表示struct hlist_node类型的一个地址。
type:结构体名
member:type结构体中的hlist_node成员变量的名称
这个宏在list链表中分析过了,表示得到ptr所指地址的这个结构体的首地址
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
2)
pos:struct hlist_node类型的一个指针;
head:struct hlist_head类型的一个指针,表示hlist链表的头结点。
这个实际上就是一个for循环,从头到尾遍历链表。
  1. #define hlist_for_each(pos, head) \
  2.     for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
  3.      pos = pos->next)
3)
这个实际上就是一个for循环,从头到尾遍历链表。这个和前面的不同的是多了一个n,这么做是为了遍历过程中防止断链的发生。
pos:struct hlist_node类型的一个指针;
n:struct hlist_node类型的一个指针;
head:struct hlist_head类型的一个指针,表示hlist链表的头结点。
  1. #define hlist_for_each_safe(pos, n, head) \
  2.     for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  3.      pos = n)
4)
tops:用来存放遍历到的数据结构的地址,类型是type *;
pos:struct hlist_node类型的一个指针;
head:hlist链表的头结点;
member:struct hlist_node在type结构体中的变量的名称。
在循环中,我们就可以使用tops来指向type类型结构体的任何一个变量了。
  1. #define hlist_for_each_entry(tpos, pos, head, member)             \
  2.     for (pos = (head)->first;                     \
  3.      pos && ({ prefetch(pos->next); 1;}) &&             \
  4.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  5.      pos = pos->next)
5)
tops:用来存放遍历到的数据结构的地址,类型是type *;
pos:struct hlist_node类型的一个指针;
member:struct hlist_node在type结构体中的变量的名称。
这个宏是从pos这个地址的下一个元素处开始继续往下遍历。
  1. #define hlist_for_each_entry_continue(tpos, pos, member)         \
  2.     for (pos = (pos)->next;                         \
  3.      pos && ({ prefetch(pos->next); 1;}) &&             \
  4.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  5.      pos = pos->next)
6)
tops:用来存放遍历到的数据结构的地址,类型是type *;
pos:struct hlist_node类型的一个指针;
member:struct hlist_node在type结构体中的变量的名称。
这个宏是从pos这个地址处开始继续往下遍历。
  1. #define hlist_for_each_entry_from(tpos, pos, member)             \
  2.     for (; pos && ({ prefetch(pos->next); 1;}) &&             \
  3.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  4.      pos = pos->next)
注:(5)和(6)的区别就是一个是从pos的下一个开始,而(6)是pos开始。
7)
tops:用来存放遍历到的数据结构的地址,类型是type *;
pos:struct hlist_node类型的一个指针;
n:struct hlist_node类型的一个指针;
head:hlist链表的头结点;
member:struct hlist_node在type结构体中的变量的名称。
在循环中,我们就可以使用tops来指向type类型结构体的任何一个变量了。这个宏函数也是为了防止在遍历的时候删除节点而引入的。
  1. #define hlist_for_each_entry_safe(tpos, pos, n, head, member)          \
  2.     for (pos = (head)->first;                     \
  3.      pos && ({ n = pos->next; 1; }) &&                  \
  4.         ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
  5.      pos = n)
阅读(2166) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~