mmap beyond end of file mmap beyond end of file unix unix

mmap beyond end of file


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.


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.