linux内核 list_for_each_entry

1. linux内核中的list用法

在linus内核中,list一般这样使用:

struct list_head {
struct list_head *priv;
struct list_head *next;
}
struct xxx {
int value; // 结构体的真实成员
struct list_head *lh;
}

 

2. list遍历

结构体xxx里面有struct list_head *lh,同时lh通过priv和next和其他list_head*建立联系,这样就形成了结构体链表。

// 已知ptr是type结构体里面的member,求这个type结构体的地址
#define list_entry(ptr, type, member) container_of(ptr, type, member)
// ① 不要修改链表头
// ② cur_struct是传入的、可以被修改的、遍历找到的、成员包含list_head的结构体
#define list_for_each_entry(cur_struct, cur_list_head, member) \
for (cur_struct = list_entry(cur_list_head, typeof(cur_struct), member); \
&cur_struct->member != cur_list_head; // linux list是一个“双向环状”列表
cur_struct = list_entry(cur_struct->member.next, typeof(*cur_struct), member))

 

posted @   moonのsun  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示