Merge branch 'jc/ident'
[git/jrn.git] / compat / win32mmap.c
blob1c5a14922f255af2c3b0e75e06925b748d3d7684
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 hmap;
6 void *temp;
7 size_t len;
8 struct stat st;
9 uint64_t o = offset;
10 uint32_t l = o & 0xFFFFFFFF;
11 uint32_t h = (o >> 32) & 0xFFFFFFFF;
13 if (!fstat(fd, &st))
14 len = xsize_t(st.st_size);
15 else
16 die("mmap: could not determine filesize");
18 if ((length + offset) > len)
19 length = len - offset;
21 if (!(flags & MAP_PRIVATE))
22 die("Invalid usage of mmap when built with USE_WIN32_MMAP");
24 hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), 0, PAGE_WRITECOPY,
25 0, 0, 0);
27 if (!hmap)
28 return MAP_FAILED;
30 temp = MapViewOfFileEx(hmap, FILE_MAP_COPY, h, l, length, start);
32 if (!CloseHandle(hmap))
33 warning("unable to close file mapping handle\n");
35 return temp ? temp : MAP_FAILED;
38 int git_munmap(void *start, size_t length)
40 return !UnmapViewOfFile(start);