Home | History | Annotate | Line # | Download | only in profile
      1  1.1  joerg /*
      2  1.1  joerg  * This code is derived from uClibc (original license follows).
      3  1.1  joerg  * https://git.uclibc.org/uClibc/tree/utils/mmap-windows.c
      4  1.1  joerg  */
      5  1.1  joerg  /* mmap() replacement for Windows
      6  1.1  joerg  *
      7  1.1  joerg  * Author: Mike Frysinger <vapier (at) gentoo.org>
      8  1.1  joerg  * Placed into the public domain
      9  1.1  joerg  */
     10  1.1  joerg 
     11  1.1  joerg /* References:
     12  1.1  joerg  * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
     13  1.1  joerg  * CloseHandle:       http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
     14  1.1  joerg  * MapViewOfFile:     http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
     15  1.1  joerg  * UnmapViewOfFile:   http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
     16  1.1  joerg  */
     17  1.1  joerg 
     18  1.1  joerg #if defined(_WIN32)
     19  1.1  joerg 
     20  1.1  joerg #include "WindowsMMap.h"
     21  1.1  joerg #include "InstrProfiling.h"
     22  1.1  joerg 
     23  1.1  joerg #ifdef __USE_FILE_OFFSET64
     24  1.1  joerg # define DWORD_HI(x) (x >> 32)
     25  1.1  joerg # define DWORD_LO(x) ((x) & 0xffffffff)
     26  1.1  joerg #else
     27  1.1  joerg # define DWORD_HI(x) (0)
     28  1.1  joerg # define DWORD_LO(x) (x)
     29  1.1  joerg #endif
     30  1.1  joerg 
     31  1.1  joerg COMPILER_RT_VISIBILITY
     32  1.1  joerg void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
     33  1.1  joerg {
     34  1.1  joerg   if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
     35  1.1  joerg     return MAP_FAILED;
     36  1.1  joerg   if (fd == -1) {
     37  1.1  joerg     if (!(flags & MAP_ANON) || offset)
     38  1.1  joerg       return MAP_FAILED;
     39  1.1  joerg   } else if (flags & MAP_ANON)
     40  1.1  joerg     return MAP_FAILED;
     41  1.1  joerg 
     42  1.1  joerg   DWORD flProtect;
     43  1.1  joerg   if (prot & PROT_WRITE) {
     44  1.1  joerg     if (prot & PROT_EXEC)
     45  1.1  joerg       flProtect = PAGE_EXECUTE_READWRITE;
     46  1.1  joerg     else
     47  1.1  joerg       flProtect = PAGE_READWRITE;
     48  1.1  joerg   } else if (prot & PROT_EXEC) {
     49  1.1  joerg     if (prot & PROT_READ)
     50  1.1  joerg       flProtect = PAGE_EXECUTE_READ;
     51  1.1  joerg     else if (prot & PROT_EXEC)
     52  1.1  joerg       flProtect = PAGE_EXECUTE;
     53  1.1  joerg   } else
     54  1.1  joerg     flProtect = PAGE_READONLY;
     55  1.1  joerg 
     56  1.1  joerg   off_t end = length + offset;
     57  1.1  joerg   HANDLE mmap_fd, h;
     58  1.1  joerg   if (fd == -1)
     59  1.1  joerg     mmap_fd = INVALID_HANDLE_VALUE;
     60  1.1  joerg   else
     61  1.1  joerg     mmap_fd = (HANDLE)_get_osfhandle(fd);
     62  1.1  joerg   h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL);
     63  1.1  joerg   if (h == NULL)
     64  1.1  joerg     return MAP_FAILED;
     65  1.1  joerg 
     66  1.1  joerg   DWORD dwDesiredAccess;
     67  1.1  joerg   if (prot & PROT_WRITE)
     68  1.1  joerg     dwDesiredAccess = FILE_MAP_WRITE;
     69  1.1  joerg   else
     70  1.1  joerg     dwDesiredAccess = FILE_MAP_READ;
     71  1.1  joerg   if (prot & PROT_EXEC)
     72  1.1  joerg     dwDesiredAccess |= FILE_MAP_EXECUTE;
     73  1.1  joerg   if (flags & MAP_PRIVATE)
     74  1.1  joerg     dwDesiredAccess |= FILE_MAP_COPY;
     75  1.1  joerg   void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
     76  1.1  joerg   if (ret == NULL) {
     77  1.1  joerg     CloseHandle(h);
     78  1.1  joerg     ret = MAP_FAILED;
     79  1.1  joerg   }
     80  1.1  joerg   return ret;
     81  1.1  joerg }
     82  1.1  joerg 
     83  1.1  joerg COMPILER_RT_VISIBILITY
     84  1.1  joerg void munmap(void *addr, size_t length)
     85  1.1  joerg {
     86  1.1  joerg   UnmapViewOfFile(addr);
     87  1.1  joerg   /* ruh-ro, we leaked handle from CreateFileMapping() ... */
     88  1.1  joerg }
     89  1.1  joerg 
     90  1.1  joerg COMPILER_RT_VISIBILITY
     91  1.1  joerg int msync(void *addr, size_t length, int flags)
     92  1.1  joerg {
     93  1.1  joerg   if (flags & MS_INVALIDATE)
     94  1.1  joerg     return -1; /* Not supported. */
     95  1.1  joerg 
     96  1.1  joerg   /* Exactly one of MS_ASYNC or MS_SYNC must be specified. */
     97  1.1  joerg   switch (flags & (MS_ASYNC | MS_SYNC)) {
     98  1.1  joerg     case MS_SYNC:
     99  1.1  joerg     case MS_ASYNC:
    100  1.1  joerg       break;
    101  1.1  joerg     default:
    102  1.1  joerg       return -1;
    103  1.1  joerg   }
    104  1.1  joerg 
    105  1.1  joerg   if (!FlushViewOfFile(addr, length))
    106  1.1  joerg     return -1;
    107  1.1  joerg 
    108  1.1  joerg   if (flags & MS_SYNC) {
    109  1.1  joerg     /* FIXME: No longer have access to handle from CreateFileMapping(). */
    110  1.1  joerg     /*
    111  1.1  joerg      * if (!FlushFileBuffers(h))
    112  1.1  joerg      *   return -1;
    113  1.1  joerg      */
    114  1.1  joerg   }
    115  1.1  joerg 
    116  1.1  joerg   return 0;
    117  1.1  joerg }
    118  1.1  joerg 
    119  1.1  joerg COMPILER_RT_VISIBILITY
    120  1.1  joerg int flock(int fd, int operation)
    121  1.1  joerg {
    122  1.1  joerg   return -1; /* Not supported. */
    123  1.1  joerg }
    124  1.1  joerg 
    125  1.1  joerg #undef DWORD_HI
    126  1.1  joerg #undef DWORD_LO
    127  1.1  joerg 
    128  1.1  joerg #endif /* _WIN32 */
    129