compat / win32mmap.con commit Merge branch 'js/windows-dotgit' (bfc99b6)
   1#include "../git-compat-util.h"
   2
   3void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
   4{
   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;
  11
  12        osfhandle = (HANDLE)_get_osfhandle(fd);
  13        if (!GetFileSizeEx(osfhandle, &len))
  14                die("mmap: could not determine filesize");
  15
  16        if ((length + offset) > len.QuadPart)
  17                length = xsize_t(len.QuadPart - offset);
  18
  19        if (!(flags & MAP_PRIVATE))
  20                die("Invalid usage of mmap when built with USE_WIN32_MMAP");
  21
  22        hmap = CreateFileMapping(osfhandle, NULL,
  23                prot == PROT_READ ? PAGE_READONLY : PAGE_WRITECOPY, 0, 0, NULL);
  24
  25        if (!hmap) {
  26                errno = EINVAL;
  27                return MAP_FAILED;
  28        }
  29
  30        temp = MapViewOfFileEx(hmap, prot == PROT_READ ?
  31                        FILE_MAP_READ : FILE_MAP_COPY, h, l, length, start);
  32
  33        if (!CloseHandle(hmap))
  34                warning("unable to close file mapping handle");
  35
  36        if (temp)
  37                return temp;
  38
  39        errno = GetLastError() == ERROR_COMMITMENT_LIMIT ? EFBIG : EINVAL;
  40        return MAP_FAILED;
  41}
  42
  43int git_munmap(void *start, size_t length)
  44{
  45        return !UnmapViewOfFile(start);
  46}