1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (memmap.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.
28 #define PROT_READ 0x1 /* Page can be read */
32 #define PROT_WRITE 0x2 /* Page can be written. */
35 #ifndef PROT_READWRITE
36 #define PROT_READWRITE 0x3 /* Page can be written to and read from. */
40 #define PROT_EXEC 0x4 /* Page can be executed. */
44 #define PROT_NONE 0x0 /* Page can not be accessed. */
48 #define MAP_FAILED ((void *) -1)
52 void* mmap(void *addr, size_t len, int prot, int flags,
53 int fildes, size_t offset)
55 void *map = (void*)NULL;
56 HANDLE handle = INVALID_HANDLE_VALUE;
62 handle = CreateFileMapping((HANDLE)
63 _get_osfhandle(fildes), 0, PAGE_READONLY, 0,
67 map = (void*)MapViewOfFile(handle, FILE_MAP_READ, 0, 0, len);
71 handle = CreateFileMapping((HANDLE)
72 _get_osfhandle(fildes),0,PAGE_READWRITE,0,
76 map = (void*)MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, len);
80 handle = CreateFileMapping((HANDLE)
81 _get_osfhandle(fildes),0,PAGE_READWRITE,0,
85 map = (void*)MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, len);
90 if (map == (void*)NULL)
91 return((void*)MAP_FAILED);
92 return((void*) ((int8_t*)map + offset));
95 int munmap(void *addr, size_t length)
97 if (!UnmapViewOfFile(addr))
102 int mprotect(void *addr, size_t len, int prot)
104 /* Incomplete, just assumes PAGE_EXECUTE_READWRITE right now
105 * instead of correctly handling prot */
107 if (prot & (PROT_READ | PROT_WRITE | PROT_EXEC))
108 prot = PAGE_EXECUTE_READWRITE;
109 return VirtualProtect(addr, len, prot, 0);
112 #elif !defined(HAVE_MMAN)
113 void* mmap(void *addr, size_t len, int prot, int flags,
114 int fildes, size_t offset)
119 int munmap(void *addr, size_t len)
125 int mprotect(void *addr, size_t len, int prot)
127 /* stub - not really needed at this point
128 * since this codepath has no dynarecs. */
134 #if defined(__MACH__) && defined(__arm__)
135 #include <libkern/OSCacheControl.h>
138 int memsync(void *start, void *end)
140 size_t len = (char*)end - (char*)start;
141 #if defined(__MACH__) && defined(__arm__)
142 sys_dcache_flush(start ,len);
143 sys_icache_invalidate(start, len);
145 #elif defined(__arm__) && !defined(__QNX__)
147 __clear_cache(start, end);
149 #elif defined(HAVE_MMAN)
150 return msync(start, len, MS_SYNC | MS_INVALIDATE
161 int memprotect(void *addr, size_t len)
163 return mprotect(addr, len, PROT_READ | PROT_WRITE | PROT_EXEC);