| 523 | } |
| 524 | |
| 525 | Status NewRandomAccessFile(const std::string& filename, |
| 526 | RandomAccessFile** result) override { |
| 527 | *result = nullptr; |
| 528 | int fd = ::open(filename.c_str(), O_RDONLY | kOpenBaseFlags); |
| 529 | if (fd < 0) { |
| 530 | return PosixError(filename, errno); |
| 531 | } |
| 532 | |
| 533 | if (!mmap_limiter_.Acquire()) { |
| 534 | *result = new PosixRandomAccessFile(filename, fd, &fd_limiter_); |
| 535 | return Status::OK(); |
| 536 | } |
| 537 | |
| 538 | uint64_t file_size; |
| 539 | Status status = GetFileSize(filename, &file_size); |
| 540 | if (status.ok()) { |
| 541 | void* mmap_base = |
| 542 | ::mmap(/*addr=*/nullptr, file_size, PROT_READ, MAP_SHARED, fd, 0); |
| 543 | if (mmap_base != MAP_FAILED) { |
| 544 | *result = new PosixMmapReadableFile(filename, |
| 545 | reinterpret_cast<char*>(mmap_base), |
| 546 | file_size, &mmap_limiter_); |
| 547 | } else { |
| 548 | status = PosixError(filename, errno); |
| 549 | } |
| 550 | } |
| 551 | ::close(fd); |
| 552 | if (!status.ok()) { |
| 553 | mmap_limiter_.Release(); |
| 554 | } |
| 555 | return status; |
| 556 | } |
| 557 | |
| 558 | Status NewWritableFile(const std::string& filename, |
| 559 | WritableFile** result) override { |