linux内核container_of宏获取结构体地址
2016-05-29 22:59
513 查看
先mark一下 linux内核container_of宏
/* * ===================================================================================== * * Filename: contion_of.cpp * * Description: * * Version: 1.0 * Created: 2016年05月29日 17时00分20秒 * Revision: none * Compiler: gcc * * Author: * Organization: * * ===================================================================================== */ #include <stdio.h> #include <stdlib.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 offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #define container_of(ptr, type, member) ({\ const typeof( ((type *)0)->member ) *__mptr = (ptr);\ (type *)( (char *)__mptr - offsetof(type,member) );}) struct A{ unsigned int c; char a; char b; }; int main(int argc, char** argv){ struct A tmp; tmp.c = 100; tmp.b = 'c'; char *a = &tmp.b; struct A *p = container_of(a,struct A,b); printf("tmp.c = %u ,A.c = %u\n",tmp.c,p->c); return 0; }
相关文章推荐
- linux进程通信--无名管道
- Linux系统如何写C/C++程序
- Linux 入门——适合初学者本文转载于的博客原文
- nfs 挂载问题
- Linux内核同步机制之completion
- Linux查看程序端口占用情况
- Linux学习路线指南
- linux学习之-linux软件包管理
- Linux系统设置静态IP地址
- LINUX6.5 + MYSQL5.6 + JIRA6.3 + CONFLUENCE5.9
- Centos6.7_KVM安装配置使用
- Linux 端口防火墙
- Linux 中查看文件第n行内容的命令
- Linux学习笔记之一:文件目录管理
- linux测试文件是否存在命令集合
- linux 进程管理
- Archlinux下virtualbox报错'/sbin/rcvboxdrv setup'
- KaLi Linux Rolling 2016 学习笔记
- VirtualBox 安装 Centos 7 笔记
- vim、g++、gdb及makefile的学习