Merge branch 'jc/maint-github-actions-update'
[git.git] / compat / mmap.c
blob2fe1c7732eea9491e4bb26902f60430b6c0bb828
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) {
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;