Merge branch 'ar/submodule-add-config'
[git/debian.git] / compat / mmap.c
blob8d6c02d4bccc0160e69d3c0892946bfc79f10d00
1 #include "../git-compat-util.h"
3 void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
5 size_t n = 0;
7 if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)
8 die("Invalid usage of mmap when built with NO_MMAP");
10 if (length == 0) {
11 errno = EINVAL;
12 return MAP_FAILED;
15 start = malloc(length);
16 if (start == NULL) {
17 errno = ENOMEM;
18 return MAP_FAILED;
21 while (n < length) {
22 ssize_t count = xpread(fd, (char *)start + n, length - n, offset + n);
24 if (count == 0) {
25 memset((char *)start+n, 0, length-n);
26 break;
29 if (count < 0) {
30 free(start);
31 errno = EACCES;
32 return MAP_FAILED;
35 n += count;
38 return start;
41 int git_munmap(void *start, size_t length)
43 free(start);
44 return 0;