![](/img/trans.png)
[英]How does a C-style struct with a bitfield get represented in a Rust #[repr(C)] struct?
[英]Why does rust libc use repr(packed) for struct?
Rust libc 使用repr(packed)
作為結構,然后傳遞給系統 libc。 例如, 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),
那么為什么 rust libc 使用repr(packed)
而不是repr(C)
將結構傳遞給系統 libc?
那么為什么 rust libc 使用
repr(packed)
而不是repr(C)
將結構傳遞給系統 libc?
一個明顯的原因是等效結構也被指定為在 C 端打包。 (許多 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;
這同樣適用於其他示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.