]> git.proxmox.com Git - libgit2.git/blob - src/unix/map.c
7f9076e19cfdfabb4acf9a56285f3fd729a877af
[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 #include "git2/common.h"
11
12 #if !defined(GIT_WIN32) && !defined(NO_MMAP)
13
14 #include "map.h"
15 #include <sys/mman.h>
16 #include <unistd.h>
17 #include <errno.h>
18
19 int git__page_size(size_t *page_size)
20 {
21 long sc_page_size = sysconf(_SC_PAGE_SIZE);
22 if (sc_page_size < 0) {
23 git_error_set(GIT_ERROR_OS, "can't determine system page size");
24 return -1;
25 }
26 *page_size = (size_t) sc_page_size;
27 return 0;
28 }
29
30 int git__mmap_alignment(size_t *alignment)
31 {
32 return git__page_size(alignment);
33 }
34
35 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset)
36 {
37 int mprot = PROT_READ;
38 int mflag = 0;
39
40 GIT_MMAP_VALIDATE(out, len, prot, flags);
41
42 out->data = NULL;
43 out->len = 0;
44
45 if (prot & GIT_PROT_WRITE)
46 mprot |= PROT_WRITE;
47
48 if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
49 mflag = MAP_SHARED;
50 else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
51 mflag = MAP_PRIVATE;
52 else
53 mflag = MAP_SHARED;
54
55 out->data = mmap(NULL, len, mprot, mflag, fd, offset);
56
57 if (!out->data || out->data == MAP_FAILED) {
58 git_error_set(GIT_ERROR_OS, "failed to mmap. Could not write data");
59 return -1;
60 }
61
62 out->len = len;
63
64 return 0;
65 }
66
67 int p_munmap(git_map *map)
68 {
69 assert(map != NULL);
70 munmap(map->data, map->len);
71
72 return 0;
73 }
74
75 #endif
76