Merge branch 'cb/maint-orphan-merge-noclobber'
[git/kirr.git] / compat / win32mmap.c
blobb58aa69fa0609dad7f591024f9da31dfa58496fb
1 #include "../git-compat-util.h"
3 void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
5 HANDLE hmap;
6 void *temp;
7 off_t len;
8 struct stat st;
9 uint64_t o = offset;
10 uint32_t l = o & 0xFFFFFFFF;
11 uint32_t h = (o >> 32) & 0xFFFFFFFF;
13 if (!fstat(fd, &st))
14 len = st.st_size;
15 else
16 die("mmap: could not determine filesize");
18 if ((length + offset) > len)
19 length = xsize_t(len - offset);
21 if (!(flags & MAP_PRIVATE))
22 die("Invalid usage of mmap when built with USE_WIN32_MMAP");
24 hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), 0, PAGE_WRITECOPY,
25 0, 0, 0);
27 if (!hmap)
28 return MAP_FAILED;
30 temp = MapViewOfFileEx(hmap, FILE_MAP_COPY, h, l, length, start);
32 if (!CloseHandle(hmap))
33 warning("unable to close file mapping handle\n");
35 return temp ? temp : MAP_FAILED;
38 int git_munmap(void *start, size_t length)
40 return !UnmapViewOfFile(start);