Skip to content

Commit 8e55442

Browse files
spearceJunio C Hamano
authored andcommitted
Switch git_mmap to use pread.
Now that Git depends on pread in index-pack its safe to say we can also depend on it within the git_mmap emulation we activate when NO_MMAP is set. On most systems pread should be slightly faster than an lseek/read/lseek sequence as its one system call vs. three system calls. We also now honor EAGAIN and EINTR error codes from pread and restart the prior read. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent d677912 commit 8e55442

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

compat/mmap.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,28 @@
22

33
void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
44
{
5-
int n = 0;
6-
off_t current_offset = lseek(fd, 0, SEEK_CUR);
5+
size_t n = 0;
76

87
if (start != NULL || !(flags & MAP_PRIVATE))
98
die("Invalid usage of mmap when built with NO_MMAP");
109

11-
if (lseek(fd, offset, SEEK_SET) < 0) {
12-
errno = EINVAL;
13-
return MAP_FAILED;
14-
}
15-
1610
start = xmalloc(length);
1711
if (start == NULL) {
1812
errno = ENOMEM;
1913
return MAP_FAILED;
2014
}
2115

2216
while (n < length) {
23-
int count = read(fd, start+n, length-n);
17+
ssize_t count = pread(fd, (char *)start + n, length - n, offset + n);
2418

2519
if (count == 0) {
26-
memset(start+n, 0, length-n);
20+
memset((char *)start+n, 0, length-n);
2721
break;
2822
}
2923

3024
if (count < 0) {
25+
if (errno == EAGAIN || errno == EINTR)
26+
continue;
3127
free(start);
3228
errno = EACCES;
3329
return MAP_FAILED;
@@ -36,11 +32,6 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of
3632
n += count;
3733
}
3834

39-
if (current_offset != lseek(fd, current_offset, SEEK_SET)) {
40-
errno = EINVAL;
41-
return MAP_FAILED;
42-
}
43-
4435
return start;
4536
}
4637

0 commit comments

Comments
 (0)