[llvm] [Support] Add mapped_file_region::sync(), equivalent to msync (PR #153632)

Steven Wu via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 14 15:42:37 PDT 2025


cachemeifyoucan wrote:

> The following passes the test

Thanks. I don't know the deleteOnClose file will impact reading before the file is closed!

I will take your example and update the test with a tweak. I think the better check is to make sure file can be read with correct content before memory region is unmapped.

```
  SmallString<0> TempPath(TestDirectory);
  sys::path::append(TempPath, "test-%%%%");
  auto TempFileOrError = fs::TempFile::create(TempPath);
  ASSERT_TRUE((bool)TempFileOrError);
  fs::TempFile File = std::move(*TempFileOrError);
  StringRef Content("hello there");
  std::string FileName = File.TmpName;
  ASSERT_NO_ERROR(
      fs::resize_file_before_mapping_readwrite(File.FD, Content.size()));
  {
    // Map in the file and write some content.
    std::error_code EC;
    fs::mapped_file_region MFR(fs::convertFDToNativeFile(File.FD),
                               fs::mapped_file_region::readwrite,
                               Content.size(), 0, EC);

    // Keep the file so it can be read.
    ASSERT_FALSE((bool)File.keep());

    // Write content through mapped memory.
    ASSERT_NO_ERROR(EC);
    std::copy(Content.begin(), Content.end(), MFR.data());

    // Synchronize to file system.
    ASSERT_FALSE((bool)MFR.sync());

    // Check the file content using file IO APIs.
    auto Buffer = MemoryBuffer::getFile(FileName);
    ASSERT_TRUE((bool)Buffer);
    ASSERT_EQ(Content, Buffer->get()->getBuffer());
  }
  // Manually remove the test file.
  ASSERT_FALSE((bool)fs::remove(FileName));

```

https://github.com/llvm/llvm-project/pull/153632


More information about the llvm-commits mailing list