【转】container_of宏 分析】的更多相关文章

offsetof宏:结构体成员相对结构体的偏移位置 container_of:根据结构体成员的地址来获取结构体的地址 offsetof 宏 原型: #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) (TYPE *)0非常巧妙,告诉编译器有一个指向结构体 TYPE 的指针,其地址是0,然后取该指针的 MEMBER 地址 &((TYPE *)0)->MEMBER,因为基址是0,所以这时获取到的 MEMBER…
在学习Linux驱动的过程中,遇到一个宏叫做container_of.该宏定义在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 thi…
问题:如何通过结构中的某个变量获取结构本身的指针??? 关于container_of宏定义在[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 embed…
转自:http://blog.chinaunix.net/uid-30254565-id-5637597.html 内核中container_of宏的详细分析 16年2月28日09:00:37 内核中有一个大名鼎鼎的宏-----container_of():这个宏定义如下所示,为了表示一下敬意,我就把注释一起粘贴下来了: /** * container_of - cast a member of a structure out to the containing structure * @ptr…
container_of宏剖析//该宏位于include/linux/kernel.h 1.定义格式 /** * 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 nam…
offsetof 宏 #include<stdio.h> #define offsetoff(type, member)      ((int)&((type*)0)->member) /* ((type*)0)->member 释义:声明一个相应类型的结构体指针,该指针指向0地址处.再通过该指针访问各元素.我们只要获取一个指针就能访问其内部的元素吗,可以这么搞?其实我是想联系全局和非全局变量,通过上面这个代码也许你不明白我要表达的意思.请继续慢慢看,直到本文后面,结合本文…
上一节拒绝造轮子!如何移植并使用Linux内核的通用链表(附完整代码实现)我们在分析Linux内核链表的时候注意到内核在求解结构体偏移的时候巧妙的使用了container_of宏定义,今天我们来详细剖析下内核到底是如何求解结构体成员变量的地址的. @ 目录 结构体在内存中是如何存储的 container_of宏 typeof (((type *)0)->member) const typeof(((type )0)->member)__mptr = (ptr); offsetof(type,…
linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER)    ((size_t) &((TYPE *)0)->MEMBER) /** * container_of - cast a member of a structure out to the containing structure * @ptr:        the pointer to the member. * @type:       the type…
1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址来获取结构体的地址.两个宏设计的很巧妙,值得学习.linux内核中有着两个宏的定义,并在链表结构中得到应用.不得不提一下linux内核中的链表,设计的如此之妙,只需要两个指针就搞定了.后续认真研究一下这个链表结构. 2.offsetof宏 使用offsetof宏需要包含stddef.h头文件,实例可…
1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址来获取结构体的地址.两个宏设计的很巧妙,值得学习.linux内核中有着两个宏的定义,并在链表结构中得到应用.不得不提一下linux内核中的链表,设计的如此之妙,只需要两个指针就搞定了.后续认真研究一下这个链表结构. 2.offsetof宏 使用offsetof宏需要包含stddef.h头文件,实例可…