map.c 2.94 KB
Newer Older
Vicent Marti committed
1 2 3 4 5 6
/*
 * Copyright (C) 2009-2011 the libgit2 contributors
 *
 * 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 11 12 13 14 15 16 17 18 19 20 21 22 23 24

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


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;
}

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

	assert((out != NULL) && (len > 0));

	if ((out == NULL) || (len == 0)) {
		errno = EINVAL;
40
		return git__throw(GIT_ERROR, "Failed to mmap. No map or zero length");
41 42 43 44 45 46 47 48
	}

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

	if (fh == INVALID_HANDLE_VALUE) {
		errno = EBADF;
49
		return git__throw(GIT_ERROR, "Failed to mmap. Invalid handle value");
50 51 52 53 54 55 56 57
	}

	if (prot & GIT_PROT_WRITE)
		fmap_prot |= PAGE_READWRITE;
	else if (prot & GIT_PROT_READ)
		fmap_prot |= PAGE_READONLY;
	else {
		errno = EINVAL;
58
		return git__throw(GIT_ERROR, "Failed to mmap. Invalid protection parameters");
59 60 61 62 63 64 65 66 67
	}

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

	if (flags & GIT_MAP_FIXED) {
		errno = EINVAL;
68
		return git__throw(GIT_ERROR, "Failed to mmap. FIXED not set");
69 70 71 72 73
	}

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

Vicent Marti committed
74
	if (page_offset != 0) { /* offset must be multiple of page size */
75
		errno = EINVAL;
76
		return git__throw(GIT_ERROR, "Failed to mmap. Offset must be multiple of page size");
77 78 79 80 81 82
	}

	out->fmh = CreateFileMapping(fh, NULL, fmap_prot, 0, 0, NULL);
	if (!out->fmh || out->fmh == INVALID_HANDLE_VALUE) {
		/* errno = ? */
		out->fmh = NULL;
83
		return git__throw(GIT_ERROR, "Failed to mmap. Invalid handle value");
84 85
	}

86
	assert(sizeof(git_off_t) == 8);
87
	off_low = (DWORD)(page_start);
88
	off_hi = (DWORD)(page_start >> 32);
89 90 91 92 93
	out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len);
	if (!out->data) {
		/* errno = ? */
		CloseHandle(out->fmh);
		out->fmh = NULL;
94
		return git__throw(GIT_ERROR, "Failed to mmap. No data written");
95 96 97 98 99 100
	}
	out->len = len;

	return GIT_SUCCESS;
}

Vicent Marti committed
101
int p_munmap(git_map *map)
102 103 104 105
{
	assert(map != NULL);

	if (!map)
106
		return git__throw(GIT_ERROR, "Failed to munmap. Map does not exist");
107 108 109 110 111 112 113

	if (map->data) {
		if (!UnmapViewOfFile(map->data)) {
			/* errno = ? */
			CloseHandle(map->fmh);
			map->data = NULL;
			map->fmh = NULL;
114
			return git__throw(GIT_ERROR, "Failed to munmap. Could not unmap view of file");
115 116 117 118 119 120 121 122
		}
		map->data = NULL;
	}

	if (map->fmh) {
		if (!CloseHandle(map->fmh)) {
			/* errno = ? */
			map->fmh = NULL;
123
			return git__throw(GIT_ERROR, "Failed to munmap. Could not close handle");
124 125 126 127 128 129 130 131
		}
		map->fmh = NULL;
	}

	return GIT_SUCCESS;
}