Merge branch 'jk/http-leakfixes' into ps/leakfixes-part-8
[git/gitster.git] / compat / win32mmap.c
bloba4ab4cb939399ba1d7becc45fcda72902741f22d
1 #include "../git-compat-util.h"
3 void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
5 HANDLE osfhandle, hmap;
6 void *temp;
7 LARGE_INTEGER len;
8 uint64_t o = offset;
9 uint32_t l = o & 0xFFFFFFFF;
10 uint32_t h = (o >> 32) & 0xFFFFFFFF;
12 osfhandle = (HANDLE)_get_osfhandle(fd);
13 if (!GetFileSizeEx(osfhandle, &len))
14 die("mmap: could not determine filesize");
16 if ((length + offset) > len.QuadPart)
17 length = xsize_t(len.QuadPart - offset);
19 if (!(flags & MAP_PRIVATE))
20 die("Invalid usage of mmap when built with USE_WIN32_MMAP");
22 hmap = CreateFileMapping(osfhandle, NULL,
23 prot == PROT_READ ? PAGE_READONLY : PAGE_WRITECOPY, 0, 0, NULL);
25 if (!hmap) {
26 errno = EINVAL;
27 return MAP_FAILED;
30 temp = MapViewOfFileEx(hmap, prot == PROT_READ ?
31 FILE_MAP_READ : FILE_MAP_COPY, h, l, length, start);
33 if (!CloseHandle(hmap))
34 warning("unable to close file mapping handle");
36 if (temp)
37 return temp;
39 errno = GetLastError() == ERROR_COMMITMENT_LIMIT ? EFBIG : EINVAL;
40 return MAP_FAILED;
43 int git_munmap(void *start, size_t length UNUSED)
45 return !UnmapViewOfFile(start);