linux 内核container_of
来源:互联网 发布:激光内雕机软件 编辑:程序博客网 时间:2024/06/02 20:07
#include <stdio.h>#define offsetof(type, number) ((size_t) &((type *)0)->number)#define container_of(ptr,type,member) ({ \ const typeof( ((type *)0)->member ) * _mptr = (ptr); \ (type *)((char *)_mptr - offsetof(type,member) );}) struct structtest { int number; char name; float code;};int main(void){ struct structtest test = {99,'c',3.14}; char * temp = &test.name; printf ("the pointer is %p\n",&test.number); printf ("the pointer is %p\n",&test.name); printf ("the pointer is %p\n",&test.code); printf ("the pointer is %p\n",ttemp); struct structtest * structtemp = container_of(temp,struct structtest,name); printf ("the pointer is %p\n",structtemp); return 0;}
container_of根据结构体成员指针,得到结构体首地址。
这里首先将结构体首地址指向0地址 ,之后根据typeof得到( ((type *)0)->member )指向的类型 定义类型指针,赋值
const typeof( ((type )0)->member ) _mptr = (ptr);
这里根据传入的结构体类型以及成员,得到相对应的偏移值 首先将结构体指向0地址,之后把指向number的指针转换为整数
#define offsetof(type, number) ((size_t) &((type *)0)->number)
先把成员指针值转换为char× 之后进行相减 就可以得到结构体首地址
(type )((char )_mptr - offsetof(type,member)
阅读全文
0 0
- Linux内核中的container_of()
- Linux内核container_of宏
- linux内核中的container_of
- Linux内核基础-container_of
- linux内核container_of详解
- linux内核中container_of
- Linux 内核list_head container_of
- Linux内核container_of详解
- Linux内核,container_of 宏
- linux 内核container_of
- linux内核中的container_of宏
- Linux内核的Container_of机制
- linux内核中container_of详解
- linux内核中container_of详解
- Linux内核中的container_of宏
- Linux内核:container_of宏分析
- linux内核container_of宏解析
- Linux内核Container_of宏解析
- python模块
- Flink入门教程--Component Stack
- android获取存储目录(路径)的几种方式和注意事项
- 解决myEclipse未保存完全后导致打不开的问题
- JAVA的MVC设计模式
- linux 内核container_of
- 用户访问端 移动端,PC端,微信等常用平台和浏览器判断
- 239. Sliding Window Maximum 滑动窗口最大值
- mysql导出数据乱码
- PHP cURL中CURLOPT_CONNECTTIMEOUT和CURLOPT_TIMEOUT的区别
- Test5
- 初识Android Studio
- 111. Minimum Depth of Binary Tree
- 解决WinDbg调试VMWare虚拟机连接不上