llvm-project
187 строк · 4.4 Кб
1/*
2* This code is derived from uClibc (original license follows).
3* https://git.uclibc.org/uClibc/tree/utils/mmap-windows.c
4*/
5/* mmap() replacement for Windows
6*
7* Author: Mike Frysinger <vapier@gentoo.org>
8* Placed into the public domain
9*/
10
11/* References:
12* CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
13* CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
14* MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
15* UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
16*/
17
18#if defined(_WIN32)
19
20#include "WindowsMMap.h"
21
22#define WIN32_LEAN_AND_MEAN
23#include <windows.h>
24
25#include "InstrProfiling.h"
26
27COMPILER_RT_VISIBILITY
28void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
29{
30if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
31return MAP_FAILED;
32if (fd == -1) {
33if (!(flags & MAP_ANON) || offset)
34return MAP_FAILED;
35} else if (flags & MAP_ANON)
36return MAP_FAILED;
37
38DWORD flProtect;
39if (prot & PROT_WRITE) {
40if (prot & PROT_EXEC)
41flProtect = PAGE_EXECUTE_READWRITE;
42else
43flProtect = PAGE_READWRITE;
44} else if (prot & PROT_EXEC) {
45if (prot & PROT_READ)
46flProtect = PAGE_EXECUTE_READ;
47else if (prot & PROT_EXEC)
48flProtect = PAGE_EXECUTE;
49} else
50flProtect = PAGE_READONLY;
51
52off_t end = length + offset;
53HANDLE mmap_fd, h;
54if (fd == -1)
55mmap_fd = INVALID_HANDLE_VALUE;
56else
57mmap_fd = (HANDLE)_get_osfhandle(fd);
58h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL);
59if (h == NULL)
60return MAP_FAILED;
61
62DWORD dwDesiredAccess;
63if (prot & PROT_WRITE)
64dwDesiredAccess = FILE_MAP_WRITE;
65else
66dwDesiredAccess = FILE_MAP_READ;
67if (prot & PROT_EXEC)
68dwDesiredAccess |= FILE_MAP_EXECUTE;
69if (flags & MAP_PRIVATE)
70dwDesiredAccess |= FILE_MAP_COPY;
71void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
72if (ret == NULL) {
73CloseHandle(h);
74ret = MAP_FAILED;
75}
76return ret;
77}
78
79COMPILER_RT_VISIBILITY
80void munmap(void *addr, size_t length)
81{
82UnmapViewOfFile(addr);
83/* ruh-ro, we leaked handle from CreateFileMapping() ... */
84}
85
86COMPILER_RT_VISIBILITY
87int msync(void *addr, size_t length, int flags)
88{
89if (flags & MS_INVALIDATE)
90return -1; /* Not supported. */
91
92/* Exactly one of MS_ASYNC or MS_SYNC must be specified. */
93switch (flags & (MS_ASYNC | MS_SYNC)) {
94case MS_SYNC:
95case MS_ASYNC:
96break;
97default:
98return -1;
99}
100
101if (!FlushViewOfFile(addr, length))
102return -1;
103
104if (flags & MS_SYNC) {
105/* FIXME: No longer have access to handle from CreateFileMapping(). */
106/*
107* if (!FlushFileBuffers(h))
108* return -1;
109*/
110}
111
112return 0;
113}
114
115COMPILER_RT_VISIBILITY
116int madvise(void *addr, size_t length, int advice)
117{
118if (advice != MADV_DONTNEED)
119return -1; /* Not supported. */
120
121if (!VirtualUnlock(addr, length))
122return -1;
123
124return 0;
125}
126
127static int lock(HANDLE handle, DWORD lockType, BOOL blocking) {
128DWORD flags = lockType;
129if (!blocking)
130flags |= LOCKFILE_FAIL_IMMEDIATELY;
131
132OVERLAPPED overlapped;
133ZeroMemory(&overlapped, sizeof(OVERLAPPED));
134overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
135BOOL result = LockFileEx(handle, flags, 0, MAXDWORD, MAXDWORD, &overlapped);
136if (!result) {
137DWORD dw = GetLastError();
138
139// In non-blocking mode, return an error if the file is locked.
140if (!blocking && dw == ERROR_LOCK_VIOLATION)
141return -1; // EWOULDBLOCK
142
143// If the error is ERROR_IO_PENDING, we need to wait until the operation
144// finishes. Otherwise, we return an error.
145if (dw != ERROR_IO_PENDING)
146return -1;
147
148DWORD dwNumBytes;
149if (!GetOverlappedResult(handle, &overlapped, &dwNumBytes, TRUE))
150return -1;
151}
152
153return 0;
154}
155
156COMPILER_RT_VISIBILITY
157int flock(int fd, int operation) {
158HANDLE handle = (HANDLE)_get_osfhandle(fd);
159if (handle == INVALID_HANDLE_VALUE)
160return -1;
161
162BOOL blocking = (operation & LOCK_NB) == 0;
163int op = operation & ~LOCK_NB;
164
165switch (op) {
166case LOCK_EX:
167return lock(handle, LOCKFILE_EXCLUSIVE_LOCK, blocking);
168
169case LOCK_SH:
170return lock(handle, 0, blocking);
171
172case LOCK_UN:
173if (!UnlockFile(handle, 0, 0, MAXDWORD, MAXDWORD))
174return -1;
175break;
176
177default:
178return -1;
179}
180
181return 0;
182}
183
184#undef DWORD_HI
185#undef DWORD_LO
186
187#endif /* _WIN32 */
188