(linux)container_of()宏】的更多相关文章

  在学习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 stru…
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…
首先来个简单版本 /* given a pointer @ptr to the field @member embedded into type (usually * struct) @type, return pointer to the embedding instance of @type. */ #define container_of(ptr, type, member) \ ((type *)(()->member))) 作用:主要用于结构体,给定一个指针ptr指向一个结构体type…
问题:如何通过结构中的某个变量获取结构本身的指针??? 关于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…
上一节拒绝造轮子!如何移植并使用Linux内核的通用链表(附完整代码实现)我们在分析Linux内核链表的时候注意到内核在求解结构体偏移的时候巧妙的使用了container_of宏定义,今天我们来详细剖析下内核到底是如何求解结构体成员变量的地址的. @ 目录 结构体在内存中是如何存储的 container_of宏 typeof (((type *)0)->member) const typeof(((type )0)->member)__mptr = (ptr); offsetof(type,…
1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址来获取结构体的地址.两个宏设计的很巧妙,值得学习.linux内核中有着两个宏的定义,并在链表结构中得到应用.不得不提一下linux内核中的链表,设计的如此之妙,只需要两个指针就搞定了.后续认真研究一下这个链表结构. 2.offsetof宏 使用offsetof宏需要包含stddef.h头文件,实例可…
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…
我们在内核中经常遇到初始化函数是这样定义的:static int __init init_func(); ,与普通函数相比,定义中多了__init.那么,__init是什么意思呢?还有与其匹配的__exit呢? __init* macro __init定义在:include/linux/init.h #define __init __attribute__ ((__section__ (".init.text")))#define __initdata __attribute__ ((…
1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址来获取结构体的地址.两个宏设计的很巧妙,值得学习.linux内核中有着两个宏的定义,并在链表结构中得到应用.不得不提一下linux内核中的链表,设计的如此之妙,只需要两个指针就搞定了.后续认真研究一下这个链表结构. 2.offsetof宏 使用offsetof宏需要包含stddef.h头文件,实例可…
title: container_of宏 date: 2019/7/24 15:49:26 toc: true --- container_of宏 解析 在linux链表结构中有这样一个宏,通过成员变量的地址找到他所在结构体的首地址,通过一个容器(结构体)中某个成员的指针得到指向这个容器(结构体)的指针,简单的说就是通过成员找容器. 计算成员结构体的偏移量,实现offsetof的功能 成员变量地址-偏移量=结构体首地址 #define list_entry(ptr, type, member)…