map.c 2.49 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8 9 10

#include "map.h"
#include <errno.h>

11
#ifndef NO_MMAP
12 13 14 15 16 17 18 19 20 21 22 23 24 25

static DWORD get_page_size(void)
{
	static DWORD page_size;
	SYSTEM_INFO sys;

	if (!page_size) {
		GetSystemInfo(&sys);
		page_size = sys.dwAllocationGranularity;
	}

	return page_size;
}

26 27 28 29 30
long git__page_size(void)
{
	return (long)get_page_size();
}

Vicent Marti committed
31
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
32 33 34 35 36 37
{
	HANDLE fh = (HANDLE)_get_osfhandle(fd);
	DWORD page_size = get_page_size();
	DWORD fmap_prot = 0;
	DWORD view_prot = 0;
	DWORD off_low = 0;
Vicent Marti committed
38
	DWORD off_hi = 0;
39 40
	git_off_t page_start;
	git_off_t page_offset;
41

42
	GIT_MMAP_VALIDATE(out, len, prot, flags);
43 44 45 46 47 48 49

	out->data = NULL;
	out->len = 0;
	out->fmh = NULL;

	if (fh == INVALID_HANDLE_VALUE) {
		errno = EBADF;
50 51
		giterr_set(GITERR_OS, "Failed to mmap. Invalid handle value");
		return -1;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	}

	if (prot & GIT_PROT_WRITE)
		fmap_prot |= PAGE_READWRITE;
	else if (prot & GIT_PROT_READ)
		fmap_prot |= PAGE_READONLY;

	if (prot & GIT_PROT_WRITE)
		view_prot |= FILE_MAP_WRITE;
	if (prot & GIT_PROT_READ)
		view_prot |= FILE_MAP_READ;

	page_start = (offset / page_size) * page_size;
	page_offset = offset - page_start;

Vicent Marti committed
67
	if (page_offset != 0) { /* offset must be multiple of page size */
68
		errno = EINVAL;
69 70
		giterr_set(GITERR_OS, "Failed to mmap. Offset must be multiple of page size");
		return -1;
71 72 73 74
	}

	out->fmh = CreateFileMapping(fh, NULL, fmap_prot, 0, 0, NULL);
	if (!out->fmh || out->fmh == INVALID_HANDLE_VALUE) {
75
		giterr_set(GITERR_OS, "Failed to mmap. Invalid handle value");
76
		out->fmh = NULL;
77
		return -1;
78 79
	}

80
	assert(sizeof(git_off_t) == 8);
81

82
	off_low = (DWORD)(page_start);
83
	off_hi = (DWORD)(page_start >> 32);
84 85
	out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len);
	if (!out->data) {
86
		giterr_set(GITERR_OS, "Failed to mmap. No data written");
87 88
		CloseHandle(out->fmh);
		out->fmh = NULL;
89
		return -1;
90 91 92
	}
	out->len = len;

93
	return 0;
94 95
}

Vicent Marti committed
96
int p_munmap(git_map *map)
97
{
98
	int error = 0;
99

100
	assert(map != NULL);
101 102 103

	if (map->data) {
		if (!UnmapViewOfFile(map->data)) {
104 105
			giterr_set(GITERR_OS, "Failed to munmap. Could not unmap view of file");
			error = -1;
106 107 108 109 110 111
		}
		map->data = NULL;
	}

	if (map->fmh) {
		if (!CloseHandle(map->fmh)) {
112 113
			giterr_set(GITERR_OS, "Failed to munmap. Could not close handle");
			error = -1;
114 115 116 117
		}
		map->fmh = NULL;
	}

118
	return error;
119 120
}

121
#endif