I have several files that I want to map into a virtual address space consecutively with mmap(). Each file length is a multiple of the page size.
For the first file, I call mmap() with the addr parameter set to nullptr. On the next call, I set the addr parameter to the address after the end of the region that the first file was mapped to.
In some cases, I am unable to map all files because mmap() ignores the addr hint. Is there a way I can pre-allocate the entire region up front and then map the files into the region one at a time?
I'd like to thank @gspr for their help with this question!
To ensure you can
mmap()all of the files consecutively, you must do the following:mmap()an anonymous region with a size equal to the sum of the file sizes.mmap()each file one at a time within the pre-allocated region. Importantly, use theMAP_FIXEDflag.MAP_FIXEDinstructsmmap()to use the exact address specified in theaddrparameter. If a mapping already exists foraddr, the mapping is overwritten. Thus, it is important that we pre-allocate the entire region upfront in step (1) so that we do not inadvertently overwrite an existing mapping for something else in step (2) when we useMAP_FIXED.