#define container_of(ptr, type, member)


#define container_of(ptr, type, member)

/* include/linux/kernel.h */

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the 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) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

输入:
结构体一个成员的member地址ptr,
结构体的类型,
结构体一个成员member的名,

输出:
得到这个结构体变量的地址;

分析:
结构体类型是确定的,结构体成员 相对 结构体定义的起始地址 偏移是确定的;
知道了一个结构体成员的地址,可以根据偏移得到 结构体定义的起始地址;

Author: Yangkai Wang
wang_yangkai@163.com
Coding in 2021/05/07


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM