linux内核数据结构之链表

代码如下:

list.h中:

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

 

container_of(ptr, type, member)在kernle.h中  

/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member. 指向member成员的指针
* @type: the type of the container struct this is embedded in.  结构数据的指针
* @member: the name of the member within the struct. 结构成员的名称
*
*/
#define container_of(ptr, type, member) ({ \
void *__mptr = (void *)(ptr); \
BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
!__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
((type *)(__mptr - offsetof(type, member))); })

 

其中offsetof(type, member)又在:stddef.h中:

19 #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)

posted @   叕叒双又  阅读(66)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-08-19 查看库文件中的接口
2017-08-19 vim配置说明20170819
点击右上角即可分享
微信分享提示