| 72 | } |
| 73 | |
| 74 | static inline void* mmap(void* addr, size_t len, int prot, int flags, int fildes, |
| 75 | off_t off) { |
| 76 | HANDLE fm, h; |
| 77 | |
| 78 | void* map = MAP_FAILED; |
| 79 | const uint64_t off64 = static_cast<uint64_t>(off); |
| 80 | const uint64_t maxSize = off64 + len; |
| 81 | |
| 82 | const DWORD dwFileOffsetLow = static_cast<DWORD>(off64 & 0xFFFFFFFFUL); |
| 83 | const DWORD dwFileOffsetHigh = static_cast<DWORD>((off64 >> 32) & 0xFFFFFFFFUL); |
| 84 | const DWORD dwMaxSizeLow = static_cast<DWORD>(maxSize & 0xFFFFFFFFUL); |
| 85 | const DWORD dwMaxSizeHigh = static_cast<DWORD>((maxSize >> 32) & 0xFFFFFFFFUL); |
| 86 | |
| 87 | const DWORD protect = __map_mmap_prot_page(prot); |
| 88 | const DWORD desiredAccess = __map_mmap_prot_file(prot); |
| 89 | |
| 90 | errno = 0; |
| 91 | |
| 92 | if (len == 0 |
| 93 | /* Unsupported flag combinations */ |
| 94 | || (flags & MAP_FIXED) != 0 |
| 95 | /* Unsupported protection combinations */ |
| 96 | || prot == PROT_EXEC) { |
| 97 | errno = EINVAL; |
| 98 | return MAP_FAILED; |
| 99 | } |
| 100 | |
| 101 | h = ((flags & MAP_ANONYMOUS) == 0) ? (HANDLE)_get_osfhandle(fildes) |
| 102 | : INVALID_HANDLE_VALUE; |
| 103 | |
| 104 | if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE) { |
| 105 | errno = EBADF; |
| 106 | return MAP_FAILED; |
| 107 | } |
| 108 | |
| 109 | fm = CreateFileMapping(h, NULL, protect, dwMaxSizeHigh, dwMaxSizeLow, NULL); |
| 110 | |
| 111 | if (fm == NULL) { |
| 112 | errno = __map_mman_error(GetLastError(), EPERM); |
| 113 | return MAP_FAILED; |
| 114 | } |
| 115 | |
| 116 | map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len); |
| 117 | |
| 118 | CloseHandle(fm); |
| 119 | |
| 120 | if (map == NULL) { |
| 121 | errno = __map_mman_error(GetLastError(), EPERM); |
| 122 | return MAP_FAILED; |
| 123 | } |
| 124 | |
| 125 | return map; |
| 126 | } |
| 127 | |
| 128 | static inline int munmap(void* addr, size_t len) { |
| 129 | if (UnmapViewOfFile(addr)) return 0; |
no test coverage detected