void* mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) { HANDLE fm, h; void * map = MAP_FAILED; const DWORD dwFileOffsetLow = (sizeof(off_t) <= sizeof(DWORD)) ? (DWORD)off : (DWORD)(off & 0xFFFFFFFFL); const DWORD dwFileOffsetHigh = (sizeof(off_t) <= sizeof(DWORD)) ? (DWORD)0 : (DWORD)((off >> 32) & 0xFFFFFFFFL); const DWORD protect = __map_mmap_prot_page(prot); const DWORD desiredAccess = __map_mmap_prot_file(prot); errno = 0; if (len == 0 /* Unsupported flag combinations */ || (flags & MAP_FIXED) != 0 /* Usupported protection combinations */ || prot == PROT_EXEC) { errno = EINVAL; return MAP_FAILED; } h = ((flags & MAP_ANONYMOUS) == 0) ? (HANDLE)_get_osfhandle(fildes) : INVALID_HANDLE_VALUE; if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE) { errno = EBADF; return MAP_FAILED; } fm = CreateFileMapping(h, NULL, protect, 0, len, NULL); if (fm == NULL) { errno = __map_mman_error(GetLastError(), EPERM); return MAP_FAILED; } map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len); CloseHandle(fm); if (map == NULL) { errno = __map_mman_error(GetLastError(), EPERM); return MAP_FAILED; } return map; }
int mprotect(void *addr, size_t len, int prot) { DWORD newProtect = __map_mmap_prot_page(prot); DWORD oldProtect = 0; if (VirtualProtect(addr, len, newProtect, &oldProtect)) return 0; errno = __map_mman_error(GetLastError(), EPERM); return -1; }
int mprotect(void *addr, size_t len, int prot) { u32 n = __map_mmap_prot_page(prot); DWORD o = 0; if (VirtualProtect(addr, len, n, &o)) return 0; errno = EPERM; return -1; }
void * mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) { HANDLE fm, h; void *map = MAP_FAILED; const u32 offlow = (sizeof(off_t) <= sizeof(u32)) ? (u32)off : (u32)(off & 0xFFFFFFFFL); const u32 offhigh = (sizeof(off_t) <= sizeof(u32)) ? (u32)0 : (u32)((off >> 32) & 0xFFFFFFFFL); const u32 protect = __map_mmap_prot_page(prot); const u32 acc = __map_mmap_prot_file(prot); const off_t maxSize = off + (off_t)len; const u32 msizelow = (sizeof(off_t) <= sizeof(u32)) ? (u32)maxSize : (u32)(maxSize & 0xFFFFFFFFL); const u32 msizehi = (sizeof(off_t) <= sizeof(u32)) ? (u32)0 : (u32)((maxSize >> 32) & 0xFFFFFFFFL); errno = 0; if (len == 0 || (flags & MAP_FIXED) != 0 || prot == PROT_EXEC) { errno = EINVAL; return MAP_FAILED; } h = ((flags & MAP_ANONYMOUS) == 0) ? (HANDLE)_get_osfhandle(fildes) : INVALID_HANDLE_VALUE; if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE) { errno = EBADF; return MAP_FAILED; } fm = CreateFileMapping(h, NULL, protect, msizehi, msizelow, NULL); if (fm == NULL) { errno = EPERM; return MAP_FAILED; } map = MapViewOfFile(fm, acc, offhigh, offlow, len); CloseHandle(fm); if (map == NULL) { errno = EPERM; return MAP_FAILED; } return map; }
void* mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) { HANDLE fm, h; void * map = MAP_FAILED; #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable: 4293) #endif const DWORD dwFileOffsetLow = (DWORD)off; const DWORD dwFileOffsetHigh = (DWORD)0; const DWORD protect = __map_mmap_prot_page(prot); const DWORD desiredAccess = __map_mmap_prot_file(prot); const off_t maxSize = off + (off_t)len; const DWORD dwMaxSizeLow = (DWORD)maxSize; const DWORD dwMaxSizeHigh = (DWORD)0; #ifdef _MSC_VER #pragma warning(pop) #endif errno = 0; if (len == 0 /* Unsupported flag combinations */ || (flags & MAP_FIXED) != 0 /* Usupported protection combinations */ || prot == PROT_EXEC) { errno = EINVAL; return MAP_FAILED; } h = ((flags & MAP_ANONYMOUS) == 0) ? (HANDLE)_get_osfhandle(fildes) : INVALID_HANDLE_VALUE; if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE) { errno = EBADF; return MAP_FAILED; } fm = CreateFileMapping(h, NULL, protect, dwMaxSizeHigh, dwMaxSizeLow, NULL); if (fm == NULL) { errno = __map_mman_error(GetLastError(), EPERM); return MAP_FAILED; } map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len); CloseHandle(fm); if (map == NULL) { errno = __map_mman_error(GetLastError(), EPERM); return MAP_FAILED; } return map; }