llvm.org GIT mirror llvm / 0be06cf
Remove a convoluted way of calling close by moving the call to the only caller. As a bonus we can actually check the return value. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224046 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
6 changed file(s) with 30 addition(s) and 108 deletion(s). Raw diff Collapse all Expand all
636636
637637 private:
638638 /// Platform-specific mapping state.
639 mapmode Mode;
640639 uint64_t Size;
641640 void *Mapping;
642641 #ifdef LLVM_ON_WIN32
645644 void *FileMappingHandle;
646645 #endif
647646
648 std::error_code init(int FD, bool CloseFD, uint64_t Offset);
647 std::error_code init(int FD, uint64_t Offset, mapmode Mode);
649648
650649 public:
651 typedef char char_type;
652
653 mapped_file_region(mapped_file_region&&);
654 mapped_file_region &operator =(mapped_file_region&&);
655
656650 /// \param fd An open file descriptor to map. mapped_file_region takes
657651 /// ownership if closefd is true. It must have been opended in the correct
658652 /// mode.
659 mapped_file_region(int fd, bool closefd, mapmode mode, uint64_t length,
660 uint64_t offset, std::error_code &ec);
653 mapped_file_region(int fd, mapmode mode, uint64_t length, uint64_t offset,
654 std::error_code &ec);
661655
662656 ~mapped_file_region();
663657
1616 #include "llvm/ADT/STLExtras.h"
1717 #include "llvm/Support/raw_ostream.h"
1818 #include
19
20 #if !defined(_MSC_VER) && !defined(__MINGW32__)
21 #include
22 #else
23 #include
24 #endif
1925
2026 using llvm::sys::fs::mapped_file_region;
2127
7177 return EC;
7278
7379 auto MappedFile = llvm::make_unique(
74 FD, true, mapped_file_region::readwrite, Size, 0, EC);
80 FD, mapped_file_region::readwrite, Size, 0, EC);
81 int Ret = close(FD);
7582 if (EC)
7683 return EC;
84 if (Ret)
85 return std::error_code(errno, std::generic_category());
7786
7887 Result.reset(
7988 new FileOutputBuffer(std::move(MappedFile), FilePath, TempFilePath));
203203 public:
204204 MemoryBufferMMapFile(bool RequiresNullTerminator, int FD, uint64_t Len,
205205 uint64_t Offset, std::error_code EC)
206 : MFR(FD, false, sys::fs::mapped_file_region::readonly,
206 : MFR(FD, sys::fs::mapped_file_region::readonly,
207207 getLegalMapSize(Len, Offset), getLegalMapOffset(Offset), EC) {
208208 if (!EC) {
209209 const char *Start = getStart(Len, Offset);
6161
6262 using namespace llvm;
6363
64 namespace {
65 /// This class automatically closes the given file descriptor when it goes out
66 /// of scope. You can take back explicit ownership of the file descriptor by
67 /// calling take(). The destructor does not verify that close was successful.
68 /// Therefore, never allow this class to call close on a file descriptor that
69 /// has been read from or written to.
70 struct AutoFD {
71 int FileDescriptor;
72
73 AutoFD(int fd) : FileDescriptor(fd) {}
74 ~AutoFD() {
75 if (FileDescriptor >= 0)
76 ::close(FileDescriptor);
77 }
78
79 int take() {
80 int ret = FileDescriptor;
81 FileDescriptor = -1;
82 return ret;
83 }
84
85 operator int() const {return FileDescriptor;}
86 };
87 }
88
8964 namespace llvm {
9065 namespace sys {
9166 namespace fs {
439414 #endif
440415 }
441416
442 std::error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
443 AutoFD ScopedFD(FD);
444 if (!CloseFD)
445 ScopedFD.take();
446
417 std::error_code mapped_file_region::init(int FD, uint64_t Offset,
418 mapmode Mode) {
447419 // Figure out how large the file is.
448420 struct stat FileInfo;
449421 if (fstat(FD, &FileInfo) == -1)
469441 return std::error_code();
470442 }
471443
472 mapped_file_region::mapped_file_region(int fd,
473 bool closefd,
474 mapmode mode,
475 uint64_t length,
476 uint64_t offset,
477 std::error_code &ec)
478 : Mode(mode)
479 , Size(length)
480 , Mapping() {
444 mapped_file_region::mapped_file_region(int fd, mapmode mode, uint64_t length,
445 uint64_t offset, std::error_code &ec)
446 : Size(length), Mapping() {
481447 // Make sure that the requested size fits within SIZE_T.
482448 if (length > std::numeric_limits::max()) {
483449 ec = make_error_code(errc::invalid_argument);
484450 return;
485451 }
486452
487 ec = init(fd, closefd, offset);
453 ec = init(fd, offset, mode);
488454 if (ec)
489455 Mapping = nullptr;
490456 }
494460 ::munmap(Mapping, Size);
495461 }
496462
497 mapped_file_region::mapped_file_region(mapped_file_region &&other)
498 : Mode(other.Mode), Size(other.Size), Mapping(other.Mapping) {
499 other.Mapping = nullptr;
500 }
501
502463 uint64_t mapped_file_region::size() const {
503464 assert(Mapping && "Mapping failed but used anyway!");
504465 return Size;
506467
507468 char *mapped_file_region::data() const {
508469 assert(Mapping && "Mapping failed but used anyway!");
509 assert(Mode != readonly && "Cannot get non-const data for readonly mapping!");
510470 return reinterpret_cast(Mapping);
511471 }
512472
466466 return std::error_code();
467467 }
468468
469 std::error_code mapped_file_region::init(int FD, bool CloseFD, uint64_t Offset) {
469 std::error_code mapped_file_region::init(int FD, uint64_t Offset,
470 mapmode Mode) {
470471 FileDescriptor = FD;
471472 // Make sure that the requested size fits within SIZE_T.
472473 if (Size > std::numeric_limits::max()) {
473474 if (FileDescriptor) {
474 if (CloseFD)
475 _close(FileDescriptor);
476475 } else
477476 ::CloseHandle(FileHandle);
478477 return make_error_code(errc::invalid_argument);
493492 if (FileMappingHandle == NULL) {
494493 std::error_code ec = windows_error(GetLastError());
495494 if (FileDescriptor) {
496 if (CloseFD)
497 _close(FileDescriptor);
498495 } else
499496 ::CloseHandle(FileHandle);
500497 return ec;
515512 std::error_code ec = windows_error(GetLastError());
516513 ::CloseHandle(FileMappingHandle);
517514 if (FileDescriptor) {
518 if (CloseFD)
519 _close(FileDescriptor);
520515 } else
521516 ::CloseHandle(FileHandle);
522517 return ec;
530525 ::UnmapViewOfFile(Mapping);
531526 ::CloseHandle(FileMappingHandle);
532527 if (FileDescriptor) {
533 if (CloseFD)
534 _close(FileDescriptor);
535528 } else
536529 ::CloseHandle(FileHandle);
537530 return ec;
543536 // alive.
544537 ::CloseHandle(FileMappingHandle);
545538 if (FileDescriptor) {
546 if (CloseFD)
547 _close(FileDescriptor); // Also closes FileHandle.
548539 } else
549540 ::CloseHandle(FileHandle);
550541 return std::error_code();
551542 }
552543
553 mapped_file_region::mapped_file_region(int fd,
554 bool closefd,
555 mapmode mode,
556 uint64_t length,
557 uint64_t offset,
558 std::error_code &ec)
559 : Mode(mode)
560 , Size(length)
561 , Mapping()
562 , FileDescriptor(fd)
563 , FileHandle(INVALID_HANDLE_VALUE)
564 , FileMappingHandle() {
544 mapped_file_region::mapped_file_region(int fd, mapmode mode, uint64_t length,
545 uint64_t offset, std::error_code &ec)
546 : Size(length), Mapping(), FileDescriptor(fd),
547 FileHandle(INVALID_HANDLE_VALUE), FileMappingHandle() {
565548 FileHandle = reinterpret_cast(_get_osfhandle(fd));
566549 if (FileHandle == INVALID_HANDLE_VALUE) {
567 if (closefd)
568 _close(FileDescriptor);
569550 FileDescriptor = 0;
570551 ec = make_error_code(errc::bad_file_descriptor);
571552 return;
572553 }
573554
574 ec = init(FileDescriptor, closefd, offset);
555 ec = init(FileDescriptor, offset, mode);
575556 if (ec) {
576557 Mapping = FileMappingHandle = 0;
577558 FileHandle = INVALID_HANDLE_VALUE;
584565 ::UnmapViewOfFile(Mapping);
585566 }
586567
587 mapped_file_region::mapped_file_region(mapped_file_region &&other)
588 : Mode(other.Mode)
589 , Size(other.Size)
590 , Mapping(other.Mapping)
591 , FileDescriptor(other.FileDescriptor)
592 , FileHandle(other.FileHandle)
593 , FileMappingHandle(other.FileMappingHandle) {
594 other.Mapping = other.FileMappingHandle = 0;
595 other.FileHandle = INVALID_HANDLE_VALUE;
596 other.FileDescriptor = 0;
597 }
598
599568 uint64_t mapped_file_region::size() const {
600569 assert(Mapping && "Mapping failed but used anyway!");
601570 return Size;
602571 }
603572
604573 char *mapped_file_region::data() const {
605 assert(Mode != readonly && "Cannot get non-const data for readonly mapping!");
606574 assert(Mapping && "Mapping failed but used anyway!");
607575 return reinterpret_cast(Mapping);
608576 }
648648 StringRef Val("hello there");
649649 {
650650 fs::mapped_file_region mfr(FileDescriptor,
651 true,
652 fs::mapped_file_region::readwrite,
653 4096,
654 0,
655 EC);
651 fs::mapped_file_region::readwrite, 4096, 0, EC);
656652 ASSERT_NO_ERROR(EC);
657653 std::copy(Val.begin(), Val.end(), mfr.data());
658654 // Explicitly add a 0.
664660 int FD;
665661 EC = fs::openFileForRead(Twine(TempPath), FD);
666662 ASSERT_NO_ERROR(EC);
667 fs::mapped_file_region mfr(FD, false, fs::mapped_file_region::readonly, 0, 0,
668 EC);
663 fs::mapped_file_region mfr(FD, fs::mapped_file_region::readonly, 0, 0, EC);
669664 ASSERT_NO_ERROR(EC);
670665
671666 // Verify content
672667 EXPECT_EQ(StringRef(mfr.const_data()), Val);
673668
674669 // Unmap temp file
675 fs::mapped_file_region m(FD, false, fs::mapped_file_region::readonly, 0, 0,
676 EC);
670 fs::mapped_file_region m(FD, fs::mapped_file_region::readonly, 0, 0, EC);
677671 ASSERT_NO_ERROR(EC);
678672 ASSERT_EQ(close(FD), 0);
679 const char *Data = m.const_data();
680 fs::mapped_file_region mfrrv(std::move(m));
681 EXPECT_EQ(mfrrv.const_data(), Data);
682673 }
683674
684675 TEST(Support, NormalizePath) {