Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept BorrowedFd in public API methods instead of RawFd #27

Merged
merged 1 commit into from
Nov 14, 2023

Conversation

ids1024
Copy link
Member

@ids1024 ids1024 commented Oct 3, 2023

This should help ensure these function calls aren't used in unsound ways.

This is a breaking API change.

Copy link
Contributor

@MarijnS95 MarijnS95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Looks like the rest of device.rs already uses OwnedFd/BorrowedFd correctly to note ownership transfers.

src/device.rs Outdated
Comment on lines 386 to 391
let fds = [
buffers[0].as_raw_fd(),
buffers[1].as_raw_fd(),
buffers[2].as_raw_fd(),
buffers[3].as_raw_fd(),
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw BorrowedFd is repr(transparent) to have the same layout as RawFd.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this should be equivalent to an unsafe mem::transmute. The compiler hopefully will recognize this and generate the same code. It could be changed, but my inclination is generally to stick with safe code.

It would also be possible to define gbm_import_fd_modifier_data to use a lifetime bound and BorrowedFd, but that's not so practical when the struct is in a generated file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, actually, I suppose this should accept [Option<BorrowedFd<'_>>; 4], or you can't pass less than four file descriptors. I kind of wish something like arrayvec were in std...

Should still transmute, with None mapping to -1, I think. Presumably passing -1 in place of an fd to GBM will return an error, but is safe.

I guess a more idiomatic way to define something like import_buffer_object_from_dma_buf_with_modifiers would be a builder pattern with a method to add planes? And an error if you add more than 4.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, and looks like you also discovered that .map() on a sized array gives back an array of the same size 👍

This should help ensure these function calls aren't used in unsound
ways.

This is a breaking API change.
@Drakulix Drakulix merged commit 71e76f5 into Smithay:master Nov 14, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants