29 static size_t size = sysconf(_SC_PAGESIZE);
37 mFd =
::open(mPath.c_str(), O_RDONLY | O_CLOEXEC);
39 throw IoError(mPath, std::strerror(errno));
42 if (fstat(mFd, &
st) != 0) {
45 throw IoError(mPath, std::strerror(err));
48 if (options.
localIO == Options::LocalIO::MMap &&
sizeof(
void*) == 8 && mSize > 0) {
49 void* p = mmap(
nullptr, mSize, PROT_READ, MAP_PRIVATE, mFd, 0);
50 if (p != MAP_FAILED) {
51 mMap = (uint8_t
const*)p;
59 munmap((
void*)mMap, mSize);
64void LocalByteSource::checkRange(uint64_t off,
size_t len)
const
66 if (off > mSize ||
len > mSize - off) {
77 mReadCalls.fetch_add(1, std::memory_order_relaxed);
78 mBytesRead.fetch_add(
len, std::memory_order_relaxed);
80 std::memcpy(
dst, mMap + off,
len);
83 auto* out = (uint8_t*)
dst;
85 auto n = ::pread(mFd, out,
len, off);
86 if (
n < 0 && errno == EINTR) {
90 throw IoError(mPath,
n == 0 ?
"unexpected end of file" : std::strerror(errno));
100 for (
auto&
r : requests) {
110 checkRange(off,
len);
112 mReadCalls.fetch_add(1, std::memory_order_relaxed);
113 mBytesRead.fetch_add(
len, std::memory_order_relaxed);
114 return {mMap + off,
len};
117void LocalByteSource::advise(uint64_t off,
size_t len,
int advice)
119 if (!mMap || off >= mSize) {
122 len = std::min<uint64_t>(
len, mSize - off);
123 auto start = off & ~(uint64_t)(pageSize() - 1);
static std::unique_ptr< ByteSource > open(std::string url, const Options &)
void dontNeed(uint64_t off, size_t len) override
LocalByteSource(std::string path, Options const &options)
void willNeed(uint64_t off, size_t len) override
Hints that a range will be needed soon, or is no longer needed.
void readvInto(std::span< ReadRequest > requests) override
~LocalByteSource() override
std::span< const uint8_t > tryMap(uint64_t off, size_t len) override
A view of the bytes without copying them, if the backend can provide it.
void readInto(uint64_t off, size_t len, void *dst) override
Throws IoError, or CorruptData when the range is past the end of the file.
GLsizei const GLchar *const * path
GLenum GLenum GLsizei len
std::string to_string(gsl::span< T, Size > span)
Failure of the underlying transport.