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

Consider supporting private, copy-on-write mappings #42

Open
jimmehc opened this issue Apr 2, 2019 · 1 comment
Open

Consider supporting private, copy-on-write mappings #42

jimmehc opened this issue Apr 2, 2019 · 1 comment

Comments

@jimmehc
Copy link

jimmehc commented Apr 2, 2019

I really like this library due to its simplicity. However, one thing its missing for my use case is support for private, copy-on-write mappings.

In other words, I want to be able to map a file and edit the mapping, but not have edits persisted to the underlying file, which is typically implemented using private, copy-on-write pages under the covers.

On Windows this means passing FILE_MAP_COPY to MapViewOfFile, instead of FILE_MAP_READ or FILE_MAP_WRITE. I believe the MSDN documentation is incorrect for this, as it says FILE_MAP_COPY should be ORed with FILE_MAP_READ, FILE_MAP_WRITE or FILE_MAP_ALL_ACCESS. This stackoverflow post indicates that this is wrong, and FILE_MAP_COPY should just be passed on its own. This is what boost.iostreams does.

On Linux, this means passing MAP_PRIVATE via flags to mmap. It's not entirely clear to me what difference PROT_READ vs. PROT_WRITE makes if MAP_PRIVATE is passed via flags.

The simplest way to support this seems to be to add a new access_mode value - maybe copy, copy_on_write, or private. FILE_MAP_COPY should be passed to MapViewOfFile on Windows, and PROT_READ and MAP_PRIVATE should be passed to mmap on Linux. However, this depends on whether I've overlooked some important subtlety on Linux when PROT_READ vs PROT_WRITE is passed to mmap with MAP_PRIVATE. If so, perhaps a new mapping_mode enum with shared and private values would be desirable.

I'm happy to take a stab at this myself if you can give me your thoughts on what the interface for this should be.

@vimpunk
Copy link
Owner

vimpunk commented Apr 3, 2019

@jimmehc I'm glad to hear you like this library! I'd be more than happy for you to send a PR :)

I think what you propose--adding an entry to access_mode--is indeed the simplest way, and how I would attempt to do this as well (due in large part to the pain of adding another template parameter to the class and thus having to update all out-of-class definitions 😀).
Moreover, having a separate mapping_mode parameter would allow for a read-only private mapping (with the combination of access_mode::read and mapping_mode::private), of which there is probably little use, but please correct me if I'm wrong.

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

No branches or pull requests

2 participants