]> git.proxmox.com Git - libgit2.git/blob - src/unix/map.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / unix / map.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "common.h"
9
10 #if !defined(GIT_WIN32) && !defined(NO_MMAP)
11
12 #include "map.h"
13 #include <sys/mman.h>
14 #include <unistd.h>
15 #include <errno.h>
16
17 int git__page_size(size_t *page_size)
18 {
19 long sc_page_size = sysconf(_SC_PAGE_SIZE);
20 if (sc_page_size < 0) {
21 git_error_set(GIT_ERROR_OS, "can't determine system page size");
22 return -1;
23 }
24 *page_size = (size_t) sc_page_size;
25 return 0;
26 }
27
28 int git__mmap_alignment(size_t *alignment)
29 {
30 return git__page_size(alignment);
31 }
32
33 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset)
34 {
35 int mprot = PROT_READ;
36 int mflag = 0;
37
38 GIT_MMAP_VALIDATE(out, len, prot, flags);
39
40 out->data = NULL;
41 out->len = 0;
42
43 if (prot & GIT_PROT_WRITE)
44 mprot |= PROT_WRITE;
45
46 if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
47 mflag = MAP_SHARED;
48 else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
49 mflag = MAP_PRIVATE;
50 else
51 mflag = MAP_SHARED;
52
53 out->data = mmap(NULL, len, mprot, mflag, fd, offset);
54
55 if (!out->data || out->data == MAP_FAILED) {
56 git_error_set(GIT_ERROR_OS, "failed to mmap. Could not write data");
57 return -1;
58 }
59
60 out->len = len;
61
62 return 0;
63 }
64
65 int p_munmap(git_map *map)
66 {
67 GIT_ASSERT_ARG(map);
68 munmap(map->data, map->len);
69 map->data = NULL;
70 map->len = 0;
71
72 return 0;
73 }
74
75 #endif
76