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