[llvm] r224138 - Move the resize file feature from mapped_file_region to the only user.
Rafael Espindola
rafael.espindola at gmail.com
Fri Dec 12 10:13:23 PST 2014
Author: rafael
Date: Fri Dec 12 12:13:23 2014
New Revision: 224138
URL: http://llvm.org/viewvc/llvm-project?rev=224138&view=rev
Log:
Move the resize file feature from mapped_file_region to the only user.
This removes a duplicated stat on every file that llvm-ar looks at.
Modified:
llvm/trunk/lib/Support/FileOutputBuffer.cpp
llvm/trunk/lib/Support/Unix/Path.inc
llvm/trunk/unittests/Support/Path.cpp
Modified: llvm/trunk/lib/Support/FileOutputBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileOutputBuffer.cpp?rev=224138&r1=224137&r2=224138&view=diff
==============================================================================
--- llvm/trunk/lib/Support/FileOutputBuffer.cpp (original)
+++ llvm/trunk/lib/Support/FileOutputBuffer.cpp Fri Dec 12 12:13:23 2014
@@ -77,6 +77,10 @@ FileOutputBuffer::create(StringRef FileP
if (EC)
return EC;
+ EC = sys::fs::resize_file(FD, Size);
+ if (EC)
+ return EC;
+
auto MappedFile = llvm::make_unique<mapped_file_region>(
FD, mapped_file_region::readwrite, Size, 0, EC);
int Ret = close(FD);
Modified: llvm/trunk/lib/Support/Unix/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Path.inc?rev=224138&r1=224137&r2=224138&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc (original)
+++ llvm/trunk/lib/Support/Unix/Path.inc Fri Dec 12 12:13:23 2014
@@ -414,19 +414,7 @@ std::error_code setLastModificationAndAc
std::error_code mapped_file_region::init(int FD, uint64_t Offset,
mapmode Mode) {
- // Figure out how large the file is.
- struct stat FileInfo;
- if (fstat(FD, &FileInfo) == -1)
- return std::error_code(errno, std::generic_category());
- uint64_t FileSize = FileInfo.st_size;
-
- if (Size == 0)
- Size = FileSize;
- else if (FileSize < Size) {
- // We need to grow the file.
- if (ftruncate(FD, Size) == -1)
- return std::error_code(errno, std::generic_category());
- }
+ assert(Size != 0);
int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE;
int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE);
Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=224138&r1=224137&r2=224138&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Fri Dec 12 12:13:23 2014
@@ -654,12 +654,15 @@ TEST_F(FileSystemTest, FileMapping) {
SmallString<64> TempPath;
ASSERT_NO_ERROR(
fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+ unsigned Size = 4096;
+ ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
+
// Map in temp file and add some content
std::error_code EC;
StringRef Val("hello there");
{
fs::mapped_file_region mfr(FileDescriptor,
- fs::mapped_file_region::readwrite, 4096, 0, EC);
+ fs::mapped_file_region::readwrite, Size, 0, EC);
ASSERT_NO_ERROR(EC);
std::copy(Val.begin(), Val.end(), mfr.data());
// Explicitly add a 0.
@@ -671,14 +674,14 @@ TEST_F(FileSystemTest, FileMapping) {
int FD;
EC = fs::openFileForRead(Twine(TempPath), FD);
ASSERT_NO_ERROR(EC);
- fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, 0, 0, EC);
+ fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
ASSERT_NO_ERROR(EC);
// Verify content
EXPECT_EQ(StringRef(mfr.const_data()), Val);
// Unmap temp file
- fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, 0, 0, EC);
+ fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
ASSERT_NO_ERROR(EC);
ASSERT_EQ(close(FD), 0);
}
More information about the llvm-commits
mailing list