5
5
#define NOMINMAX
6
6
#endif // !defined(NOMINMAX)
7
7
8
- #if defined(__unix__) || defined(__APPLE__)
8
+ #if defined(__unix__) || defined(__APPLE__) || defined(__MINGW32__)
9
9
#include < fcntl.h> // for open, O_RDONLY
10
10
#include < sys/mman.h> // for mmap, mmap64, munmap
11
- #include < sys/stat.h>
12
- #include < unistd.h> // for close, getpagesize
11
+ #include < unistd.h> // for close, getpagesize
13
12
#elif defined(_MSC_VER)
14
13
#define WIN32_LEAN_AND_MEAN
15
14
#include < windows.h>
16
- #endif // defined(__unix__)
15
+ #endif // defined(__unix__)
17
16
18
- #include < algorithm>
19
- #include < cerrno> // for errno
20
- #include < cstdio>
21
- #include < fstream>
22
- #include < limits> // for numeric_limits
23
- #include < memory>
24
- #include < string>
25
- #include < utility>
26
- #include < vector> // for vector
17
+ #include < algorithm> // for copy, transform
18
+ #include < cctype> // for tolower
19
+ #include < cerrno> // for errno
20
+ #include < cstddef> // for size_t
21
+ #include < cstdint> // for int32_t, uint32_t
22
+ #include < cstring> // for memcpy
23
+ #include < fstream> // for ifstream
24
+ #include < iterator> // for distance
25
+ #include < limits> // for numeric_limits
26
+ #include < memory> // for unique_ptr
27
+ #include < string> // for string
28
+ #include < system_error> // for error_code, system_category
29
+ #include < utility> // for move
30
+ #include < vector> // for vector
27
31
28
32
#include " io.h"
33
+ #include " xgboost/collective/socket.h" // for LastError
29
34
#include " xgboost/logging.h"
30
- #include " xgboost/collective/socket.h"
31
35
32
36
namespace xgboost {
33
37
namespace common {
@@ -191,6 +195,14 @@ struct PrivateMmapStream::MMAPFile {
191
195
std::string path;
192
196
};
193
197
198
+ namespace {
199
+ auto SystemErrorMsg () {
200
+ std::int32_t errsv = system::LastError ();
201
+ auto err = std::error_code{errsv, std::system_category ()};
202
+ return err;
203
+ }
204
+ } // anonymous namespace
205
+
194
206
PrivateMmapStream::PrivateMmapStream (std::string path, bool read_only, std::size_t offset,
195
207
std::size_t length)
196
208
: MemoryFixSizeBuffer{} {
@@ -206,7 +218,7 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
206
218
CHECK_NE (fd, INVALID_HANDLE_VALUE) << " Failed to open:" << path;
207
219
#else
208
220
auto fd = open (path.c_str (), O_RDONLY);
209
- CHECK_GE (fd, 0 ) << " Failed to open:" << path << " . " << strerror (errno );
221
+ CHECK_GE (fd, 0 ) << " Failed to open:" << path << " . " << SystemErrorMsg ( );
210
222
#endif
211
223
212
224
char * ptr{nullptr };
@@ -218,7 +230,7 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
218
230
prot |= PROT_WRITE;
219
231
}
220
232
ptr = reinterpret_cast <char *>(mmap64 (nullptr , view_size, prot, MAP_PRIVATE, fd, view_start));
221
- CHECK_NE (ptr, MAP_FAILED) << " Failed to map: " << path << " . " << strerror (errno );
233
+ CHECK_NE (ptr, MAP_FAILED) << " Failed to map: " << path << " . " << SystemErrorMsg ( );
222
234
#elif defined(_MSC_VER)
223
235
auto file_size = GetFileSize (fd, nullptr );
224
236
DWORD access = read_only ? PAGE_READONLY : PAGE_READWRITE;
@@ -228,10 +240,7 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
228
240
std::uint32_t hoff = view_start >> 32 ;
229
241
CHECK (map_file) << " Failed to map: " << path << " . " << GetLastError ();
230
242
ptr = reinterpret_cast <char *>(MapViewOfFile (map_file, access, hoff, loff, view_size));
231
- if (ptr == nullptr ) {
232
- system::ThrowAtError (" MapViewOfFile" );
233
- }
234
- CHECK_NE (ptr, nullptr ) << " Failed to map: " << path << " . " << GetLastError ();
243
+ CHECK_NE (ptr, nullptr ) << " Failed to map: " << path << " . " << SystemErrorMsg ();
235
244
#else
236
245
CHECK_LE (offset, std::numeric_limits<off_t >::max ())
237
246
<< " File size has exceeded the limit on the current system." ;
@@ -240,7 +249,7 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
240
249
prot |= PROT_WRITE;
241
250
}
242
251
ptr = reinterpret_cast <char *>(mmap (nullptr , view_size, prot, MAP_PRIVATE, fd, view_start));
243
- CHECK_NE (ptr, MAP_FAILED) << " Failed to map: " << path << " . " << strerror (errno );
252
+ CHECK_NE (ptr, MAP_FAILED) << " Failed to map: " << path << " . " << SystemErrorMsg ( );
244
253
#endif // defined(__linux__)
245
254
246
255
handle_.reset (new MMAPFile{fd, ptr, view_size, std::move (path)});
0 commit comments