]> git.ipfire.org Git - thirdparty/git.git/blob - compat/win32mmap.c
Merge branch 'tr/maint-cherry-pick-list' into maint
[thirdparty/git.git] / compat / win32mmap.c
1 #include "../git-compat-util.h"
2
3 void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
4 {
5 HANDLE hmap;
6 void *temp;
7 size_t len;
8 struct stat st;
9 uint64_t o = offset;
10 uint32_t l = o & 0xFFFFFFFF;
11 uint32_t h = (o >> 32) & 0xFFFFFFFF;
12
13 if (!fstat(fd, &st))
14 len = xsize_t(st.st_size);
15 else
16 die("mmap: could not determine filesize");
17
18 if ((length + offset) > len)
19 length = len - offset;
20
21 if (!(flags & MAP_PRIVATE))
22 die("Invalid usage of mmap when built with USE_WIN32_MMAP");
23
24 hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), 0, PAGE_WRITECOPY,
25 0, 0, 0);
26
27 if (!hmap)
28 return MAP_FAILED;
29
30 temp = MapViewOfFileEx(hmap, FILE_MAP_COPY, h, l, length, start);
31
32 if (!CloseHandle(hmap))
33 warning("unable to close file mapping handle\n");
34
35 return temp ? temp : MAP_FAILED;
36 }
37
38 int git_munmap(void *start, size_t length)
39 {
40 return !UnmapViewOfFile(start);
41 }