首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >__mptr在最新的container_of宏中的用途是什么?

__mptr在最新的container_of宏中的用途是什么?
EN

Stack Overflow用户
提问于 2022-05-01 04:53:11
回答 1查看 218关注 0票数 2

我正在阅读Linux5.17.5内核,现在查看container_of()宏。

代码语言:javascript
运行
复制
/**
 * 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 container_of(ptr, type, member) ({              \
    void *__mptr = (void *)(ptr);                   \
    static_assert(__same_type(*(ptr), ((type *)0)->member) ||   \
              __same_type(*(ptr), void),            \
              "pointer type mismatch in container_of()");   \
    ((type *)(__mptr - offsetof(type, member))); })

我的问题很简单:__mptr的目的是什么,我可以这样用(void *)ptr代替__mptr吗?

代码语言:javascript
运行
复制
#define container_of(ptr, type, member) ({          \
    static_assert(__same_type(*(ptr), ((type *)0)->member) ||   \
              __same_type(*(ptr), void),            \
              "pointer type mismatch in container_of()");   \
    ((type *)((void*)ptr - offsetof(type, member))); })
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-01 06:15:41

由于“kernel.h:在container_of()中更好地处理指向数组的指针”,__mptr不再用于类型检查。它用于消除scripts/gcc-plugins/randomize_layout_plugin.c的信息信息,如inform(gimple_location(stmt), "found mismatched ssa struct pointer types: %qT and %qT\n", ptr_lhs_type, ptr_rhs_type);。该文件的工作之一是:

代码语言:javascript
运行
复制
/*
 * iterate over all statements to find "bad" casts:
 * those where the address of the start of a structure is cast
 * to a pointer of a structure of a different type, or a
 * structure pointer type is cast to a different structure pointer type
 */

如果缺少__mptr,宏将包括您所说的代码:

(type *)((void*)ptr - offsetof(type, member)))

(PS:char *在这里更好,因为iso c标准保证offsetof为1字节,void *仅由gnu c保证)

如果offsetof获得零,并且ptr将包含struct类型member的开始地址,那么这将转换为完全不同的类型:struct type *。此表单每个scripts/gcc-plugins/randomize_layout_plugin.c都会出现故障,并会被它检测到。

随着void *__mptr = (void *)(ptr);的引入,编译器不再知道__mptr的类型了,所以当将void * __mptr转换为(type *)时,scripts/gcc-plugins/randomize_layout_plugin.c不会抱怨,您可以从https://lkml.org/lkml/2017/6/20/873看到实际的情况和相关的修复补丁

下面是用于类型检查的4.13之前的内核的原始答案:

让我们简化container_of,看看这个例子:

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stddef.h>
#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})
#define container_of_without_typechecking(ptr, type, member) ({         \
    (type *)( (char *)ptr - offsetof(type,member) );})
struct Foo
{
    int a;
};
int main(int argc, char *argv[]) {
    struct Foo foo;
    int *a;

    printf("foo addr: %p\n", &foo);
    a = &foo.a;
    printf("container of a: %p\n", container_of_without_typechecking((unsigned long long*)a, struct Foo, a));
    printf("typecheck: container of a: %p\n", container_of((unsigned long long*)a, struct Foo, a));
    return 0;
}

container_of_without_typechecking没有__mptr,但container_of有。

汇编时:

代码语言:javascript
运行
复制
a.c: In function ‘main’:
a.c:13:55: warning: initialization of ‘const int *’ from incompatible pointer type ‘long long unsigned int *’ [-Wincompatible-pointer-types]
   13 |         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
      |                                                       ^
a.c:28:47: note: in expansion of macro ‘container_of’
   28 |     printf("typecheck: container of a: %p\n", container_of((unsigned long long*)a, struct Foo, a));
      |                                               ^~~~~~~~~~~~

如您所见,container_of抛出一个incompatible pointer type警告,而container_of_without_typechecking不抛出,因此它只是用于类型检查。

另外,请注意,Linux内核将此警告视为错误:

代码语言:javascript
运行
复制
KBUILD_CFLAGS   += $(call cc-option,-Werror=incompatible-pointer-types)

因此,如果您将错误的类型传递给,您将得到一个错误,而不是一个警告。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72074089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档