llvm.org GIT mirror llvm / 7f4b22e
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224138 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
3 changed file(s) with 11 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
7676 if (EC)
7777 return EC;
7878
79 EC = sys::fs::resize_file(FD, Size);
80 if (EC)
81 return EC;
82
7983 auto MappedFile = llvm::make_unique(
8084 FD, mapped_file_region::readwrite, Size, 0, EC);
8185 int Ret = close(FD);
413413
414414 std::error_code mapped_file_region::init(int FD, uint64_t Offset,
415415 mapmode Mode) {
416 // Figure out how large the file is.
417 struct stat FileInfo;
418 if (fstat(FD, &FileInfo) == -1)
419 return std::error_code(errno, std::generic_category());
420 uint64_t FileSize = FileInfo.st_size;
421
422 if (Size == 0)
423 Size = FileSize;
424 else if (FileSize < Size) {
425 // We need to grow the file.
426 if (ftruncate(FD, Size) == -1)
427 return std::error_code(errno, std::generic_category());
428 }
416 assert(Size != 0);
429417
430418 int flags = (Mode == readwrite) ? MAP_SHARED : MAP_PRIVATE;
431419 int prot = (Mode == readonly) ? PROT_READ : (PROT_READ | PROT_WRITE);
653653 SmallString<64> TempPath;
654654 ASSERT_NO_ERROR(
655655 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
656 unsigned Size = 4096;
657 ASSERT_NO_ERROR(fs::resize_file(FileDescriptor, Size));
658
656659 // Map in temp file and add some content
657660 std::error_code EC;
658661 StringRef Val("hello there");
659662 {
660663 fs::mapped_file_region mfr(FileDescriptor,
661 fs::mapped_file_region::readwrite, 4096, 0, EC);
664 fs::mapped_file_region::readwrite, Size, 0, EC);
662665 ASSERT_NO_ERROR(EC);
663666 std::copy(Val.begin(), Val.end(), mfr.data());
664667 // Explicitly add a 0.
670673 int FD;
671674 EC = fs::openFileForRead(Twine(TempPath), FD);
672675 ASSERT_NO_ERROR(EC);
673 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, 0, 0, EC);
676 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, Size, 0, EC);
674677 ASSERT_NO_ERROR(EC);
675678
676679 // Verify content
677680 EXPECT_EQ(StringRef(mfr.const_data()), Val);
678681
679682 // Unmap temp file
680 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, 0, 0, EC);
683 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, Size, 0, EC);
681684 ASSERT_NO_ERROR(EC);
682685 ASSERT_EQ(close(FD), 0);
683686 }