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

Using memmap2 with RawFd #18

Closed
mripard opened this issue Jun 9, 2021 · 1 comment · Fixed by #19
Closed

Using memmap2 with RawFd #18

mripard opened this issue Jun 9, 2021 · 1 comment · Fixed by #19

Comments

@mripard
Copy link

mripard commented Jun 9, 2021

Even though files are the most obvious target for an mmap, we might want to call mmap on a file descriptor that isn't backed by a File. Buffers on Linux can be allocated that way for example.

The current API that takes a File doesn't really allow to use the crate in such a case.

Something that would work would be

let buffer_fd = allocate_buffer(whatever);
let file = unsafe { File::from_raw_fd(buffer_fd) };
let mut mmap = unsafe {
    MmapOptions::new().map_mut(&file)?
};

However, FrowRawFd consumes the ownership of the FD, therefore making it fairly hard to integrate into a larger buffer management codebase.

We could switch from File to AsRawFd the API, but I'm not sure whether that change in the API would be welcome?

@RazrFalcon
Copy link
Owner

Well, we have to support Windows too and AsRawFd is Unix-only. I guess we have to provide some kind of wrapper that would accept File, RawFd and RawHandle. Basically:

pub unsafe fn map<T: Into<OurWrapper>>(&self, src: T) -> Result<Mmap> {
    // ...
}

Patches are welcome.

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.

2 participants