Fix a bunch of pointer declarations (codestyle)
[git/git.git] / compat / mmap.c
CommitLineData
4050c0df 1#include "../git-compat-util.h"
730d48a2 2
d6779124 3void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
730d48a2 4{
8e554429 5 size_t n = 0;
730d48a2 6
f48000fc 7 if (start != NULL || !(flags & MAP_PRIVATE))
d6779124 8 die("Invalid usage of mmap when built with NO_MMAP");
730d48a2 9
730d48a2 10 start = xmalloc(length);
f48000fc 11 if (start == NULL) {
730d48a2
JS
12 errno = ENOMEM;
13 return MAP_FAILED;
14 }
15
f48000fc 16 while (n < length) {
8e554429 17 ssize_t count = pread(fd, (char *)start + n, length - n, offset + n);
730d48a2 18
f48000fc 19 if (count == 0) {
8e554429 20 memset((char *)start+n, 0, length-n);
730d48a2
JS
21 break;
22 }
23
f48000fc 24 if (count < 0) {
8e554429
SP
25 if (errno == EAGAIN || errno == EINTR)
26 continue;
730d48a2
JS
27 free(start);
28 errno = EACCES;
29 return MAP_FAILED;
30 }
31
32 n += count;
33 }
34
730d48a2
JS
35 return start;
36}
37
d6779124 38int git_munmap(void *start, size_t length)
730d48a2 39{
730d48a2 40 free(start);
730d48a2
JS
41 return 0;
42}