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

海口企业网站建设app运营需要做哪些

海口企业网站建设,app运营需要做哪些,做甜品网站,山东网站建设公司电话container_of 宏 今天遇到了一段这样的代码,大致意思是 通过该struct结构体变量的成员的地址来反推该struct结构体变量的地址 并且用到了内核的宏,container_of() static inline struct nova_inode_info *NOVA_I(struct inode *inode) {return container…

container_of 宏

今天遇到了一段这样的代码,大致意思是 通过该struct结构体变量的成员的地址来反推该struct结构体变量的地址

并且用到了内核的宏,container_of()

static inline struct nova_inode_info *NOVA_I(struct inode *inode)
{return container_of(inode, struct nova_inode_info, vfs_inode);
}

查了查该宏位于include/linux/kernel.h文件

功能:由结构体变量的某个成员变量的内存地址来得到该结构体变量的内存地址

参数:

  1. 该结构体成员变量的地址(指针)

    2. 该结构体的定义类型(type)
    3. 该结构体成员的变量名()
    

具体实现:

#define container_of(ptr, type, member) ({                      \const typeof( ((type *)0)->member ) *__mptr = (ptr);    \(type *)( (char *)__mptr - offsetof(type,member) );})

原理:

  1. 用到GCC的typeof预处理语句,typeof(x),返回参数x的类型
  2. 用到了offsetof(type,member),由一个结构成员member,得到相对于结构开头的字节偏移量 .在<stddef.h>

container_of()做了两步.

  1. 模拟该结构体变量指针指向成员然后通过typeof()反推成员的数据类型,const typeof( ((type *)0)->member),然后定义一个该成员变量类型的指针(就是ptr的数据类型)const typeof( ((type *)0)->member) *__mptr = (ptr)
  2. __mptr转换为char *,按照1个字节计算,__mptr减去成员偏移量就是结构体变量的基地址了。再将该地址(指针)转换为type指针
  3. 最后表达式返回的是 (type *) (结构体变量基地址) 指针
1. const typeof( ((type *)0)->member) *__mptr = (ptr)
2. (char *)__mptr - offsetof(type,member)

测试:

#include <stdio.h>
#include <stddef.h>#define everything_to_string(x) #x 
/*
#define container_of(ptr,type,member) ({ \const typeof( ((type *)0)->member ) *__mptr = (ptr); \(type *)( (char *)__mptr - offsetof(type,member) );})
*/
//const struct member *__mptr=ptr;
//(struct Student *)( (char *)__mptr - offsetof(type,member) ) 
#define container_of(ptr,type,member) ({ \const typeof( ((type *)0)->member ) *__mptr = (ptr); \(type *)((char *)__mptr - offsetof(type,member)); })struct info {int a;int b;
};
struct Student {char name[4]; // 0int age;      // 4int grade;    // 8int sex;      // 12struct info last;
};int main()
{struct Student student={"123",22,100,1,{10,20}};struct info *ptr=&student.last;struct Student *pstudent = container_of(ptr,struct Student,last);printf("[%p]\n",pstudent);printf("[%p]\n",ptr);return 0;
}

参考资料

  1. https://stackoverflow.com/questions/15832301/understanding-container-of-macro-in-the-linux-kernel
  2. https://gaomf.cn/2017/10/07/C_typeof/
  3. https://www.runoob.com/cprogramming/c-macro-offsetof.html
http://www.mmbaike.com/news/94447.html

相关文章:

  • wordpress 更改后台地址seo排名推广
  • vue做的网站影响收录么网站域名注册查询
  • 微信网站开发软件全网推广平台
  • 青海seo技术培训百度seo优化培训
  • 广州做网站哪里好莆田百度seo公司
  • b2b 网站特点手机网站制作软件
  • 那些网站是vue做的竞价托管哪家便宜
  • 如何查网站的icp备案百度图片识别搜索
  • 网站管理员怎么做联系方式苏州百度搜索排名优化
  • 平面设计培训班学费一般多少百度贴吧企业seo排名外包
  • 制作网站的手机软件软文推广页面
  • 重庆所有做网站的公司有哪些免费发布推广信息的b2b
  • 出站链接对网站有什么影响seo教程视频
  • 河南省富士康现状seo职业
  • 广东网站建设968手机百度下载免费安装
  • 手机网站跳转代码旅游营销推广方案
  • 做便宜的宝贝的网站包头整站优化
  • vr 做的网站百度站长平台
  • wordpress 站点打开慢搜索引擎收录查询工具
  • 北流网站建设seo优化网站模板
  • 制作网站报价个人怎么注册自己的网站
  • 2019 做网站搜狗识图
  • wordpress公司网站杭州网络
  • 怎么建立手机网站中国职业培训在线
  • 网站标题主关键词seo搜索推广费用多少
  • 广州网站建设推广网站优化公司大家好
  • 柳市做网站的公司软文营销的特点
  • 网站建设的日常工作有什么百度推广seo
  • wordpress如何做导航网站今日疫情最新数据
  • android app for wordpress哪家公司做seo