Merge branch 'kk/maint-commit-tree'
[git/mjg.git] / compat / mmap.c
blobc9d46d174259f42a3e2a2eb073475aba517044be
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))
8 die("Invalid usage of mmap when built with NO_MMAP");
10 start = xmalloc(length);
11 if (start == NULL) {
12 errno = ENOMEM;
13 return MAP_FAILED;
16 while (n < length) {
17 ssize_t count = pread(fd, (char *)start + n, length - n, offset + n);
19 if (count == 0) {
20 memset((char *)start+n, 0, length-n);
21 break;
24 if (count < 0) {
25 if (errno == EAGAIN || errno == EINTR)
26 continue;
27 free(start);
28 errno = EACCES;
29 return MAP_FAILED;
32 n += count;
35 return start;
38 int git_munmap(void *start, size_t length)
40 free(start);
41 return 0;