@@ -35,12 +35,13 @@ static DWORD protect_page(int prot) noexcept
3535
3636 if ((prot & PROT_EXEC) != 0 )
3737 {
38- return ((prot & PROT_WRITE) != 0 ) ? PAGE_EXECUTE_READWRITE :
39- PAGE_EXECUTE_READ;
38+ return ((prot & PROT_WRITE) != 0 ) ?
39+ PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ;
4040 }
4141 else
4242 {
43- return ((prot & PROT_WRITE) != 0 ) ? PAGE_READWRITE : PAGE_READONLY;
43+ return ((prot & PROT_WRITE) != 0 ) ?
44+ PAGE_READWRITE : PAGE_READONLY;
4445 }
4546}
4647
@@ -65,6 +66,8 @@ static DWORD protect_file(int prot) noexcept
6566
6667// public interface
6768
69+ // TODO: FILE_FLAG_SEQUENTIAL_SCAN or FILE_FLAG_RANDOM_ACCESS.
70+
6871void * mmap (void * /* addr*/ , size_t len, int prot, int flags, int fd,
6972 oft__ off) noexcept
7073{
@@ -79,38 +82,37 @@ void* mmap(void* /*addr*/, size_t len, int prot, int flags, int fd,
7982 const auto file_lo = large ? (DWORD)((off) & MAXDWORD) : (DWORD)off;
8083 const auto file_hi = large ? (DWORD)((off >> 32 ) & MAXDWORD) : (DWORD)0 ;
8184
82- if (len == 0 || (flags & MAP_FIXED) != 0 || prot == PROT_EXEC)
85+ // Disallow fixed, executable, and or anonymous mapping.
86+ if (len == 0 ||
87+ (prot == PROT_EXEC) ||
88+ (flags & MAP_FIXED) != 0 ||
89+ (flags & MAP_ANONYMOUS) != 0 )
8390 {
8491 errno = EINVAL;
8592 return MAP_FAILED;
8693 }
8794
8895 // Never call CloseHandle on the return value of this function.
89- const auto handle = ((flags & MAP_ANONYMOUS) == 0 ) ?
90- (HANDLE)_get_osfhandle (fd) : INVALID_HANDLE_VALUE;
91-
92- if ((flags & MAP_ANONYMOUS) == 0 && handle == INVALID_HANDLE_VALUE)
96+ const auto handle = (HANDLE)_get_osfhandle (fd);
97+ if (handle == INVALID_HANDLE_VALUE)
9398 {
9499 errno = EBADF;
95100 return MAP_FAILED;
96101 }
97102
98- const auto mapping = CreateFileMappingW (handle, NULL , protect, max_hi,
99- max_lo, NULL );
100-
103+ const auto mapping = CreateFileMappingW (handle, NULL , protect, max_hi, max_lo, NULL );
101104 if (mapping == NULL )
102105 {
103106 errno = last_error (EPERM);
104107 return MAP_FAILED;
105108 }
106109
107- const auto map = MapViewOfFile (mapping, access, file_hi, file_lo, len);
108-
109110 // "to fully close a file mapping object, an application must unmap all
110111 // mapped views of the file mapping object by calling UnmapViewOfFile and
111112 // close the file mapping object handle by calling CloseHandle. These
112113 // functions can be called in any order." - so we close the handle here and
113114 // retain only the map.
115+ const auto map = MapViewOfFile (mapping, access, file_hi, file_lo, len);
114116 if (map == NULL || CloseHandle (mapping) == FALSE )
115117 {
116118 errno = last_error (EPERM);
@@ -121,6 +123,11 @@ void* mmap(void* /*addr*/, size_t len, int prot, int flags, int fd,
121123 return map;
122124}
123125
126+ // HACK: stub is not called for win32.
127+ void * mremap (void *, size_t , size_t , int ) noexcept
128+ {
129+ }
130+
124131// HACK: fd parameter used to pass file descripter, not linux compatible.
125132// HACK: No mremap equivalent, munmap and mmap are required to change size.
126133void * mremap_ (void * addr, size_t old_size, size_t new_size, int prot,
@@ -156,7 +163,6 @@ int mprotect(void* addr, size_t len, int prot) noexcept
156163{
157164 DWORD old_protect = 0 ;
158165 const auto new_protect = protect_page (prot);
159-
160166 if (VirtualProtect (addr, len, new_protect, &old_protect) == FALSE )
161167 {
162168 errno = last_error (EPERM);
@@ -271,4 +277,10 @@ int ftruncate(int fd, oft__ size) noexcept
271277 return 0 ;
272278}
273279
280+ // stub
281+ int sysconf (int ) noexcept
282+ {
283+ return {};
284+ }
285+
274286#endif // _WIN32
0 commit comments