map.c 1.08 KB
Newer Older
Vicent Marti committed
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
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 <git2/common.h>

#ifndef GIT_WIN32
10 11 12 13 14

#include "map.h"
#include <sys/mman.h>
#include <errno.h>

Vicent Marti committed
15
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
16 17 18 19
{
	int mprot = 0;
	int mflag = 0;

20
	GIT_MMAP_VALIDATE(out, len, prot, flags);
21 22 23 24 25 26 27 28 29 30 31 32 33

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

	if (prot & GIT_PROT_WRITE)
		mprot = PROT_WRITE;
	else if (prot & GIT_PROT_READ)
		mprot = PROT_READ;

	if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
		mflag = MAP_SHARED;
	else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
		mflag = MAP_PRIVATE;
34 35
	else
		mflag = MAP_SHARED;
36

37
	out->data = mmap(NULL, len, mprot, mflag, fd, offset);
38

39 40 41
	if (!out->data || out->data == MAP_FAILED) {
		giterr_set(GITERR_OS, "Failed to mmap. Could not write data");
		return -1;
42 43 44
	}

	out->len = len;
45

46
	return 0;
47 48
}

Vicent Marti committed
49
int p_munmap(git_map *map)
50 51 52
{
	assert(map != NULL);
	munmap(map->data, map->len);
53

54
	return 0;
55 56
}

57
#endif
58