55652006d5aff540642f1fda890f4fd52aaf1f31
[mono.git] / mono / utils / mono-mmap-windows-uwp.c
1 /*
2  * mono-dl-windows-uwp.c: UWP dl support for Mono.
3  *
4  * Copyright 2016 Microsoft
5  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6 */
7 #include <config.h>
8 #include <glib.h>
9
10 #if G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT)
11 #include <Windows.h>
12 #include <mono/utils/mono-mmap-windows.h>
13
14 void*
15 mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
16 {
17         void *ptr;
18         int mflags = 0;
19         HANDLE file, mapping;
20         int prot = mono_mmap_win_prot_from_flags (flags);
21
22         mflags = FILE_MAP_READ;
23         if (flags & MONO_MMAP_WRITE)
24                 mflags = FILE_MAP_COPY;
25
26         file = (HANDLE) _get_osfhandle (fd);
27         mapping = CreateFileMappingFromApp (file, NULL, prot, length, NULL);
28
29         if (mapping == NULL)
30                 return NULL;
31
32         ptr = MapViewOfFileFromApp (mapping, mflags, offset, length);
33
34         if (ptr == NULL) {
35                 CloseHandle (mapping);
36                 return NULL;
37         }
38
39         *ret_handle = (void*)mapping;
40         return ptr;
41 }
42
43 int
44 mono_file_unmap (void *addr, void *handle)
45 {
46         UnmapViewOfFile (addr);
47         CloseHandle ((HANDLE)handle);
48         return 0;
49 }
50
51 #else /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */
52
53 #ifdef _MSC_VER
54 // Quiet Visual Studio linker warning, LNK4221, in cases when this source file intentional ends up empty.
55 void __mono_win32_mono_mmap_windows_uwp_quiet_lnk4221(void) {}
56 #endif
57 #endif /* G_HAVE_API_SUPPORT(HAVE_UWP_WINAPI_SUPPORT) */