Skip to content

Commit c69f13b

Browse files
committed
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. llvm-svn: 224138
1 parent 555a7a6 commit c69f13b

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

llvm/lib/Support/FileOutputBuffer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ FileOutputBuffer::create(StringRef FilePath, size_t Size,
7777
if (EC)
7878
return EC;
7979

80+
EC = sys::fs::resize_file(FD, Size);
81+
if (EC)
82+
return EC;
83+
8084
auto MappedFile = llvm::make_unique<mapped_file_region>(
8185
FD, mapped_file_region::readwrite, Size, 0, EC);
8286
int Ret = close(FD);

llvm/lib/Support/Unix/Path.inc

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,19 +414,7 @@ std::error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
414414

415415
std::error_code mapped_file_region::init(int FD, uint64_t Offset,
416416
mapmode Mode) {
417-
// Figure out how large the file is.
418-
struct stat FileInfo;
419-
if (fstat(FD, &FileInfo) == -1)
420-
return std::error_code(errno, std::generic_category());
421-
uint64_t FileSize = FileInfo.st_size;
422-
423-
if (Size == 0)
424-
Size = FileSize;
425-
else if (FileSize < Size) {
426-
// We need to grow the file.
427-
if (ftruncate(FD, Size) == -1)
428-
return std::error_code(errno, std::generic_category());
429-
}
417+
assert(Size != 0);
430418

431419
int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE;
432420
int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE);

llvm/unittests/Support/Path.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,15 @@ TEST_F(FileSystemTest, FileMapping) {
654654
SmallString<64> TempPath;
655655
ASSERT_NO_ERROR(
656656
fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
657+
unsigned Size = 4096;
658+
ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
659+
657660
// Map in temp file and add some content
658661
std::error_code EC;
659662
StringRef Val("hello there");
660663
{
661664
fs::mapped_file_region mfr(FileDescriptor,
662-
fs::mapped_file_region::readwrite, 4096, 0, EC);
665+
fs::mapped_file_region::readwrite, Size, 0, EC);
663666
ASSERT_NO_ERROR(EC);
664667
std::copy(Val.begin(), Val.end(), mfr.data());
665668
// Explicitly add a 0.
@@ -671,14 +674,14 @@ TEST_F(FileSystemTest, FileMapping) {
671674
int FD;
672675
EC = fs::openFileForRead(Twine(TempPath), FD);
673676
ASSERT_NO_ERROR(EC);
674-
fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, 0, 0, EC);
677+
fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
675678
ASSERT_NO_ERROR(EC);
676679

677680
// Verify content
678681
EXPECT_EQ(StringRef(mfr.const_data()), Val);
679682

680683
// Unmap temp file
681-
fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, 0, 0, EC);
684+
fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
682685
ASSERT_NO_ERROR(EC);
683686
ASSERT_EQ(close(FD), 0);
684687
}

0 commit comments

Comments
 (0)