mmap(win32): avoid copy-on-write when it is unnecessary
Often we are mmap()ing read-only. In those cases, it is wasteful to map in copy-on-write mode. Even worse: it can cause errors where we run out of space in the page file. So let's be extra careful to map files in read-only mode whenever possible. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Apr 22, 2016 at 16:31 UTC
7ce7ee2d8228a97f023c7e34488ed83a557d83fb
1 file changed
+3
-2
compat/win32mmap.c
+3
-2
@@ -22,14 +22,15 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of
22
die("Invalid usage of mmap when built with USE_WIN32_MMAP");
23
24
hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), NULL,
25
- PAGE_WRITECOPY, 0, 0, NULL);
25
+ prot == PROT_READ ? PAGE_READONLY : PAGE_WRITECOPY, 0, 0, NULL);
26
27
if (!hmap) {
28
errno = EINVAL;
29
return MAP_FAILED;
30
}
31
32
- temp = MapViewOfFileEx(hmap, FILE_MAP_COPY, h, l, length, start);
32
+ temp = MapViewOfFileEx(hmap, prot == PROT_READ ?
33
+ FILE_MAP_READ : FILE_MAP_COPY, h, l, length, start);
34
35
if (!CloseHandle(hmap))
36
warning("unable to close file mapping handle");