blob: f44dec88fc817d5a97a083a93b1afc038e72caf2 [file] [log] [blame]
Zheng Bao54516722012-10-22 16:41:42 +08001#include "common.h"
2#include <windows.h>
3
4static inline size_t xsize_t(off_t len)
5{
6 if (len > (size_t) len)
7 die("Cannot handle files this big");
8 return (size_t)len;
9}
10
11void *win32_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
12{
13 HANDLE hmap;
14 void *temp;
15 off_t len;
16 struct stat st;
17 uint64_t o = offset;
18 uint32_t l = o & 0xFFFFFFFF;
19 uint32_t h = (o >> 32) & 0xFFFFFFFF;
20
21 if (!fstat(fd, &st))
22 len = st.st_size;
23 else
24 printf("mmap: could not determine filesize");
25
26 if ((length + offset) > len)
27 length = xsize_t(len - offset);
28
29 if (!(flags & MAP_PRIVATE))
30 printf("Invalid usage of mmap when built with USE_WIN32_MMAP");
31
32 hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), 0, PAGE_WRITECOPY,
33 0, 0, 0);
34
35 if (!hmap)
36 return MAP_FAILED;
37
38 temp = MapViewOfFileEx(hmap, FILE_MAP_COPY, h, l, length, start);
39
40 if (!CloseHandle(hmap))
41 printf("unable to close file mapping handle");
42
43 return temp ? temp : MAP_FAILED;
44}
45
46int win32_munmap(void *start, size_t length)
47{
48 return !UnmapViewOfFile(start);
49}