[英]Why does rust libc use repr(packed) for struct?
Rust libc use repr(packed)
as shown here for struct which will be then passed to system libc. Rust libc 使用
repr(packed)
作为结构,然后传递给系统 libc。 For example, utsname is repr(packed)
and then used in fn uname As per the doc mentioned here ,例如, utsname是
repr(packed)
然后在fn uname中使用根据 此处提到的文档,
repr(packed) forces Rust to strip any padding, and only align the type to a byte. This may improve the memory footprint, but will likely have other negative side-effects.
In particular, most architectures strongly prefer values to be aligned. This may mean the unaligned loads are penalized (x86),
Then why does rust libc use repr(packed)
and not repr(C)
for passing struct to system libc?那么为什么 rust libc 使用
repr(packed)
而不是repr(C)
将结构传递给系统 libc?
Then why does rust libc use
repr(packed)
and notrepr(C)
for passing struct to system libc?那么为什么 rust libc 使用
repr(packed)
而不是repr(C)
将结构传递给系统 libc?
One obvious reason is that the equivalent structures are specified as packed on the C side as well.一个明显的原因是等效结构也被指定为在 C 端打包。 (Many C compilers support "packed" as a non-standard extension with the same meaning as in Rust.) The definition of
epoll_event
on Linux confirms this: (许多 C 编译器支持“packed”作为非标准扩展,其含义与 Rust 相同。)
epoll_event
在 Linux 上的定义证实了这一点:
#ifdef __x86_64__
#define EPOLL_PACKED __attribute__((packed))
#else
#define EPOLL_PACKED
#endif
struct epoll_event {
__u32 events;
__u64 data;
} EPOLL_PACKED;
The same should apply to the other examples.这同样适用于其他示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.