list_for_each与list_entry详解

list_for_each与list_entry详解


2024年5月17日发(作者:把路由器恢复出厂设置)

list_for_each与list_for_each_entry详解

一、list_for_each

_for_each原型

#define list_for_each(pos, head)

for (pos = (head)->next, prefetch(pos->next); pos != (head);

pos = pos->next, prefetch(pos->next))

它实际上是一个 for 循环,利用传入的pos 作为循环变量,从表头 head开始,逐

项向后(next方向)移动 pos ,直至又回到 head (prefetch() 可以不考虑,用于预

取以提高遍历速度)。

注意:此宏必要把list_head放在数据结构第一项成员,至此,它的地址也就是结构

变量的地址。

2.使用方法(以访问当前进程的子进程为例):

struct list_head {

struct list_head *next, *prev;

};

在struct task_struct 中有如下定义:

struct list_head children;

所以

struct task_struct *task;

struct list_head *list;

list_for_each(list,¤t->chilidren) {

task = list_entry(list, struct task_struct, sibling);/*task指向当前的某个子进

程*/

}

其中用到了函数list_entry():

这个函数的作用在图1中表示就是可以通过已知的指向member子项的指针,获得

整个结构体的指针(地址)

#define list_entry(ptr, type, member)

container_of(ptr, type, member)


发布者:admin,转转请注明出处:http://www.yc00.com/xitong/1715944058a2695765.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信