我正在阅读Linux5.17.5内核,现在查看container_of()宏。
/**
* 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
吗?
#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))); })
发布于 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);
。该文件的工作之一是:
/*
* 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
,看看这个例子:
#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
有。
汇编时:
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内核将此警告视为错误:
KBUILD_CFLAGS += $(call cc-option,-Werror=incompatible-pointer-types)
因此,如果您将错误的类型传递给,您将得到一个错误,而不是一个警告。
https://stackoverflow.com/questions/72074089
复制相似问题