25 if (knownSize == std::numeric_limits<std::size_t>::max()) {
27 std::ifstream in(path, std::ios::binary | std::ios::ate);
28 if (!in)
throw std::runtime_error(
"open failed for size detection");
29 knownSize =
static_cast<std::size_t
>(in.tellg());
32 if (stat(path, &st) != 0)
throw std::runtime_error(
"stat failed for size detection");
33 knownSize =
static_cast<std::size_t
>(st.st_size);
39 return Data(
static_cast<std::byte*
>(mapAddr=
nullptr),
size= 0);
41 #if defined(_POSIX_MAPPED_FILES) && _POSIX_MAPPED_FILES > 0
43 int fd = ::open(path, O_RDONLY | O_CLOEXEC);
45 void* p = ::mmap(
nullptr, knownSize, PROT_READ, MAP_PRIVATE, fd, 0);
46 int saved_errno = errno;
49 if (p == MAP_FAILED) {
60 ::madvise(mapAddr,
size, MADV_WILLNEED);
63 return Data(
static_cast<std::byte*
>(mapAddr),
size);
73 std::ifstream in(path, std::ios::binary);
74 if (!in)
throw std::runtime_error(
"open failed");
76 buffer.resize(knownSize);
77 in.read(
reinterpret_cast<char*
>(buffer.data()),
static_cast<std::streamsize
>(knownSize));
78 if (!in && !in.eof())
throw std::runtime_error(
"read failed");
81 auto got =
static_cast<std::size_t
>(in.gcount());
82 if (got != knownSize) buffer.resize(got);
86 backend_ = Backend::ReadAll;
90 int fd = ::open(path, O_RDONLY | O_CLOEXEC);
91 if (fd < 0)
throw std::runtime_error(
"open failed");
100 throw std::runtime_error(
"read failed");
103 off +=
static_cast<std::size_t
>(r);