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

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 14 15:40:00 PDT 2025


aganea wrote:

The following passes the test:

```
TEST_F(FileSystemTest, FileMappingSync) {
  // Create a temp file.
  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");
  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);
    ASSERT_NO_ERROR(EC);
    std::copy(Content.begin(), Content.end(), MFR.data());

    // Synchronize and check the contents before unmapping.
    MFR.sync();
  }
  std::string Name = File.TmpName;
  ASSERT_FALSE((bool)File.keep());
  auto Buffer = MemoryBuffer::getFile(Name);
  if (!Buffer)
    llvm::outs() << "failed to open \'" << Name
                 << "\': " << Buffer.getError().message() << "\n";
  ASSERT_TRUE((bool)Buffer);
  ASSERT_EQ(Content, Buffer->get()->getBuffer());
  ASSERT_NO_ERROR(fs::remove(Name));
}
```

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


More information about the llvm-commits mailing list