Fabcoin Core  0.16.2
P2P Digital Currency
mmap_win32.c
Go to the documentation of this file.
1 /* mmap() replacement for Windows
2  *
3  * Author: Mike Frysinger <vapier@gentoo.org>
4  * Placed into the public domain
5  */
6 
7 /* References:
8  * CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
9  * CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
10  * MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
11  * UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
12  */
13 #if defined(__MINGW32__) || defined(_WIN32)
14 #include <io.h>
15 #include <windows.h>
16 #include "mmap.h"
17 
18 #ifdef __USE_FILE_OFFSET64
19 # define DWORD_HI(x) (x >> 32)
20 # define DWORD_LO(x) ((x) & 0xffffffff)
21 #else
22 # define DWORD_HI(x) (0)
23 # define DWORD_LO(x) (x)
24 #endif
25 
26 #define UNUSED(x) (void)(x)
27 
28 void* mmap(void* start, size_t length, int prot, int flags, int fd, off_t offset)
29 {
30  UNUSED(start);
31  UNUSED(length);
32 
33  if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
34  return MAP_FAILED;
35  if (fd == -1) {
36  if (!(flags & MAP_ANON) || offset)
37  return MAP_FAILED;
38  } else if (flags & MAP_ANON)
39  return MAP_FAILED;
40 
41  DWORD flProtect;
42  if (prot & PROT_WRITE) {
43  if (prot & PROT_EXEC)
44  flProtect = PAGE_EXECUTE_READWRITE;
45  else
46  flProtect = PAGE_READWRITE;
47  } else if (prot & PROT_EXEC) {
48  if (prot & PROT_READ)
49  flProtect = PAGE_EXECUTE_READ;
50  else if (prot & PROT_EXEC)
51  flProtect = PAGE_EXECUTE;
52  } else
53  flProtect = PAGE_READONLY;
54 
55  off_t end = length + offset;
56  HANDLE mmap_fd, h;
57  if (fd == -1)
58  mmap_fd = INVALID_HANDLE_VALUE;
59  else
60  mmap_fd = (HANDLE)_get_osfhandle(fd);
61  h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL);
62  if (h == NULL)
63  return MAP_FAILED;
64 
65  DWORD dwDesiredAccess;
66  if (prot & PROT_WRITE)
67  dwDesiredAccess = FILE_MAP_WRITE;
68  else
69  dwDesiredAccess = FILE_MAP_READ;
70  if (prot & PROT_EXEC)
71  dwDesiredAccess |= FILE_MAP_EXECUTE;
72  if (flags & MAP_PRIVATE)
73  dwDesiredAccess |= FILE_MAP_COPY;
74  void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
75  if (ret == NULL) {
76  ret = MAP_FAILED;
77  }
78  // since we are handling the file ourselves with fd, close the Windows Handle here
79  CloseHandle(h);
80  return ret;
81 }
82 
83 void munmap(void* addr, size_t length)
84 {
85  UNUSED(length);
86 
87  UnmapViewOfFile(addr);
88 }
89 
90 #undef DWORD_HI
91 #undef DWORD_LO
92 
93 #endif
#define h(i)
Definition: sha.cpp:736
#define UNUSED
#define fd(x)
Definition: rijndael.cpp:172