Merge branch 'jk/perf-no-dups'
[git/git.git] / compat / mmap.c
1 #include "../git-compat-util.h"
2
3 void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
4 {
5 size_t n = 0;
6
7 if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)
8 die("Invalid usage of mmap when built with NO_MMAP");
9
10 start = xmalloc(length);
11 if (start == NULL) {
12 errno = ENOMEM;
13 return MAP_FAILED;
14 }
15
16 while (n < length) {
17 ssize_t count = xpread(fd, (char *)start + n, length - n, offset + n);
18
19 if (count == 0) {
20 memset((char *)start+n, 0, length-n);
21 break;
22 }
23
24 if (count < 0) {
25 free(start);
26 errno = EACCES;
27 return MAP_FAILED;
28 }
29
30 n += count;
31 }
32
33 return start;
34 }
35
36 int git_munmap(void *start, size_t length)
37 {
38 free(start);
39 return 0;
40 }