map.c 2.87 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
#include "common.h"

10 11 12
#include "map.h"
#include <errno.h>

13
#ifndef NO_MMAP
14 15 16 17 18 19 20 21

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

	if (!page_size) {
		GetSystemInfo(&sys);
22
		page_size = sys.dwPageSize;
23 24 25 26 27
	}

	return page_size;
}

28 29 30 31 32 33 34 35 36 37 38 39 40
static DWORD get_allocation_granularity(void)
{
	static DWORD granularity;
	SYSTEM_INFO sys;

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

	return granularity;
}

41
int git__page_size(size_t *page_size)
42
{
43 44
	*page_size = get_page_size();
	return 0;
45 46
}

47 48 49 50 51 52
int git__mmap_alignment(size_t *page_size)
{
	*page_size = get_allocation_granularity();
	return 0;
}

Vicent Marti committed
53
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
54 55
{
	HANDLE fh = (HANDLE)_get_osfhandle(fd);
56
	DWORD alignment = get_allocation_granularity();
57 58 59
	DWORD fmap_prot = 0;
	DWORD view_prot = 0;
	DWORD off_low = 0;
Vicent Marti committed
60
	DWORD off_hi = 0;
61 62
	git_off_t page_start;
	git_off_t page_offset;
63

64
	GIT_MMAP_VALIDATE(out, len, prot, flags);
65 66 67 68 69 70 71

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

	if (fh == INVALID_HANDLE_VALUE) {
		errno = EBADF;
72
		giterr_set(GITERR_OS, "failed to mmap. Invalid handle value");
73
		return -1;
74 75 76 77 78 79 80 81 82 83 84 85
	}

	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;

86
	page_start = (offset / alignment) * alignment;
87 88
	page_offset = offset - page_start;

89
	if (page_offset != 0) { /* offset must be multiple of the allocation granularity */
90
		errno = EINVAL;
91
		giterr_set(GITERR_OS, "failed to mmap. Offset must be multiple of allocation granularity");
92
		return -1;
93 94 95 96
	}

	out->fmh = CreateFileMapping(fh, NULL, fmap_prot, 0, 0, NULL);
	if (!out->fmh || out->fmh == INVALID_HANDLE_VALUE) {
97
		giterr_set(GITERR_OS, "failed to mmap. Invalid handle value");
98
		out->fmh = NULL;
99
		return -1;
100 101
	}

102
	assert(sizeof(git_off_t) == 8);
103

104
	off_low = (DWORD)(page_start);
105
	off_hi = (DWORD)(page_start >> 32);
106 107
	out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len);
	if (!out->data) {
108
		giterr_set(GITERR_OS, "failed to mmap. No data written");
109 110
		CloseHandle(out->fmh);
		out->fmh = NULL;
111
		return -1;
112 113 114
	}
	out->len = len;

115
	return 0;
116 117
}

Vicent Marti committed
118
int p_munmap(git_map *map)
119
{
120
	int error = 0;
121

122
	assert(map != NULL);
123 124 125

	if (map->data) {
		if (!UnmapViewOfFile(map->data)) {
126
			giterr_set(GITERR_OS, "failed to munmap. Could not unmap view of file");
127
			error = -1;
128 129 130 131 132 133
		}
		map->data = NULL;
	}

	if (map->fmh) {
		if (!CloseHandle(map->fmh)) {
134
			giterr_set(GITERR_OS, "failed to munmap. Could not close handle");
135
			error = -1;
136 137 138 139
		}
		map->fmh = NULL;
	}

140
	return error;
141 142
}

143
#endif