File Coverage

lib/File/mmap-compat.c
Criterion Covered Total %
statement 3 3 100.0
branch 2 2 100.0
condition n/a
subroutine n/a
pod n/a
total 5 5 100.0


line stmt bran cond sub pod time code
1             #if defined(WIN32) && !defined(__CYGWIN__)
2              
3             # include
4             # include
5             # define PROT_NONE 0
6             # define PROT_READ 1
7             # define PROT_WRITE 2
8             # define PROT_EXEC 4
9             # define MAP_SHARED 0
10             # define MAP_PRIVATE 1
11             # define MAP_ANONYMOUS 2
12             # define MAP_FAILED ((void *) -1)
13              
14             # define PROT_ALL (PROT_READ | PROT_WRITE | PROT_EXEC)
15             # ifndef FILE_MAP_EXECUTE
16             # define FILE_MAP_EXECUTE 0
17             # endif
18             # define BITS32_MASK 0xFFFFFFFF
19              
20              
21             static DWORD page_size() {
22             static DWORD pagesize = 0;
23             if (pagesize == 0) {
24             SYSTEM_INFO info;
25             GetSystemInfo(&info);
26             pagesize = info.dwPageSize;
27             }
28             return pagesize;
29             }
30              
31             static DWORD old_protect;
32              
33             static const struct {
34             DWORD createflag;
35             DWORD viewflag;
36             } winflags[] = {
37             { 0 , 0 }, /* PROT_NONE */
38             { PAGE_READONLY , FILE_MAP_READ }, /* PROT_READ */
39             { PAGE_READWRITE, FILE_MAP_WRITE }, /* PROT_WRITE */
40             { PAGE_READWRITE, FILE_MAP_ALL_ACCESS }, /* PROT_READ | PROT_WRITE */
41             { PAGE_EXECUTE_READ , FILE_MAP_READ | FILE_MAP_EXECUTE}, /* PROT_NONE | PROT_EXEC */
42             { PAGE_EXECUTE_READ , FILE_MAP_READ | FILE_MAP_EXECUTE}, /* PROT_READ | PROT_EXEC */
43             { PAGE_EXECUTE_READWRITE, FILE_MAP_WRITE | FILE_MAP_EXECUTE}, /* PROT_WRITE | PROT_EXEC */
44             { PAGE_EXECUTE_READWRITE, FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE}, /* PROT_READ| PROT_WRITE | PROT_EXEC */
45             };
46              
47             void* mmap(void* address, size_t length, int prot, int flags, int fd, unsigned long long offset) {
48             HANDLE file;
49             HANDLE mapping;
50             DWORD viewflag;
51             unsigned long long maxsize = offset + length;
52             prot &= PROT_ALL;
53             file = (flags & MAP_ANONYMOUS) ? INVALID_HANDLE_VALUE : (HANDLE)_get_osfhandle(fd);
54             mapping = CreateFileMapping(file, NULL, winflags[prot].createflag, maxsize >> 32, maxsize & BITS32_MASK, NULL);
55             if (mapping == NULL)
56             return MAP_FAILED;
57             viewflag = (flags & MAP_PRIVATE) ? (FILE_MAP_COPY | ( prot & PROT_EXEC ? FILE_MAP_EXECUTE : 0 ) ) : winflags[prot].viewflag;
58             address = MapViewOfFile(mapping, viewflag, offset >> 32, offset & BITS32_MASK, length);
59             CloseHandle(mapping);
60             if (address == NULL)
61             return MAP_FAILED;
62             return address;
63             }
64              
65             # define munmap(address, length) ( UnmapViewOfFile(address) ? 0 : -1 )
66             # define msync(address, length, flags) ( FlushViewOfFile(address, length) ? 0 : -1 )
67             # define mlock(address, length) ( VirtualLock(address, length) ? 0 : -1 )
68             # define munlock(address, length) ( VirtualUnlock(address, length) ? 0 : -1 )
69             # define mprotect(address, length, prot) ( VirtualProtect(address, length, winflags[prot & PROT_ALL].createflag, &old_protect) ? 0 : -1 )
70              
71             #define madvise(address, length, advice) 0
72              
73             #else /* WIN32 */
74              
75             # include
76             # include
77             # include
78             # include
79              
80             static size_t page_size() {
81             static size_t pagesize = 0;
82 20 100         if (pagesize == 0)
83 7           pagesize = sysconf(_SC_PAGESIZE);
84 20           return pagesize;
85             }
86              
87             #ifdef VMS
88             # define madvise(address, length, advice) 0
89             #elif defined(__CYGWIN__) || defined(__QNX__)
90             # define madvise posix_madvise
91             #endif
92              
93             #endif /* WIN32 */
94              
95             #ifndef MADV_NORMAL
96             # define MADV_NORMAL 0
97             # define MADV_RANDOM 0
98             # define MADV_SEQUENTIAL 0
99             # define MADV_WILLNEED 0
100             # define MADV_DONTNEED 0
101             #endif
102              
103             #ifndef MAP_ANONYMOUS
104             # define MAP_ANONYMOUS MAP_ANON
105             #elif !defined MAP_ANON
106             # define MAP_ANON MAP_ANONYMOUS
107             #endif /* MAP_ANONYMOUS */
108              
109             #ifndef MAP_FILE
110             # define MAP_FILE 0
111             #endif
112              
113             #ifndef MAP_VARIABLE
114             # define MAP_VARIABLE 0
115             #endif
116