MCPcopy Create free account
hub / github.com/cinit/TMoe / mapFileDescriptor

Method mapFileDescriptor

app/src/main/cpp/utils/FileMemMap.cpp:54–79  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52}
53
54int 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
81void FileMemMap::unmap() noexcept {
82 if (mAddress != nullptr && munmap(mAddress, mMapLength) == 0) {

Callers

nothing calls this directly

Calls 1

GetPageSizeFunction · 0.85

Tested by

no test coverage detected