map.c 1.33 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
#include <git2/common.h>

9
#if !defined(GIT_WIN32) && !defined(NO_MMAP)
10 11 12

#include "map.h"
#include <sys/mman.h>
13
#include <unistd.h>
14 15
#include <errno.h>

16
int git__page_size(size_t *page_size)
17
{
18 19 20 21 22 23 24
	long sc_page_size = sysconf(_SC_PAGE_SIZE);
	if (sc_page_size < 0) {
		giterr_set_str(GITERR_OS, "Can't determine system page size");
		return -1;
	}
	*page_size = (size_t) sc_page_size;
	return 0;
25 26
}

Vicent Marti committed
27
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
28
{
29
	int mprot = PROT_READ;
30 31
	int mflag = 0;

32
	GIT_MMAP_VALIDATE(out, len, prot, flags);
33 34 35 36 37

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

	if (prot & GIT_PROT_WRITE)
38
		mprot |= PROT_WRITE;
39 40 41 42 43

	if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
		mflag = MAP_SHARED;
	else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
		mflag = MAP_PRIVATE;
44 45
	else
		mflag = MAP_SHARED;
46

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

49 50 51
	if (!out->data || out->data == MAP_FAILED) {
		giterr_set(GITERR_OS, "Failed to mmap. Could not write data");
		return -1;
52 53 54
	}

	out->len = len;
55

56
	return 0;
57 58
}

Vicent Marti committed
59
int p_munmap(git_map *map)
60 61 62
{
	assert(map != NULL);
	munmap(map->data, map->len);
63

64
	return 0;
65 66
}

67
#endif
68