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

Introduce mmap_nofd to work around <F: AsFd>? #2126

Closed
newpavlov opened this issue Sep 8, 2023 · 4 comments · Fixed by #2127
Closed

Introduce mmap_nofd to work around <F: AsFd>? #2126

newpavlov opened this issue Sep 8, 2023 · 4 comments · Fixed by #2127

Comments

@newpavlov
Copy link
Contributor

Today when I use nix's mmap for creating anonymous mappings I have to use mmap::<BorrowedFd>(...), which feels... quite awkward. It would be nice to have a variant of mmap with the following signature:

pub unsafe fn mmap_nofd(
    addr: Option<NonZeroUsize>,
    length: NonZeroUsize,
    prot: ProtFlags,
    flags: MapFlags,
) -> Result<*mut c_void>

The offset argument has meaning only for file-backed mappings, so we can remove it as well.

@newpavlov
Copy link
Contributor Author

Alternatively, it can be named as mmap_anonymous.

@asomers
Copy link
Member

asomers commented Sep 30, 2023

For that matter, is F: AsFd the best argument type for non-anonymous mappings? Would anybody ever want to supply an OwnedFd? If not, then maybe the argument type should be Option<BorrowedFd>.

@SteveLauC
Copy link
Member

For anonymous mmap

Today when I use nix's mmap for creating anonymous mappings I have to use mmap::<BorrowedFd>(...), which feels quite awkward

It is indeed awkward to have to specify the generic argument even though we don't use it at all, so perhaps a dedicated anonymous function would be great here?

BTW, if we are gonna add this function, mmap_anonymous seems to be a better name than mmap_nofd.


For file-backed (non-anonymous) mmap

Is F: AsFd the best argument type for non-anonymous mappings? Would anybody ever want to supply an OwnedFd? If not, then maybe the argument type should be Option<BorrowedFd>.

From the Linux man page:

After the mmap() call has returned, the file descriptor, fd, can be closed immediately without invalidating the mapping.

It seems to be ok to close that fd after calling mmap(..., fd, ...)

@JonathanWoollett-Light
Copy link
Contributor

mmap_anonymous makes the most sense to me.

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 a pull request may close this issue.

4 participants