10 bool disableMMap,
bool willNeed) {
12 std::errc errc= std::errc();
15 if (knownSize == (std::numeric_limits<std::size_t>::max)()) {
18 auto result= stat(path, &st);
21 auto result= _wstat64(path, &st);
24 errc= (errno != 0) ? std::errc(errno) : std::errc::io_error;
27 knownSize = size_t(st.st_size);
34 #if defined(_POSIX_MAPPED_FILES) && _POSIX_MAPPED_FILES > 0
36 int fd = ::open(path, O_RDONLY | O_CLOEXEC);
38 void* p = ::mmap(
nullptr, knownSize, PROT_READ, MAP_PRIVATE, fd, 0);
41 if (p != MAP_FAILED) {
48 ::madvise(mapAddr,
size, MADV_WILLNEED);
50 ::madvise(mapAddr,
size, MADV_SEQUENTIAL);
67 std::ifstream in(path, std::ios::binary);
69 errc= (errno != 0) ? std::errc(errno) : std::errc::io_error;
73 noMMapBuf.resize(knownSize /
sizeof(std::max_align_t) + 1);
74 in.read(
reinterpret_cast<char*
>(
noMMapBuf.data()),
static_cast<std::streamsize
>(knownSize));
75 if (!in && !in.eof()) {
76 errc= (errno != 0) ? std::errc(errno) : std::errc::io_error;
81 auto got =
static_cast<std::size_t
>(in.gcount());
82 if (got != knownSize)
noMMapBuf.resize(got/
sizeof(std::max_align_t) + 1);
90 HANDLE h = CreateFileW( path, GENERIC_READ, FILE_SHARE_READ,
nullptr,
91 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr );
92 if (h == INVALID_HANDLE_VALUE) {
93 errc = std::errc::io_error;
97 noMMapBuf.resize(knownSize/
sizeof(std::max_align_t) + 1);
99 std::size_t totalRead = 0;
100 while (totalRead < knownSize) {
101 DWORD chunk =
static_cast<DWORD
>(
102 std::min<std::size_t>(knownSize - totalRead,
static_cast<std::size_t
>(0x7fffffff)) );
105 BOOL ok = ReadFile( h,
noMMapBuf.data() + totalRead, chunk, &bytesRead,
nullptr );
108 errc = std::errc::io_error;
113 totalRead +=
static_cast<std::size_t
>(bytesRead);
116 if (totalRead != knownSize)
117 noMMapBuf.resize(totalRead/
sizeof(std::max_align_t) + 1);