[PATCH] D95494: Support: Add mapped_file_region::sync(), equivalent to msync
Duncan P. N. Exon Smith via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 12 11:48:43 PST 2021
dexonsmith updated this revision to Diff 323425.
dexonsmith added a comment.
ping / rebase
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D95494/new/
https://reviews.llvm.org/D95494
Files:
llvm/include/llvm/Support/FileSystem.h
llvm/lib/Support/Unix/Path.inc
llvm/lib/Support/Windows/Path.inc
llvm/unittests/Support/Path.cpp
Index: llvm/unittests/Support/Path.cpp
===================================================================
--- llvm/unittests/Support/Path.cpp
+++ llvm/unittests/Support/Path.cpp
@@ -1368,6 +1368,33 @@
ASSERT_NO_ERROR(fs::remove(TempPath));
}
+TEST_F(FileSystemTest, FileMappingSync) {
+ // Create a temp file.
+ int FD;
+ SmallString<64> TempPath;
+ ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
+ StringRef Content("hello there");
+ ASSERT_NO_ERROR(fs::resize_file_before_mapping_readwrite(FD, Content.size()));
+
+ {
+ // Map in the file and write some content.
+ std::error_code EC;
+ fs::mapped_file_region mfr(fs::convertFDToNativeFile(FD),
+ fs::mapped_file_region::readwrite,
+ Content.size(), 0, EC);
+ ASSERT_NO_ERROR(EC);
+ ASSERT_EQ(close(FD), 0);
+ std::copy(Content.begin(), Content.end(), mfr.data());
+
+ // Synchronize and check the contents before unmapping.
+ mfr.sync();
+ auto Buffer = MemoryBuffer::getFile(TempPath);
+ ASSERT_TRUE((bool)Buffer);
+ ASSERT_EQ(Content, Buffer->get()->getBuffer());
+ }
+ ASSERT_NO_ERROR(fs::remove(TempPath));
+}
+
TEST(Support, NormalizePath) {
// Input, Expected Win, Expected Posix
using TestTuple = std::tuple<const char *, const char *, const char *>;
Index: llvm/lib/Support/Windows/Path.inc
===================================================================
--- llvm/lib/Support/Windows/Path.inc
+++ llvm/lib/Support/Windows/Path.inc
@@ -935,6 +935,16 @@
return Size;
}
+std::error_code mapped_file_region::sync() const {
+ assert(Mapping && "Mapping failed but used anyway!");
+ assert(Mode == readwrite && "sync only valid for readwrite regions!");
+ if (!::FlushViewOfFile(Mapping, Size))
+ return mapWindowsError(GetLastError());
+ if (!::FlushFileBuffers(FileHandle))
+ return mapWindowsError(GetLastError());
+ return std::error_code();
+}
+
char *mapped_file_region::data() const {
assert(Mapping && "Mapping failed but used anyway!");
return reinterpret_cast<char*>(Mapping);
Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -860,6 +860,14 @@
return Size;
}
+std::error_code mapped_file_region::sync() const {
+ assert(Mapping && "Mapping failed but used anyway!");
+ assert(Mode == readwrite && "sync only valid for readwrite regions!");
+ if (::msync(Mapping, Size, MS_SYNC))
+ return std::error_code(errno, std::generic_category());
+ return std::error_code();
+}
+
char *mapped_file_region::data() const {
assert(Mapping && "Mapping failed but used anyway!");
return reinterpret_cast<char*>(Mapping);
Index: llvm/include/llvm/Support/FileSystem.h
===================================================================
--- llvm/include/llvm/Support/FileSystem.h
+++ llvm/include/llvm/Support/FileSystem.h
@@ -1251,6 +1251,9 @@
size_t size() const;
char *data() const;
+ /// Write changes to disk and synchronize. Equivalent to POSIX msync.
+ std::error_code sync() const;
+
/// Get a const view of the data. Modifying this memory has undefined
/// behavior.
const char *const_data() const;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95494.323425.patch
Type: text/x-patch
Size: 3329 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210212/b6481893/attachment.bin>
More information about the llvm-commits
mailing list