@@ -187,8 +187,8 @@ std::size_t PadPageForMmap(std::size_t file_bytes, dmlc::Stream* fo) {
187
187
auto padded = n_pages * page_size;
188
188
auto padding = padded - file_bytes;
189
189
std::vector<std::uint8_t > padding_bytes (padding, 0 );
190
- fo->Write (padding_bytes.data (), padding_bytes.size ());
191
- return padded ;
190
+ // fo->Write(padding_bytes.data(), padding_bytes.size());
191
+ return file_bytes ;
192
192
}
193
193
194
194
struct PrivateMmapStream ::MMAPFile {
@@ -197,6 +197,8 @@ struct PrivateMmapStream::MMAPFile {
197
197
#else
198
198
std::int32_t fd {0 };
199
199
#endif
200
+ char * base_ptr{ nullptr };
201
+ std::size_t base_size{0 };
200
202
std::string path;
201
203
};
202
204
@@ -217,54 +219,58 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
217
219
auto fd = open (path.c_str (), O_RDONLY);
218
220
CHECK_GE (fd, 0 ) << " Failed to open:" << path << " . " << strerror (errno);
219
221
#endif
220
- handle_ = nullptr ;
221
- handle_.reset (new MMAPFile{fd, path});
222
222
223
- void * ptr{nullptr };
223
+ char * ptr{nullptr };
224
+ auto view_start = offset / GetPageSize () * GetPageSize ();
225
+ auto view_size = length + (offset - view_start);
226
+ std::cout << view_start << " size: " << view_size << std::endl;
224
227
#if defined(__linux__) || defined(__GLIBC__)
225
228
int prot{PROT_READ};
226
229
if (!read_only) {
227
230
prot |= PROT_WRITE;
228
231
}
229
- ptr = reinterpret_cast <char *>(mmap64 (nullptr , length , prot, MAP_PRIVATE, handle_-> fd , offset ));
230
- CHECK_NE (ptr, MAP_FAILED) << " Failed to map: " << handle_-> path << " . " << strerror (errno);
232
+ ptr = reinterpret_cast <char *>(mmap64 (nullptr , view_size , prot, MAP_PRIVATE, fd, view_start ));
233
+ CHECK_NE (ptr, MAP_FAILED) << " Failed to map: " << path << " . " << strerror (errno);
231
234
#elif defined(_MSC_VER)
232
- auto file_size = GetFileSize (handle_-> fd , nullptr );
235
+ auto file_size = GetFileSize (fd, nullptr );
233
236
DWORD access = read_only ? PAGE_READONLY : PAGE_READWRITE;
234
- auto map_file = CreateFileMapping (handle_-> fd , nullptr , access, 0 , file_size, nullptr );
237
+ auto map_file = CreateFileMapping (fd, nullptr , access, 0 , file_size, nullptr );
235
238
access = read_only ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS;
236
- std::uint32_t loff = static_cast <std::uint32_t >(offset );
237
- std::uint32_t hoff = offset >> 32 ;
238
- CHECK (map_file) << " Failed to map: " << handle_-> path << " . " << GetLastError (); ;
239
- ptr = MapViewOfFile (map_file, access, hoff, loff, length );
239
+ std::uint32_t loff = static_cast <std::uint32_t >(view_start );
240
+ std::uint32_t hoff = view_start >> 32 ;
241
+ CHECK (map_file) << " Failed to map: " << path << " . " << GetLastError ();
242
+ ptr = reinterpret_cast < char *>( MapViewOfFile (map_file, access, hoff, loff, view_size) );
240
243
if (ptr == nullptr ) {
241
244
system::ThrowAtError (" MapViewOfFile" );
242
245
}
243
- CHECK_NE (ptr, nullptr ) << " Failed to map: " << handle_-> path << " . " << GetLastError () ;
246
+ CHECK_NE (ptr, nullptr ) << " Failed to map: " << path << " . " << GetLastError ();
244
247
#else
245
248
CHECK_LE (offset, std::numeric_limits<off_t >::max ())
246
249
<< " File size has exceeded the limit on the current system." ;
247
250
int prot{PROT_READ};
248
251
if (!read_only) {
249
252
prot |= PROT_WRITE;
250
253
}
251
- ptr = reinterpret_cast <char *>(mmap (nullptr , length , prot, MAP_PRIVATE, fd_, offset ));
252
- CHECK_NE (ptr, MAP_FAILED) << " Failed to map: " << handle_-> path << " . " << strerror (errno);
254
+ ptr = reinterpret_cast <char *>(mmap (nullptr , view_size , prot, MAP_PRIVATE, fd, view_start ));
255
+ CHECK_NE (ptr, MAP_FAILED) << " Failed to map: " << path << " . " << strerror (errno);
253
256
#endif // defined(__linux__)
254
- return reinterpret_cast <char *>(ptr);
257
+
258
+ handle_.reset (new MMAPFile{ fd, ptr, view_size, std::move (path) });
259
+ ptr += (offset - view_start);
260
+ return ptr;
255
261
}
256
262
257
263
PrivateMmapStream::~PrivateMmapStream () {
258
264
CHECK (handle_);
259
265
#if defined(_MSC_VER)
260
266
if (p_buffer_) {
261
- CHECK (UnmapViewOfFile (p_buffer_ )) " Faled to munmap." << GetLastError ();
267
+ CHECK (UnmapViewOfFile (handle_-> base_ptr )) " Faled to munmap." << GetLastError ();
262
268
}
263
269
if (handle_->fd != INVALID_HANDLE_VALUE) {
264
270
CHECK (CloseHandle (handle_->fd ));
265
271
}
266
272
#else
267
- CHECK_NE (munmap (p_buffer_, buffer_size_ ), -1 )
273
+ CHECK_NE (munmap (handle_-> base_ptr , handle_-> base_size ), -1 )
268
274
<< " Faled to munmap." << handle_->path << " . " << strerror (errno);
269
275
CHECK_NE (close (handle_->fd ), -1 ) << " Faled to close: " << handle_->path << " . " << strerror (errno);
270
276
#endif
0 commit comments