list_for_each_entry浅析_list for i in range
Linux内核中使用的最多的是双向循环链表;
链表代码在头文件<linux/list.h>中声明,其数据结构很简单:
struct list_head {
struct list_head *next;
struct list_head *prev;
};
值得注意的是list_head通常放在自己定义的结构体中使用;
例如:
1, 定义结构体
struct stu{
int age;
char *name;
int score;
struct list_head list;
};
2, 初始化结构体及链表
struct stu *mid_stu;
mid_stu = kzmalloc(.....);
NIT_LIST_HEAD(&mid_stu->list);
3, 添加链表成员
void list_add(struct list_head *new, struct list_head *head);
4, 删除链表成员
void list_del(struct list_head *entry);
5, 遍历链表
最常用的遍历链表的方法为list_for_each_entry
遍历链表中很关键的一步是根据成员地址找到结构体地址
直接上代码:
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
----------------------
list_entry表示在找出ptr指向的链表节点所在的type类型的结构体首地址
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
ptr:表示和member同为相同类型的链表,此处ptr表示指向链表中的一个节点
type:表示需要寻找的结构体类型。
member:表示type类型的结构体里面的成员。
----------------------
#define container_of(ptr, type, member) ({\
const typeof( ((type *)0)->member ) *__mptr = (ptr);\
(type *)( (char *)__mptr - offsetof(type,member) );})
相关文章
- Spring Boot中对接Twilio以实现发送验证码和验证短信码
- Spring Boot 3.5:这次更新让你连配置都不用写了,惊不惊喜?
- Spring Boot+Pinot实战:毫秒级实时竞价系统构建
- SpringBoot敏感配置项加密与解密实战
- SpringBoot 注解最全详解,建议收藏!
- Spring Boot 常用注解大全:从入门到进阶
- SpringBoot启动之谜:@SpringBootApplication如何让配置化繁为简
- Springboot集成Kafka原理_spring集成kafka的原理
- Spring Boot中@Data注解的深度解析与实战应用
- 大佬用1000字就把SpringBoot的配置文件讲的明明白白!