]> git.ipfire.org Git - thirdparty/git.git/blob - compat/win32mmap.c
Merge branch 'jc/maint-am-keep' into maint
[thirdparty/git.git] / compat / win32mmap.c
1 #include "../git-compat-util.h"
2
3 /*
4 * Note that this doesn't return the actual pagesize, but
5 * the allocation granularity. If future Windows specific git code
6 * needs the real getpagesize function, we need to find another solution.
7 */
8 int mingw_getpagesize(void)
9 {
10 SYSTEM_INFO si;
11 GetSystemInfo(&si);
12 return si.dwAllocationGranularity;
13 }
14
15 void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
16 {
17 HANDLE hmap;
18 void *temp;
19 size_t len;
20 struct stat st;
21 uint64_t o = offset;
22 uint32_t l = o & 0xFFFFFFFF;
23 uint32_t h = (o >> 32) & 0xFFFFFFFF;
24
25 if (!fstat(fd, &st))
26 len = xsize_t(st.st_size);
27 else
28 die("mmap: could not determine filesize");
29
30 if ((length + offset) > len)
31 length = len - offset;
32
33 if (!(flags & MAP_PRIVATE))
34 die("Invalid usage of mmap when built with USE_WIN32_MMAP");
35
36 hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), 0, PAGE_WRITECOPY,
37 0, 0, 0);
38
39 if (!hmap)
40 return MAP_FAILED;
41
42 temp = MapViewOfFileEx(hmap, FILE_MAP_COPY, h, l, length, start);
43
44 if (!CloseHandle(hmap))
45 warning("unable to close file mapping handle\n");
46
47 return temp ? temp : MAP_FAILED;
48 }
49
50 int git_munmap(void *start, size_t length)
51 {
52 return !UnmapViewOfFile(start);
53 }