container_of

 1 /**
 2  * container_of - cast a member of a structure out to the containing structure
 3  * @ptr:    the pointer to the member.
 4  * @type:   the type of the container struct this is embedded in.
 5  * @member: the name of the member within the struct.
 6  *
 7  */
 8 #define container_of(ptr, type, member) ({           9     const typeof( ((type *)0)->member ) *__mptr = (ptr);    10     (type *)( (char *)__mptr - offsetof(type,member) );})

它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针。

1 #undef offsetof
2 #ifdef __compiler_offsetof
3 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
4 #else
5 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
6 #endif
时间: 2024-07-28 13:16:04

container_of的相关文章

linux内核宏container_of

首先来个简单版本 1 /* given a pointer @ptr to the field @member embedded into type (usually 2 * struct) @type, return pointer to the embedding instance of @type. */ 3 #define container_of(ptr, type, member) 4 ((type *)((char *)(ptr)-(char *)(&((type *)0)->

Linux Kernel:  container_of 黑科技

1 #include<stdio.h> 2 3 #define offsetof(TYPE,MEMBER) ((size_t)&((TYPE*)0)->MEMBER) 4 5 #define container_of(ptr,type,member) ({ 6 const typeof( ((type*)0)->member ) *__mptr = (ptr); 7 (type *)( (char *)__mptr - offsetof(type,member) );})

C语言笔记(结构体与offsetof、container_of之前的关系)

关于结构体学习,需要了解:结构体的定义和使用.内存对齐.结构体指针.得到结构体元素的偏移量(offsetof宏实现) 一.复习结构体的基本定义和使用 1 typedef struct mystruct 2 { 3 int a; 4 char b; 5 double c; 6 }MyS1; 7 8 /* 9 函数功能:演示结构体的定义和使用 10 */ 11 void func1(void) 12 { 13 //定义时赋值 14 MyS1 s1 = { 15 .a =1, 16 .b =2, 17

typeof, offsetof 和container_of

要理解Linux中实现的双向循环链表("侵入式"链表),首先得弄明白宏container_of. 本文尝试从gcc的关键字typeof和宏offsetof入手,循序渐进地剖析宏container_of之实现原理. 1. typeof (from: https://en.wikipedia.org/wiki/Typeof) typeof is an operator provided by several programming languages to determine the da

深入浅出实例解析linux内核container_of宏

做一件事情首先应该知道它的目的是什么. container_of的目的:如何通过结构中的某个变量获取结构本身的指针. 总体思路:假想一下,你的结构体中有好几个成员,你如何通过里面的"任一成员"获取整个结构体的首地址呢.container_of的做法就是通过typeof定义一个与"任一成员"同类型的指针变量pvar_a(假设变量名就是pvar_a),并让指针变量pvar_a指向这个"任一成员",然后用 "pvar_a的地址" 减

第十七天:APCS和container_of宏

APCS 全称:ARM 过程调用标准 如果要写用来与编译后的 C 连接的汇编代码,则必须使用 APCS. 今天的课程最终的两个目标:使用符合APCS标准的汇编写输出hello world  以及编写container_of宏 .这两个的推导过程比较复杂和具有跳跃性.结论的话要记住两个知识点,一:编写汇编函数的时候要对相应的寄存器进行保护.保护代码需要记住,每次写函数的时候都要用到.二:container_of实现了根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针的功能.以

Samsung_tiny4412(笔记)--&gt;volatile,container_of,file_operations,file,inode

/*********************************************************************************** * * Samsung_tiny4412(笔记)-->volatile,container_of,file_operations,file,inode * * 声明: * 本文的结构体的注释主要是参考网络上的解释,几乎无任何个人理解,主要是为后续 * 代码编辑提供参考. * * 2015-3-8 阴 深圳 尚观 Etc 曾剑锋

C语言之offset_of宏和container_of宏

通过结构体整体变量来访问其中各个元素,本质上是通过指针方式来访问的,形式上是通过.的方式来访问的(这时候其实是编译器帮我们自动计算了偏移量). 1:offset_of宏 作用:计算结构体中某个元素和结构体首地址的偏移量(其实质是通过编译器来帮我们计算). 定义: #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER) 参数分析:TYPE是结构体类型,MEMBER是结构体中一个元素的元素名 返回值:member元素相对于整个结构体

C语言之offsetof宏和container_of宏

首先我们要明白一点通过结构体变量来访问结构体中的各个元素时,其本质上是 通过指针的方式来实现访问的,只不过是这个时候编译器帮自动帮我们计算了每个 元素与结构体起始地址之间的偏移量而已 一:offsetof宏: #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER) 1:参数与返回值分析: (1)TYPE是结构体类型,MEMBER是结构体中一个元素的元素名 (2)这个宏返回的是member元素相对于整个结构体变量的首地址的偏移量,