Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mmap beyond end of file

Tags:

unix

file-io

mmap

I have mapped a file onto memory using mmap() and I need to write beyond end of file. I cannot find any conclusive documentation evidence of what happens in this case. This document says access beyond EOF is undefined. Is there any correct way to do this?

like image 862
varrunr Avatar asked Nov 30 '12 22:11

varrunr


2 Answers

Unmap the file, extend it by any conventional means (fseek to end + fwrite) and mmap it again it. This should be quite fast; any parts of the file that are paged in would stay paged in.

You can also extend the file while it is mapped and then mremap it. This should work fine, because you are not changing any of the mapped pages. The effect of this may be OS-dependent.

Linux mmap manpage:

The effect of changing the size of the underlying file of a mapping on the pages that correspond to added or removed regions of the file is unspecified.

like image 74
Alex I Avatar answered Oct 08 '22 19:10

Alex I


I would just use truncate(2) or ftruncate(2) first to set the length I want (it makes files bigger too, despite the name) and then mmap(2) it. This will do exactly what you want regardless of what mmap(2) does when you write beyond the original EOF.

This (or some other documented method of file extension) is required for a portable program anyway, as both the Posix and the Linux man page states that the file is not extended by writing beyond the end.

like image 26
DigitalRoss Avatar answered Oct 08 '22 18:10

DigitalRoss