title: How can I ensure that a memory-mapped file keeps the memory pages accessible?
body: <p>I am using Qt to map a file to a piece of memory pages</p>
  
  <pre><code>QFile::map (qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions)
  </code></pre>
  
  <p>Essentially, this should be a <code>mmap</code> system function call. I wonder how I can guarantee that I can access the returned memory, even if the file on disk is truncated. I seem to need this because I read from a disk file and want to gracefully handle errors</p>
  
  <pre><code>if (offset &gt; m_file.size()) 
    // throw an error...
  if (m_mappedFile != NULL) return m_mappedFile + offset;
  </code></pre>
  
  <p>Obviously, this contains a race condition, because the file size may change in between the check and the access to the mapping. How can this be avoided?</p>