blob: 14d31010dfe57e520c6864df4f84ea732018785f [file] [log] [blame]
Junio C Hamano4050c0d2005-12-05 11:54:29 -08001#include "../git-compat-util.h"
Johannes Schindelin730d48a2005-10-08 15:54:36 -07002
Shawn O. Pearced6779122006-12-24 00:45:37 -05003void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
Johannes Schindelin730d48a2005-10-08 15:54:36 -07004{
Shawn O. Pearce8e554422006-12-24 00:45:47 -05005 size_t n = 0;
Johannes Schindelin730d48a2005-10-08 15:54:36 -07006
Carlo Marcelo Arenas Belónbf1e6da2018-10-23 05:35:19 -07007 if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)
Shawn O. Pearced6779122006-12-24 00:45:37 -05008 die("Invalid usage of mmap when built with NO_MMAP");
Johannes Schindelin730d48a2005-10-08 15:54:36 -07009
Johannes Schindelin730d48a2005-10-08 15:54:36 -070010 start = xmalloc(length);
Junio C Hamanof48000f2005-10-08 15:54:36 -070011 if (start == NULL) {
Johannes Schindelin730d48a2005-10-08 15:54:36 -070012 errno = ENOMEM;
13 return MAP_FAILED;
14 }
15
Junio C Hamanof48000f2005-10-08 15:54:36 -070016 while (n < length) {
Yiannis Marangos9aa91af2014-04-10 21:54:12 +030017 ssize_t count = xpread(fd, (char *)start + n, length - n, offset + n);
Johannes Schindelin730d48a2005-10-08 15:54:36 -070018
Junio C Hamanof48000f2005-10-08 15:54:36 -070019 if (count == 0) {
Shawn O. Pearce8e554422006-12-24 00:45:47 -050020 memset((char *)start+n, 0, length-n);
Johannes Schindelin730d48a2005-10-08 15:54:36 -070021 break;
22 }
23
Junio C Hamanof48000f2005-10-08 15:54:36 -070024 if (count < 0) {
Johannes Schindelin730d48a2005-10-08 15:54:36 -070025 free(start);
26 errno = EACCES;
27 return MAP_FAILED;
28 }
29
30 n += count;
31 }
32
Johannes Schindelin730d48a2005-10-08 15:54:36 -070033 return start;
34}
35
Shawn O. Pearced6779122006-12-24 00:45:37 -050036int git_munmap(void *start, size_t length)
Johannes Schindelin730d48a2005-10-08 15:54:36 -070037{
Johannes Schindelin730d48a2005-10-08 15:54:36 -070038 free(start);
Johannes Schindelin730d48a2005-10-08 15:54:36 -070039 return 0;
40}