1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (memmap_win32.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #ifndef FILE_MAP_EXECUTE
30 #define FILE_MAP_EXECUTE 0x0020
31 #endif /* FILE_MAP_EXECUTE */
33 static int __map_mman_error(const DWORD err, const int deferr)
41 static DWORD __map_mmap_prot_page(const int prot)
45 if (prot == PROT_NONE)
48 if ((prot & PROT_EXEC) != 0)
49 protect = ((prot & PROT_WRITE) != 0) ?
50 PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ;
52 protect = ((prot & PROT_WRITE) != 0) ?
53 PAGE_READWRITE : PAGE_READONLY;
58 static DWORD __map_mmap_prot_file(const int prot)
60 DWORD desiredAccess = 0;
62 if (prot == PROT_NONE)
65 if ((prot & PROT_READ) != 0)
66 desiredAccess |= FILE_MAP_READ;
67 if ((prot & PROT_WRITE) != 0)
68 desiredAccess |= FILE_MAP_WRITE;
69 if ((prot & PROT_EXEC) != 0)
70 desiredAccess |= FILE_MAP_EXECUTE;
75 void* mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
79 void * map = MAP_FAILED;
83 #pragma warning(disable: 4293)
86 const DWORD dwFileOffsetLow = (sizeof(off_t) <= sizeof(DWORD)) ?
87 (DWORD)off : (DWORD)(off & 0xFFFFFFFFL);
88 const DWORD dwFileOffsetHigh = (sizeof(off_t) <= sizeof(DWORD)) ?
89 (DWORD)0 : (DWORD)((off >> 32) & 0xFFFFFFFFL);
90 const DWORD protect = __map_mmap_prot_page(prot);
91 const DWORD desiredAccess = __map_mmap_prot_file(prot);
93 const off_t maxSize = off + (off_t)len;
95 const DWORD dwMaxSizeLow = (sizeof(off_t) <= sizeof(DWORD)) ?
96 (DWORD)maxSize : (DWORD)(maxSize & 0xFFFFFFFFL);
97 const DWORD dwMaxSizeHigh = (sizeof(off_t) <= sizeof(DWORD)) ?
98 (DWORD)0 : (DWORD)((maxSize >> 32) & 0xFFFFFFFFL);
107 /* Unsupported flag combinations */
108 || (flags & MAP_FIXED) != 0
109 /* Usupported protection combinations */
110 || prot == PROT_EXEC)
116 h = ((flags & MAP_ANONYMOUS) == 0) ?
117 (HANDLE)_get_osfhandle(fildes) : INVALID_HANDLE_VALUE;
119 if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE)
125 fm = CreateFileMapping(h, NULL, protect, dwMaxSizeHigh, dwMaxSizeLow, NULL);
130 map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len);
139 errno = __map_mman_error(GetLastError(), EPERM);
143 int munmap(void *addr, size_t len)
145 if (UnmapViewOfFile(addr))
148 errno = __map_mman_error(GetLastError(), EPERM);
153 int mprotect(void *addr, size_t len, int prot)
155 DWORD newProtect = __map_mmap_prot_page(prot);
156 DWORD oldProtect = 0;
158 if (VirtualProtect(addr, len, newProtect, &oldProtect))
161 errno = __map_mman_error(GetLastError(), EPERM);
166 int msync(void *addr, size_t len, int flags)
168 if (FlushViewOfFile(addr, len))
171 errno = __map_mman_error(GetLastError(), EPERM);
176 int mlock(const void *addr, size_t len)
178 if (VirtualLock((LPVOID)addr, len))
181 errno = __map_mman_error(GetLastError(), EPERM);
186 int munlock(const void *addr, size_t len)
188 if (VirtualUnlock((LPVOID)addr, len))
191 errno = __map_mman_error(GetLastError(), EPERM);