1
0
mirror of synced 2025-04-20 11:47:43 +03:00
This commit is contained in:
yhirose 2024-09-09 22:22:56 -04:00
parent 3f00e1b321
commit 9f8db2c230

View File

@ -2468,13 +2468,14 @@ public:
private: private:
#if defined(_WIN32) #if defined(_WIN32)
HANDLE hFile_; HANDLE hFile_ = NULL;
HANDLE hMapping_; HANDLE hMapping_ = NULL;
bool is_open_empty_file_on_windows_ = false;
#else #else
int fd_; int fd_ = -1;
#endif #endif
size_t size_; size_t size_ = 0;
void *addr_; void *addr_ = nullptr;
}; };
} // namespace detail } // namespace detail
@ -2895,14 +2896,7 @@ inline void stream_line_reader::append(char c) {
} }
} }
inline mmap::mmap(const char *path) inline mmap::mmap(const char *path) {
#if defined(_WIN32)
: hFile_(NULL), hMapping_(NULL)
#else
: fd_(-1)
#endif
,
size_(0), addr_(nullptr) {
open(path); open(path);
} }
@ -2946,6 +2940,13 @@ inline bool mmap::open(const char *path) {
hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); hMapping_ = ::CreateFileMappingW(hFile_, NULL, PAGE_READONLY, 0, 0, NULL);
#endif #endif
// TODO: Special treatment for an empty file on Windows... (#1933)
if (hMapping_ == NULL && size_ == 0) {
close();
is_open_empty_file_on_windows_ = true;
return true;
}
if (hMapping_ == NULL) { if (hMapping_ == NULL) {
close(); close();
return false; return false;
@ -2956,6 +2957,11 @@ inline bool mmap::open(const char *path) {
#else #else
addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0);
#endif #endif
if (addr_ == nullptr) {
close();
return false;
}
#else #else
fd_ = ::open(path, O_RDONLY); fd_ = ::open(path, O_RDONLY);
if (fd_ == -1) { return false; } if (fd_ == -1) { return false; }
@ -2968,21 +2974,33 @@ inline bool mmap::open(const char *path) {
size_ = static_cast<size_t>(sb.st_size); size_ = static_cast<size_t>(sb.st_size);
addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0); addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0);
#endif
if (addr_ == nullptr) { if (addr_ == MAP_FAILED) {
close(); close();
return false; return false;
} }
#endif
return true; return true;
} }
inline bool mmap::is_open() const { return addr_ != nullptr; } inline bool mmap::is_open() const {
#if defined(_WIN32)
if (is_open_empty_file_on_windows_) {
return true;
}
#endif
return addr_ != nullptr;
}
inline size_t mmap::size() const { return size_; } inline size_t mmap::size() const { return size_; }
inline const char *mmap::data() const { inline const char *mmap::data() const {
#if defined(_WIN32)
if (is_open_empty_file_on_windows_) {
return "";
}
#endif
return static_cast<const char *>(addr_); return static_cast<const char *>(addr_);
} }
@ -3002,6 +3020,8 @@ inline void mmap::close() {
::CloseHandle(hFile_); ::CloseHandle(hFile_);
hFile_ = INVALID_HANDLE_VALUE; hFile_ = INVALID_HANDLE_VALUE;
} }
is_open_empty_file_on_windows_ = false;
#else #else
if (addr_ != nullptr) { if (addr_ != nullptr) {
munmap(addr_, size_); munmap(addr_, size_);