Skip to content

Commit 31a738e

Browse files
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6
1 parent ada1911 commit 31a738e

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

llvm/include/llvm/Support/FileSystem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,9 @@ class mapped_file_region {
13421342
LLVM_ABI size_t size() const;
13431343
LLVM_ABI char *data() const;
13441344

1345+
/// Write changes to disk and synchronize. Equivalent to POSIX msync.
1346+
LLVM_ABI std::error_code sync() const;
1347+
13451348
/// Get a const view of the data. Modifying this memory has undefined
13461349
/// behavior.
13471350
LLVM_ABI const char *const_data() const;

llvm/lib/Support/Unix/Path.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,12 @@ void mapped_file_region::unmapImpl() {
876876
::munmap(Mapping, Size);
877877
}
878878

879+
std::error_code mapped_file_region::sync() const {
880+
if (int Res = ::msync(Mapping, Size, MS_SYNC))
881+
return std::error_code(Res, std::generic_category());
882+
return std::error_code();
883+
}
884+
879885
void mapped_file_region::dontNeedImpl() {
880886
assert(Mode == mapped_file_region::readonly);
881887
if (!Mapping)

llvm/lib/Support/Windows/Path.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,12 @@ void mapped_file_region::unmapImpl() {
10061006

10071007
void mapped_file_region::dontNeedImpl() {}
10081008

1009+
std::error_code mapped_file_region::sync() const {
1010+
if (::FlushViewOfFile(Mapping, 0))
1011+
return std::error_code();
1012+
return mapWindowsError(::GetLastError());
1013+
}
1014+
10091015
int mapped_file_region::alignment() {
10101016
SYSTEM_INFO SysInfo;
10111017
::GetSystemInfo(&SysInfo);

llvm/unittests/Support/Path.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,32 @@ TEST_F(FileSystemTest, FileMapping) {
14711471
ASSERT_NO_ERROR(fs::remove(TempPath));
14721472
}
14731473

1474+
TEST_F(FileSystemTest, FileMappingSync) {
1475+
// Create a temp file.
1476+
auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");
1477+
ASSERT_TRUE((bool)TempFileOrError);
1478+
fs::TempFile File = std::move(*TempFileOrError);
1479+
StringRef Content("hello there");
1480+
ASSERT_NO_ERROR(
1481+
fs::resize_file_before_mapping_readwrite(File.FD, Content.size()));
1482+
{
1483+
// Map in the file and write some content.
1484+
std::error_code EC;
1485+
fs::mapped_file_region MFR(fs::convertFDToNativeFile(File.FD),
1486+
fs::mapped_file_region::readwrite,
1487+
Content.size(), 0, EC);
1488+
ASSERT_NO_ERROR(EC);
1489+
std::copy(Content.begin(), Content.end(), MFR.data());
1490+
1491+
// Synchronize and check the contents before unmapping.
1492+
MFR.sync();
1493+
auto Buffer = MemoryBuffer::getFile(File.TmpName);
1494+
ASSERT_TRUE((bool)Buffer);
1495+
ASSERT_EQ(Content, Buffer->get()->getBuffer());
1496+
}
1497+
ASSERT_FALSE((bool)File.discard());
1498+
}
1499+
14741500
TEST(Support, NormalizePath) {
14751501
// Input, Expected Win, Expected Posix
14761502
using TestTuple = std::tuple<const char *, const char *, const char *>;

0 commit comments

Comments
 (0)