1
1
/* *
2
2
* Copyright 2019-2023, by XGBoost Contributors
3
3
*/
4
+ #if !defined(NOMINMAX) && defined(_WIN32)
5
+ #define NOMINMAX
6
+ #endif // !defined(NOMINMAX)
7
+
4
8
#if defined(__unix__) || defined(__APPLE__)
5
9
#include < fcntl.h> // for open, O_RDONLY
6
10
#include < sys/mman.h> // for mmap, mmap64, munmap
@@ -162,8 +166,18 @@ std::string FileExtension(std::string fname, bool lower) {
162
166
}
163
167
}
164
168
169
+ std::size_t GetPageSize () {
170
+ #if defined(_MSC_VER)
171
+ SYSTEM_INFO sys_info;
172
+ GetSystemInfo (&sys_info);
173
+ return sys_info.dwPageSize ;
174
+ #else
175
+ return getpagesize ();
176
+ #endif
177
+ }
178
+
165
179
std::size_t PadPageForMmap (std::size_t file_bytes, dmlc::Stream* fo) {
166
- decltype (file_bytes) page_size = getpagesize ();
180
+ decltype (file_bytes) page_size = GetPageSize ();
167
181
CHECK (page_size != 0 && page_size % 2 == 0 ) << " Failed to get page size on the current system." ;
168
182
CHECK_NE (file_bytes, 0 ) << " Empty page encountered." ;
169
183
auto n_pages = file_bytes / page_size + !!(file_bytes % page_size != 0 );
@@ -195,9 +209,9 @@ void* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
195
209
CHECK_NE (fd, INVALID_HANDLE_VALUE) << " Failed to open:" << path;
196
210
#else
197
211
auto fd = open (path.c_str (), O_RDONLY);
198
- #endif
199
212
CHECK_GE (fd, 0 ) << " Failed to open:" << path << " . " << strerror (errno);
200
- handle_ = std::make_unique<MMAPFile>(fd, std::move (path));
213
+ #endif
214
+ handle_.reset (new MMAPFile{fd, std::move (path)});
201
215
202
216
void * ptr{nullptr };
203
217
#if defined(__linux__) || defined(__GLIBC__)
@@ -212,7 +226,9 @@ void* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
212
226
DWORD access = read_only ? PAGE_READONLY : PAGE_READWRITE;
213
227
auto map_file = CreateFileMapping (handle_->fd , nullptr , access, 0 , file_size, nullptr );
214
228
access = read_only ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS;
215
- ptr = MapViewOfFile (map_file, access, 0 , offset, length);
229
+ std::uint32_t loff = static_cast <std::uint32_t >(offset);
230
+ std::uint32_t hoff = offset >> 32 ;
231
+ ptr = MapViewOfFile (map_file, access, hoff, loff, length);
216
232
CHECK_NE (ptr, nullptr ) << " Failed to map: " << handle_->path << " . " << GetLastError ();
217
233
#else
218
234
CHECK_LE (offset, std::numeric_limits<off_t >::max ())
0 commit comments