Blame src/win32/map.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "map.h"
Packit Service 20376f
#include <errno.h>
Packit Service 20376f
Packit Service 20376f
#ifndef NO_MMAP
Packit Service 20376f
Packit Service 20376f
static DWORD get_page_size(void)
Packit Service 20376f
{
Packit Service 20376f
	static DWORD page_size;
Packit Service 20376f
	SYSTEM_INFO sys;
Packit Service 20376f
Packit Service 20376f
	if (!page_size) {
Packit Service 20376f
		GetSystemInfo(&sys);
Packit Service 20376f
		page_size = sys.dwPageSize;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return page_size;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static DWORD get_allocation_granularity(void)
Packit Service 20376f
{
Packit Service 20376f
	static DWORD granularity;
Packit Service 20376f
	SYSTEM_INFO sys;
Packit Service 20376f
Packit Service 20376f
	if (!granularity) {
Packit Service 20376f
		GetSystemInfo(&sys);
Packit Service 20376f
		granularity = sys.dwAllocationGranularity;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return granularity;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git__page_size(size_t *page_size)
Packit Service 20376f
{
Packit Service 20376f
	*page_size = get_page_size();
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git__mmap_alignment(size_t *page_size)
Packit Service 20376f
{
Packit Service 20376f
	*page_size = get_allocation_granularity();
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
Packit Service 20376f
{
Packit Service 20376f
	HANDLE fh = (HANDLE)_get_osfhandle(fd);
Packit Service 20376f
	DWORD alignment = get_allocation_granularity();
Packit Service 20376f
	DWORD fmap_prot = 0;
Packit Service 20376f
	DWORD view_prot = 0;
Packit Service 20376f
	DWORD off_low = 0;
Packit Service 20376f
	DWORD off_hi = 0;
Packit Service 20376f
	git_off_t page_start;
Packit Service 20376f
	git_off_t page_offset;
Packit Service 20376f
Packit Service 20376f
	GIT_MMAP_VALIDATE(out, len, prot, flags);
Packit Service 20376f
Packit Service 20376f
	out->data = NULL;
Packit Service 20376f
	out->len = 0;
Packit Service 20376f
	out->fmh = NULL;
Packit Service 20376f
Packit Service 20376f
	if (fh == INVALID_HANDLE_VALUE) {
Packit Service 20376f
		errno = EBADF;
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to mmap. Invalid handle value");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (prot & GIT_PROT_WRITE)
Packit Service 20376f
		fmap_prot |= PAGE_READWRITE;
Packit Service 20376f
	else if (prot & GIT_PROT_READ)
Packit Service 20376f
		fmap_prot |= PAGE_READONLY;
Packit Service 20376f
Packit Service 20376f
	if (prot & GIT_PROT_WRITE)
Packit Service 20376f
		view_prot |= FILE_MAP_WRITE;
Packit Service 20376f
	if (prot & GIT_PROT_READ)
Packit Service 20376f
		view_prot |= FILE_MAP_READ;
Packit Service 20376f
Packit Service 20376f
	page_start = (offset / alignment) * alignment;
Packit Service 20376f
	page_offset = offset - page_start;
Packit Service 20376f
Packit Service 20376f
	if (page_offset != 0) { /* offset must be multiple of the allocation granularity */
Packit Service 20376f
		errno = EINVAL;
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to mmap. Offset must be multiple of allocation granularity");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	out->fmh = CreateFileMapping(fh, NULL, fmap_prot, 0, 0, NULL);
Packit Service 20376f
	if (!out->fmh || out->fmh == INVALID_HANDLE_VALUE) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to mmap. Invalid handle value");
Packit Service 20376f
		out->fmh = NULL;
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	assert(sizeof(git_off_t) == 8);
Packit Service 20376f
Packit Service 20376f
	off_low = (DWORD)(page_start);
Packit Service 20376f
	off_hi = (DWORD)(page_start >> 32);
Packit Service 20376f
	out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len);
Packit Service 20376f
	if (!out->data) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to mmap. No data written");
Packit Service 20376f
		CloseHandle(out->fmh);
Packit Service 20376f
		out->fmh = NULL;
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
	out->len = len;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int p_munmap(git_map *map)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	assert(map != NULL);
Packit Service 20376f
Packit Service 20376f
	if (map->data) {
Packit Service 20376f
		if (!UnmapViewOfFile(map->data)) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to munmap. Could not unmap view of file");
Packit Service 20376f
			error = -1;
Packit Service 20376f
		}
Packit Service 20376f
		map->data = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (map->fmh) {
Packit Service 20376f
		if (!CloseHandle(map->fmh)) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to munmap. Could not close handle");
Packit Service 20376f
			error = -1;
Packit Service 20376f
		}
Packit Service 20376f
		map->fmh = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#endif