Merge branch 'jk/perf-no-dups'
[git/git.git] / compat / win32mmap.c
CommitLineData
b130a72b
JL
1#include "../git-compat-util.h"
2
b130a72b
JL
3void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
4{
d5425d10 5 HANDLE osfhandle, hmap;
b130a72b 6 void *temp;
d5425d10 7 LARGE_INTEGER len;
b130a72b
JL
8 uint64_t o = offset;
9 uint32_t l = o & 0xFFFFFFFF;
10 uint32_t h = (o >> 32) & 0xFFFFFFFF;
11
d5425d10
JS
12 osfhandle = (HANDLE)_get_osfhandle(fd);
13 if (!GetFileSizeEx(osfhandle, &len))
b130a72b
JL
14 die("mmap: could not determine filesize");
15
d5425d10
JS
16 if ((length + offset) > len.QuadPart)
17 length = xsize_t(len.QuadPart - offset);
b130a72b
JL
18
19 if (!(flags & MAP_PRIVATE))
20 die("Invalid usage of mmap when built with USE_WIN32_MMAP");
21
d5425d10 22 hmap = CreateFileMapping(osfhandle, NULL,
7ce7ee2d 23 prot == PROT_READ ? PAGE_READONLY : PAGE_WRITECOPY, 0, 0, NULL);
b130a72b 24
6a730e10
JS
25 if (!hmap) {
26 errno = EINVAL;
b130a72b 27 return MAP_FAILED;
6a730e10 28 }
b130a72b 29
7ce7ee2d
JS
30 temp = MapViewOfFileEx(hmap, prot == PROT_READ ?
31 FILE_MAP_READ : FILE_MAP_COPY, h, l, length, start);
b130a72b
JL
32
33 if (!CloseHandle(hmap))
82247e9b 34 warning("unable to close file mapping handle");
b130a72b 35
6a730e10
JS
36 if (temp)
37 return temp;
38
39 errno = GetLastError() == ERROR_COMMITMENT_LIMIT ? EFBIG : EINVAL;
40 return MAP_FAILED;
b130a72b
JL
41}
42
43int git_munmap(void *start, size_t length)
44{
45 return !UnmapViewOfFile(start);
46}