map.c 2.51 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
int git__page_size(size_t *page_size)
27
{
28 29
	*page_size = get_page_size();
	return 0;
30 31
}

Vicent Marti committed
32
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
33 34 35 36 37 38
{
	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
39
	DWORD off_hi = 0;
40 41
	git_off_t page_start;
	git_off_t page_offset;
42

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

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

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

	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
68
	if (page_offset != 0) { /* offset must be multiple of page size */
69
		errno = EINVAL;
70 71
		giterr_set(GITERR_OS, "Failed to mmap. Offset must be multiple of page size");
		return -1;
72 73 74 75
	}

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

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

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

94
	return 0;
95 96
}

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

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

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

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

119
	return error;
120 121
}

122
#endif