list_for_each_entry_rcu的使用方法

57 篇文章 4 订阅

遍历带有双向链表结构的结构体变量。
它实际上是一个 for 循环,利用传入的 pos 作为循环变量,从表头 head 开始,逐项向后(next 方向)移动 pos,直至又回head(prefetch() 可以不考虑,用于预取以提高遍历速度 )。

#define list_for_each_entry_rcu(pos, head, member) \
	for (pos = list_entry_rcu((head)->next, typeof(*pos), member); \
		prefetch(pos->member.next), &pos->member != (head); \
		pos = list_entry_rcu(pos->member.next, typeof(*pos), member))

list_for_each_entry_rcu(pos, head, member)
{
	 addr = pos;  //对返回值pos的操作,这样更容易去理解list_for_each_entry,可以把它看作for()循环
	/*获取pos,代码处理*/
}
1、 pos对应的结构体成员地址
2、 head对应的是结构体头指针
3、 member对应的是结构体的成员


用法用例:
typedef struct tagTest_TableList
{
	UINT i;
	UCHAR a;
	struct list_head TableList;
}TEST_LIST_S;
其中struct list_head 是:
struct list_head{
	struct list_head *next,*prev;
};

TEST_LIST_S *g_testlist = NULL;
TEST_LIST_S *temp;
list_for_each_entry_rcu(temp, &(g_testlist->TableList), TableList);
{
	if(NULL == temp)
		continue;
	temp->i = 1;//对获取的temp结构体变量进行操作
	/*...*/
}


#define list_entry_rcu(ptr, type, member) \
	container_of(rcu_dereference(ptr), type, member)
1、 list_entry_rcu作用:根据指向结构体type中成员member的指针ptr,返回指向该结构体的指针。
2typeof关键字是C语言中的一种新扩展,返回变量的类型
3prefetch(pos->member.next):通知CPU将()内的数据预取,以提高效率。
4、 判断条件:&pos->member != (head)list_for_each_entry_rcu的作用:head为链表的头,它作为一个成员member被包含在pos指向的结构体中,从head开始遍历链表,直到pos又指向包含head的结构体,停止遍历。

container_of的理解可以转到:https://blog.csdn.net/qq_35399548/article/details/118767270?spm=1001.2014.3001.5502)

参考链接:
https://blog.csdn.net/qq_35399548/article/details/118767270?spm=1001.2014.3001.5502
https://blog.csdn.net/u012503639/article/details/77771814

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值