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