Skip to content

Commit 1fda624

Browse files
committed
mmap(win32): avoid expensive fstat() call
On Windows, we have to emulate the fstat() call to fill out information that takes extra effort to obtain, such as the file permissions/type. If all we want is the file size, we can use the much cheaper GetFileSizeEx() function (available since Windows XP). Suggested by Philip Kelley. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 1789c15 commit 1fda624

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

compat/win32mmap.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@
22

33
void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
44
{
5-
HANDLE hmap;
5+
HANDLE osfhandle, hmap;
66
void *temp;
7-
off_t len;
8-
struct stat st;
7+
LARGE_INTEGER len;
98
uint64_t o = offset;
109
uint32_t l = o & 0xFFFFFFFF;
1110
uint32_t h = (o >> 32) & 0xFFFFFFFF;
1211

13-
if (!fstat(fd, &st))
14-
len = st.st_size;
15-
else
12+
osfhandle = (HANDLE)_get_osfhandle(fd);
13+
if (!GetFileSizeEx(osfhandle, &len))
1614
die("mmap: could not determine filesize");
1715

18-
if ((length + offset) > len)
19-
length = xsize_t(len - offset);
16+
if ((length + offset) > len.QuadPart)
17+
length = xsize_t(len.QuadPart - offset);
2018

2119
if (!(flags & MAP_PRIVATE))
2220
die("Invalid usage of mmap when built with USE_WIN32_MMAP");
2321

24-
hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), NULL,
22+
hmap = CreateFileMapping(osfhandle, NULL,
2523
prot == PROT_READ ? PAGE_READONLY : PAGE_WRITECOPY, 0, 0, NULL);
2624

2725
if (!hmap) {

0 commit comments

Comments
 (0)