3

Rust libc use repr(packed) as shown here for struct which will be then passed to system libc. For example, utsname is repr(packed) and then used in fn uname As per the doc mentioned here,

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?

1 Answer 1

3

Then why does rust libc use repr(packed) and not repr(C) for passing struct to system libc?

One obvious reason is that the equivalent structures are specified as packed on the C side as well. (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:

#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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.