2009-03-13 16:50:45 +01:00
|
|
|
#include "../git-compat-util.h"
|
|
|
|
|
|
|
|
void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
|
|
|
|
{
|
2016-04-22 16:31:32 +02:00
|
|
|
HANDLE osfhandle, hmap;
|
2009-03-13 16:50:45 +01:00
|
|
|
void *temp;
|
2016-04-22 16:31:32 +02:00
|
|
|
LARGE_INTEGER len;
|
2009-03-13 16:50:45 +01:00
|
|
|
uint64_t o = offset;
|
|
|
|
uint32_t l = o & 0xFFFFFFFF;
|
|
|
|
uint32_t h = (o >> 32) & 0xFFFFFFFF;
|
|
|
|
|
2016-04-22 16:31:32 +02:00
|
|
|
osfhandle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
if (!GetFileSizeEx(osfhandle, &len))
|
2009-03-13 16:50:45 +01:00
|
|
|
die("mmap: could not determine filesize");
|
|
|
|
|
2016-04-22 16:31:32 +02:00
|
|
|
if ((length + offset) > len.QuadPart)
|
|
|
|
length = xsize_t(len.QuadPart - offset);
|
2009-03-13 16:50:45 +01:00
|
|
|
|
|
|
|
if (!(flags & MAP_PRIVATE))
|
|
|
|
die("Invalid usage of mmap when built with USE_WIN32_MMAP");
|
|
|
|
|
2016-04-22 16:31:32 +02:00
|
|
|
hmap = CreateFileMapping(osfhandle, NULL,
|
2016-04-22 16:31:26 +02:00
|
|
|
prot == PROT_READ ? PAGE_READONLY : PAGE_WRITECOPY, 0, 0, NULL);
|
2009-03-13 16:50:45 +01:00
|
|
|
|
2016-04-22 16:31:22 +02:00
|
|
|
if (!hmap) {
|
|
|
|
errno = EINVAL;
|
2009-03-13 16:50:45 +01:00
|
|
|
return MAP_FAILED;
|
2016-04-22 16:31:22 +02:00
|
|
|
}
|
2009-03-13 16:50:45 +01:00
|
|
|
|
2016-04-22 16:31:26 +02:00
|
|
|
temp = MapViewOfFileEx(hmap, prot == PROT_READ ?
|
|
|
|
FILE_MAP_READ : FILE_MAP_COPY, h, l, length, start);
|
2009-03-13 16:50:45 +01:00
|
|
|
|
|
|
|
if (!CloseHandle(hmap))
|
2012-04-30 02:28:45 +02:00
|
|
|
warning("unable to close file mapping handle");
|
2009-03-13 16:50:45 +01:00
|
|
|
|
2016-04-22 16:31:22 +02:00
|
|
|
if (temp)
|
|
|
|
return temp;
|
|
|
|
|
|
|
|
errno = GetLastError() == ERROR_COMMITMENT_LIMIT ? EFBIG : EINVAL;
|
|
|
|
return MAP_FAILED;
|
2009-03-13 16:50:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int git_munmap(void *start, size_t length)
|
|
|
|
{
|
|
|
|
return !UnmapViewOfFile(start);
|
|
|
|
}
|