| 52 | } |
| 53 | |
| 54 | int FileMemMap::mapFileDescriptor(int fd, bool readOnly, size_t length, bool shared) { |
| 55 | if (fd < 0) { |
| 56 | return EBADFD; |
| 57 | } |
| 58 | if (length == 0) { |
| 59 | struct stat64 fileInfo = {}; |
| 60 | if (fstat64(fd, &fileInfo) < 0) { |
| 61 | return errno; |
| 62 | } |
| 63 | length = size_t(fileInfo.st_size); |
| 64 | } |
| 65 | if (length == 0) { |
| 66 | return EINVAL; |
| 67 | } |
| 68 | size_t kPageSize = utils::GetPageSize(); |
| 69 | size_t pageAlignedSize = (length + kPageSize - 1u) & ~(kPageSize - 1u); |
| 70 | void* addr = mmap(nullptr, pageAlignedSize, readOnly ? PROT_READ : (PROT_READ | PROT_WRITE), |
| 71 | shared ? MAP_SHARED : MAP_PRIVATE, fd, 0); |
| 72 | if (addr == MAP_FAILED) { |
| 73 | return errno; |
| 74 | } |
| 75 | mAddress = addr; |
| 76 | mLength = length; |
| 77 | mMapLength = pageAlignedSize; |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | void FileMemMap::unmap() noexcept { |
| 82 | if (mAddress != nullptr && munmap(mAddress, mMapLength) == 0) { |
nothing calls this directly
no test coverage detected