遍历带有双向链表结构的结构体变量。
它实际上是一个 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,返回指向该结构体的指针。
2、 typeof关键字是C语言中的一种新扩展,返回变量的类型
3、 prefetch(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