File indexing completed on 2025-08-28 08:26:59
0001
0002
0003
0004
0005
0006
0007
0008 #pragma once
0009
0010 #include "arrow/util/windows_compatibility.h"
0011
0012 #include <errno.h>
0013 #include <io.h>
0014 #include <sys/types.h>
0015
0016 #include <cstdint>
0017
0018 #define PROT_NONE 0
0019 #define PROT_READ 1
0020 #define PROT_WRITE 2
0021 #define PROT_EXEC 4
0022
0023 #define MAP_FILE 0
0024 #define MAP_SHARED 1
0025 #define MAP_PRIVATE 2
0026 #define MAP_TYPE 0xf
0027 #define MAP_FIXED 0x10
0028 #define MAP_ANONYMOUS 0x20
0029 #define MAP_ANON MAP_ANONYMOUS
0030
0031 #define MAP_FAILED ((void*)-1)
0032
0033
0034 #define MS_ASYNC 1
0035 #define MS_SYNC 2
0036 #define MS_INVALIDATE 4
0037
0038 #ifndef FILE_MAP_EXECUTE
0039 # define FILE_MAP_EXECUTE 0x0020
0040 #endif
0041
0042 static inline int __map_mman_error(const DWORD err, const int deferr) {
0043 if (err == 0) return 0;
0044
0045 return err;
0046 }
0047
0048 static inline DWORD __map_mmap_prot_page(const int prot) {
0049 DWORD protect = 0;
0050
0051 if (prot == PROT_NONE) return protect;
0052
0053 if ((prot & PROT_EXEC) != 0) {
0054 protect = ((prot & PROT_WRITE) != 0) ? PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ;
0055 } else {
0056 protect = ((prot & PROT_WRITE) != 0) ? PAGE_READWRITE : PAGE_READONLY;
0057 }
0058
0059 return protect;
0060 }
0061
0062 static inline DWORD __map_mmap_prot_file(const int prot) {
0063 DWORD desiredAccess = 0;
0064
0065 if (prot == PROT_NONE) return desiredAccess;
0066
0067 if ((prot & PROT_READ) != 0) desiredAccess |= FILE_MAP_READ;
0068 if ((prot & PROT_WRITE) != 0) desiredAccess |= FILE_MAP_WRITE;
0069 if ((prot & PROT_EXEC) != 0) desiredAccess |= FILE_MAP_EXECUTE;
0070
0071 return desiredAccess;
0072 }
0073
0074 static inline void* mmap(void* addr, size_t len, int prot, int flags, int fildes,
0075 off_t off) {
0076 HANDLE fm, h;
0077
0078 void* map = MAP_FAILED;
0079 const uint64_t off64 = static_cast<uint64_t>(off);
0080 const uint64_t maxSize = off64 + len;
0081
0082 const DWORD dwFileOffsetLow = static_cast<DWORD>(off64 & 0xFFFFFFFFUL);
0083 const DWORD dwFileOffsetHigh = static_cast<DWORD>((off64 >> 32) & 0xFFFFFFFFUL);
0084 const DWORD dwMaxSizeLow = static_cast<DWORD>(maxSize & 0xFFFFFFFFUL);
0085 const DWORD dwMaxSizeHigh = static_cast<DWORD>((maxSize >> 32) & 0xFFFFFFFFUL);
0086
0087 const DWORD protect = __map_mmap_prot_page(prot);
0088 const DWORD desiredAccess = __map_mmap_prot_file(prot);
0089
0090 errno = 0;
0091
0092 if (len == 0
0093
0094 || (flags & MAP_FIXED) != 0
0095
0096 || prot == PROT_EXEC) {
0097 errno = EINVAL;
0098 return MAP_FAILED;
0099 }
0100
0101 h = ((flags & MAP_ANONYMOUS) == 0) ? (HANDLE)_get_osfhandle(fildes)
0102 : INVALID_HANDLE_VALUE;
0103
0104 if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE) {
0105 errno = EBADF;
0106 return MAP_FAILED;
0107 }
0108
0109 fm = CreateFileMapping(h, NULL, protect, dwMaxSizeHigh, dwMaxSizeLow, NULL);
0110
0111 if (fm == NULL) {
0112 errno = __map_mman_error(GetLastError(), EPERM);
0113 return MAP_FAILED;
0114 }
0115
0116 map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len);
0117
0118 CloseHandle(fm);
0119
0120 if (map == NULL) {
0121 errno = __map_mman_error(GetLastError(), EPERM);
0122 return MAP_FAILED;
0123 }
0124
0125 return map;
0126 }
0127
0128 static inline int munmap(void* addr, size_t len) {
0129 if (UnmapViewOfFile(addr)) return 0;
0130
0131 errno = __map_mman_error(GetLastError(), EPERM);
0132
0133 return -1;
0134 }
0135
0136 static inline int mprotect(void* addr, size_t len, int prot) {
0137 DWORD newProtect = __map_mmap_prot_page(prot);
0138 DWORD oldProtect = 0;
0139
0140 if (VirtualProtect(addr, len, newProtect, &oldProtect)) return 0;
0141
0142 errno = __map_mman_error(GetLastError(), EPERM);
0143
0144 return -1;
0145 }
0146
0147 static inline int msync(void* addr, size_t len, int flags) {
0148 if (FlushViewOfFile(addr, len)) return 0;
0149
0150 errno = __map_mman_error(GetLastError(), EPERM);
0151
0152 return -1;
0153 }
0154
0155 static inline int mlock(const void* addr, size_t len) {
0156 if (VirtualLock((LPVOID)addr, len)) return 0;
0157
0158 errno = __map_mman_error(GetLastError(), EPERM);
0159
0160 return -1;
0161 }
0162
0163 static inline int munlock(const void* addr, size_t len) {
0164 if (VirtualUnlock((LPVOID)addr, len)) return 0;
0165
0166 errno = __map_mman_error(GetLastError(), EPERM);
0167
0168 return -1;
0169 }