The Magical container_of() Macro

本文深入解析了Linux内核中container_of宏的工作原理及其巧妙的设计思路。container_of宏通过指针、容器类型及成员名三个参数,逆向定位到包含特定成员的结构体。文章详细解释了宏的实现细节,包括GNU C语言扩展特性、零指针引用技巧等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html#Statement-Exprs

The Magical container_of() Macro

When you begin with the kernel, and you start to look around and read the code, you will eventually come across this magical preprocessor construct.

What does it do? Well, precisely what its name indicates. It takes three arguments – a pointertype of the container, and the name of the member the pointer refers to. The macro will then expand to a new address pointing to the container which accommodates the respective member. It is indeed a particularly clever macro, but how the hell can this possibly work? Let me illustrate…

The first diagram illustrates the principle of the container_of(ptr, type,member) macro for who might find the above description too clumsy.

The container_of macroIllustration of how the containter_of macro works.

Bellow is the actual implementation of the macro from Linux Kernel:

#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

At first glance, this might look like a whole lot of magic, but it isn’t quite so. Let’s take it step by step.

Statements in Expressions

The first thing to gain your attention might be the structure of the whole expression. The statement should return a pointer, right? But there is just some kind of weird ({}) block with two statements in it. This in fact is a GNU extension to C language called braced-group within expression. The compiler will evaluate the whole block and use the value of the last statement contained in the block. Take for instance the following code. It will print 5.

int x = ({1; 2;}) + 3;
printf("%d\n", x);

typeof()

This is a non-standard GNU C extension. It takes one argument and returns its type. Its exact semantics is throughly described in gcc documentation.

int x = 5;
typeof(x) y = 6;
printf("%d %d\n", x, y);

Zero Pointer Dereference

But what about the zero pointer dereference? Well, it’s a little pointer magic to get the type of the member. It won’t crash, because the expression itself will never be evaluated. All the compiler cares for is its type. The same situation occurs in case we ask back for the address. The compiler again doesn’t care for the value, it will simply add the offset of the member to the address of the structure, in this particular case 0, and return the new address.

struct s {
        char m1;
        char m2;
};

/* This will print 1 */
printf("%d\n", &((struct s*)0)->m2);

Also note that the following two definitions are equivalent:

typeof(((struct s *)0)->m2) c;
char c;

offsetof(st, m)

This macro will return a byte offset of a member to the beginning of the structure. It is even part of the standard library (available in stddef.h ). Not in the kernel space though, as the standard C library is not present there. It is a little bit of the same 0 pointer dereference magic as we saw earlier and to avoid that modern compilers usually offer a built-in function, that implements that. Here is the messy version (from the kernel):

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

It returns an address of a member called MEMBER of a structure of type TYPE that is stored in memory from address 0 (which happens to be the offset we’re looking for).

Putting It All Together

#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})

When you look more closely at the original definition from the beginning of this post, you will start wondering if the first line is really good for anything. You will be right. The first line is not intrinsically important for the result of the macro, but it is there for type checking purposes. And what the second line really does? It subtracts the offset of the structure’s member from its address yielding the address of the container structure. That’s it!

After you strip all the magical operators, constructs and tricks, it is that simple :-).

EnMAP-Box是一款高效、便捷的遥感图像处理软件,其独特之处在于它是一个免安装的应用程序,用户可以直接运行而无需进行复杂的安装过程。这款工具主要用于处理和分析来自各种遥感传感器的数据,如EnMAP(环境多波段光谱成像仪)和其他同类设备获取的高光谱图像。EnMAP-Box的设计目标是为科研人员和实践工作者提供一个直观、易用的平台,以执行复杂的遥感数据处理任务。 在使用EnMAP-Box之前,一个关键的前提条件是需要有一个兼容的IDL(Interactive Data Language)环境。IDL是一种强大的编程语言,特别适用于科学数据的处理和可视化,尤其是在地球科学和遥感领域。它提供了丰富的库函数,支持对多维数组操作,这使得它成为处理遥感图像的理想选择。EnMAP-Box是基于IDL开发的,因此,用户在使用该软件之前需要确保已经正确配置了IDL环境。 EnMAP-Box的主要功能包括: 1. 数据导入:能够读取多种遥感数据格式,如ENVI、HDF、GeoTIFF等,方便用户将不同来源的遥感图像导入到软件中进行分析。 2. 预处理:提供辐射校正、大气校正、几何校正等功能,用于改善原始图像的质量,确保后续分析的准确性。 3. 分光分析:支持高光谱图像的光谱特征提取,如光谱指数计算、光谱端元分离等,有助于识别地物类型和监测环境变化。 4. 图像分类:通过监督或非监督方法进行图像分类,可以自动或半自动地将图像像素划分为不同的地物类别。 5. 时间序列分析:对于多时相遥感数据,EnMAP-Box能进行时间序列分析,揭示地表动态变化趋势。 6. 结果导出与可视化:处理后的结果可以导出为各种格式,同时软件内置了图像显示和地图投影功能,帮助用户直观地查看和理解处理结果。 7. 自定义脚本:利用IDL的强大功能,用户可以编写自定义脚本来实现特定的遥感处理需求,增强了软件的灵活性和可扩展性。 在使用EnMAP-Box的过程中,用户可能会遇到一些挑战,例如对IDL编程语言不熟悉,或者对遥感数据处理的基本概念和方法缺乏了解。这时,可以通过查阅软件自带的文档、教程,以及在线资源来提升技能。同时,积极参与相关的学习社区和论坛,与其他用户交流经验,可以帮助解决遇到的问题。 EnMAP-Box作为一款基于IDL的遥感图像处理工具,为遥感数据分析提供了便利,但需要用户具备一定的IDL基础和遥感知识。通过熟练掌握EnMAP-Box,用户可以高效地处理和解析遥感数据,揭示地表信息,为环境保护、资源管理等领域提供科学支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值