当前位置: 首页 > news >正文

linux 的list_for_each_entry

linux的宏定义提高了代码的简洁性,但有时候的命名不够完美。比如list_for_each_entry,看名字只知道是遍历list,但一看里面的三个变量参数,有点懵逼。

/**
* list_for_each_entry  -       iterate over list of given type
* @pos:        the type * to use as a loop cursor.
* @head:       the head for your list.
* @member:     the name of the list_head within the struct.
*/
#define list_for_each_entry(pos, head, member)                          \
for (pos = list_first_entry(head, typeof(*pos), member);        \
!list_entry_is_head(pos, head, member);                    \
pos = list_next_entry(pos, member))
谁是given type? 被遍历的是谁?咋看看不出来。

/**
* list_first_entry - get the first element from a list
* @ptr:        the list head to take the element from.
* @type:       the type of the struct this is embedded in.
* @member:     the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/

#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)

这个宏的comments解释说是获取一个list的第一个元素。 但是一看宏定义有点懵,怎么又来一个宏在里面?

/**
* list_entry - get the struct for this entry
* @ptr:        the &struct list_head pointer.
* @type:       the type of the struct this is embedded in.
* @member:     the name of the list_head within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)

这个宏定义看解释是获取当前entry的所属结构体,又来一个宏:

  1. /**

  2. * container_of - cast a member of a structure out to the containing structure

  3. * @ptr: the pointer to the member.

  4. * @type: the type of the container struct this is embedded in.

  5. * @member: the name of the member within the struct.

  6. *

  7. * WARNING: any const qualifier of @ptr is lost.

  8. */

  9. #define container_of(ptr, type, member) ({ \

  10. void *__mptr = (void *)(ptr); \

  11. static_assert(__same_type(*(ptr), ((type *)0)->member) || \

  12. __same_type(*(ptr), void), \

  13. "pointer type mismatch in container_of()"); \

  14. ((type *)(__mptr - offsetof(type, member))); })

 此宏就是,给了一个结构体变量的其中成员变量,而获取此结构体变量的地址。ptr是成员变量地址,type是结构体类型,member是成员变量在结构体声明里的名字称谓。

所以说 list_entry 是获取ptr所属的结构体地址。list_first_entry 是获取(ptr)->next 所在结构体的地址,

而list_for_each_entry里面还有宏:

/**
* list_entry_is_head - test if the entry points to the head of the list
* @pos:        the type * to cursor
* @head:       the head for your list.
* @member:     the name of the list_head within the struct.
*/
#define list_entry_is_head(pos, head, member)                           \
(&pos->member == (head))
这个宏名字取的更敷衍。解释内容说是为了验证是不是list的头指针。 看代码是验证pos这个结构体的成员member地址是不是和head一样。

再看另外一个宏:

/**
* list_next_entry - get the next element in list
* @pos:        the type * to cursor
* @member:     the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
解释内容说是获取list的下一个成员指针,看代码是获取(pos)->member.next 所在结构体的地址。

这样整体来看:

#define list_for_each_entry(pos, head, member)                          \
for (pos = list_first_entry(head, typeof(*pos), member);        \ //获取head->next所在结构体地址给pos
!list_entry_is_head(pos, head, member);                    \//判断pos的member成员地址是否和head一样,如果一样,就跳出循环
pos = list_next_entry(pos, member))   //获取(pos)->member.next 所在结构体的地址

综上所述,这里遍历了一个循环链表,表头是head,但是似乎head没被拿来遍历到?直接从head->next开始的?遍历到再次碰到head的地址就结束了,即表明遍历了一圈了。 member就是链表个成员(链表类型地址)内含的内容地址。这里head为什么是废弃的?

http://www.dtcms.com/a/285421.html

相关文章:

  • 【c++】STL-容器 list 的实现
  • 20250718-2-Kubernetes 应用程序生命周期管理-Pod对象:基本概念(豌豆荚)_笔记
  • [AI8051U入门第五步]modbus_RTU主机
  • 怎么把图片做成实拍的感觉?给图片加上拍摄时间,相机信息等就可以了
  • PostgreSQL 16 Administration Cookbook 读书笔记:第7章 Database Administration
  • 如何下载并安装AIGCPanel
  • 设计模式五:桥模式(Bridge Pattern)
  • charles雷电模拟器抓包教程
  • 大数据时代下的时序数据库选型指南:基于工业场景的IoTDB技术优势与适用性研究
  • CCF编程能力等级认证GESP—C++2级—20250628
  • 张力场中的领航者:驾驭二元对立的“情境智慧”模型
  • UVC for USBCamera in Android - 篇二
  • HAL库的串口
  • Vite/Vue 项目 | 开发环境指定Host(允许其它电脑访问)
  • uniapp+vue2——自定义底部导航tabbar
  • STC89C52系列单片机内部结构详解
  • 我用EV-21569-SOM评估来开发ADSP-21569(八)-UART串口例程
  • 基于单片机的点阵式汉字电子显示屏的设计
  • Vue3 业务落地全景:脚手架、权限、国际化、微前端、跨端与低代码 50 条实战心法
  • 基于单片机的便携太阳能光伏系统研究
  • 基于单片机金沙河粮仓环境监测系统设计与实现
  • stm32驱动双步进电机
  • 第三章第四节 按键控制光敏传感器控制蜂鸣器
  • LeetCode|Day18|20. 有效的括号|Python刷题笔记
  • 格式转换Total Excel Converter:20 种格式XLS XLSX 批量转 PDFWord
  • 饿了么app 抓包 hook
  • 【论文蒸馏】Recent Advances in Speech Language Models: A Survey
  • 怎么判断一个对象是不是vue的实例
  • 新手向:图片批量裁剪工具
  • 【IOS webview】IOS13不支持svelte 样式嵌套