2005-12-05 22:54:29 +03:00
|
|
|
#include "../git-compat-util.h"
|
2005-10-09 02:54:36 +04:00
|
|
|
|
2006-12-24 08:45:37 +03:00
|
|
|
void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
|
2005-10-09 02:54:36 +04:00
|
|
|
{
|
2006-12-24 08:45:47 +03:00
|
|
|
size_t n = 0;
|
2005-10-09 02:54:36 +04:00
|
|
|
|
2005-10-09 02:54:36 +04:00
|
|
|
if (start != NULL || !(flags & MAP_PRIVATE))
|
2006-12-24 08:45:37 +03:00
|
|
|
die("Invalid usage of mmap when built with NO_MMAP");
|
2005-10-09 02:54:36 +04:00
|
|
|
|
|
|
|
start = xmalloc(length);
|
2005-10-09 02:54:36 +04:00
|
|
|
if (start == NULL) {
|
2005-10-09 02:54:36 +04:00
|
|
|
errno = ENOMEM;
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
2005-10-09 02:54:36 +04:00
|
|
|
while (n < length) {
|
2006-12-24 08:45:47 +03:00
|
|
|
ssize_t count = pread(fd, (char *)start + n, length - n, offset + n);
|
2005-10-09 02:54:36 +04:00
|
|
|
|
2005-10-09 02:54:36 +04:00
|
|
|
if (count == 0) {
|
2006-12-24 08:45:47 +03:00
|
|
|
memset((char *)start+n, 0, length-n);
|
2005-10-09 02:54:36 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-10-09 02:54:36 +04:00
|
|
|
if (count < 0) {
|
2006-12-24 08:45:47 +03:00
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
2005-10-09 02:54:36 +04:00
|
|
|
free(start);
|
|
|
|
errno = EACCES;
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2006-12-24 08:45:37 +03:00
|
|
|
int git_munmap(void *start, size_t length)
|
2005-10-09 02:54:36 +04:00
|
|
|
{
|
|
|
|
free(start);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|