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>
Shawn O. Pearce committed
Dec 24, 2006 at 00:45 UTC
8e554429e8a9dbb5dba6b8aeca66fc27c0bb5538
1 file changed
+5
-14
compat/mmap.c
+5
-14
@@ -2,17 +2,11 @@
2
3
void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
4
{
5
- int n = 0;
6
- off_t current_offset = lseek(fd, 0, SEEK_CUR);
5
+ size_t n = 0;
6
7
if (start != NULL || !(flags & MAP_PRIVATE))
8
die("Invalid usage of mmap when built with NO_MMAP");
9
11
- if (lseek(fd, offset, SEEK_SET) < 0) {
12
- errno = EINVAL;
13
- return MAP_FAILED;
14
- }
15
-
10
start = xmalloc(length);
11
if (start == NULL) {
12
errno = ENOMEM;
@@ -20,14 +14,16 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of
14
}
15
16
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);
18
19
if (count == 0) {
26
- memset(start+n, 0, length-n);
20
+ memset((char *)start+n, 0, length-n);
21
break;
22
}
23
24
if (count < 0) {
25
+ if (errno == EAGAIN || errno == EINTR)
26
+ continue;
27
free(start);
28
errno = EACCES;
29
return MAP_FAILED;
@@ -36,11 +32,6 @@ void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t of
32
n += count;
33
}
34
39
- if (current_offset != lseek(fd, current_offset, SEEK_SET)) {
40
- errno = EINVAL;
41
- return MAP_FAILED;
42
- }
43
-
35
return start;
36
}
37