由于“kernel.h: handle pointers to arrays better in 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标准保证它是byte for
offsetof,
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
将不再抱怨
下面是4.13之前的内核的原始答案,当时__mptr
用于类型判断:
让我们简化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)
因此,如果将错误的类型传递到container_of
,则会得到error而不是警告.