map.c 1.43 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 10

#include "common.h"

#include "git2/common.h"
11

12
#if !defined(GIT_WIN32) && !defined(NO_MMAP)
13 14 15

#include "map.h"
#include <sys/mman.h>
16
#include <unistd.h>
17 18
#include <errno.h>

19
int git__page_size(size_t *page_size)
20
{
21 22
	long sc_page_size = sysconf(_SC_PAGE_SIZE);
	if (sc_page_size < 0) {
23
		giterr_set(GITERR_OS, "can't determine system page size");
24 25 26 27
		return -1;
	}
	*page_size = (size_t) sc_page_size;
	return 0;
28 29
}

30 31 32 33 34
int git__mmap_alignment(size_t *alignment)
{
  return git__page_size(alignment);
}

Vicent Marti committed
35
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
36
{
37
	int mprot = PROT_READ;
38 39
	int mflag = 0;

40
	GIT_MMAP_VALIDATE(out, len, prot, flags);
41 42 43 44 45

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

	if (prot & GIT_PROT_WRITE)
46
		mprot |= PROT_WRITE;
47 48 49 50 51

	if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
		mflag = MAP_SHARED;
	else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
		mflag = MAP_PRIVATE;
52 53
	else
		mflag = MAP_SHARED;
54

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

57
	if (!out->data || out->data == MAP_FAILED) {
58
		giterr_set(GITERR_OS, "failed to mmap. Could not write data");
59
		return -1;
60 61 62
	}

	out->len = len;
63

64
	return 0;
65 66
}

Vicent Marti committed
67
int p_munmap(git_map *map)
68 69 70
{
	assert(map != NULL);
	munmap(map->data, map->len);
71

72
	return 0;
73 74
}

75
#endif
76