Skip to content

Commit d18c843

Browse files
committed
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 <[email protected]>
1 parent f10f2c5 commit d18c843

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

compat/win32mmap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of
2222
die("Invalid usage of mmap when built with USE_WIN32_MMAP");
2323

2424
hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), NULL,
25-
PAGE_WRITECOPY, 0, 0, NULL);
25+
prot == PROT_READ ? PAGE_READONLY : PAGE_WRITECOPY, 0, 0, NULL);
2626

2727
if (!hmap) {
2828
errno = EINVAL;
2929
return MAP_FAILED;
3030
}
3131

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);
3334

3435
if (!CloseHandle(hmap))
3536
warning("unable to close file mapping handle");

0 commit comments

Comments
 (0)