source src/unix/map.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
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 | 9585 | 2 | int git__page_size(size_t *page_size) | |
20 | - | { | ||
21 | 9585 | 2 | long sc_page_size = sysconf(_SC_PAGE_SIZE); | |
22 | 9585 | 3 | if (sc_page_size < 0) { | |
23 | ##### | 4 | git_error_set(GIT_ERROR_OS, "can't determine system page size"); | |
24 | ##### | 5 | return -1; | |
25 | - | } | ||
26 | 9585 | 6 | *page_size = (size_t) sc_page_size; | |
27 | 9585 | 6 | return 0; | |
28 | - | } | ||
29 | - | |||
30 | 9580 | 2 | int git__mmap_alignment(size_t *alignment) | |
31 | - | { | ||
32 | 9580 | 2 | return git__page_size(alignment); | |
33 | - | } | ||
34 | - | |||
35 | 10224 | 2 | int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset) | |
36 | - | { | ||
37 | 10224 | 2 | int mprot = PROT_READ; | |
38 | 10224 | 2 | int mflag = 0; | |
39 | - | |||
40 | 10224 | 2-9 | GIT_MMAP_VALIDATE(out, len, prot, flags); | |
41 | - | |||
42 | 10224 | 10 | out->data = NULL; | |
43 | 10224 | 10 | out->len = 0; | |
44 | - | |||
45 | 10224 | 10 | if (prot & GIT_PROT_WRITE) | |
46 | 4791 | 11 | mprot |= PROT_WRITE; | |
47 | - | |||
48 | 10224 | 12 | if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED) | |
49 | 10224 | 13 | mflag = MAP_SHARED; | |
50 | ##### | 14 | else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE) | |
51 | ##### | 15 | mflag = MAP_PRIVATE; | |
52 | - | else | ||
53 | ##### | 16 | mflag = MAP_SHARED; | |
54 | - | |||
55 | 10224 | 17 | out->data = mmap(NULL, len, mprot, mflag, fd, offset); | |
56 | - | |||
57 | 10224 | 18,19 | if (!out->data || out->data == MAP_FAILED) { | |
58 | ##### | 20 | git_error_set(GIT_ERROR_OS, "failed to mmap. Could not write data"); | |
59 | ##### | 21 | return -1; | |
60 | - | } | ||
61 | - | |||
62 | 10224 | 22 | out->len = len; | |
63 | - | |||
64 | 10224 | 22 | return 0; | |
65 | - | } | ||
66 | - | |||
67 | 10224 | 2 | int p_munmap(git_map *map) | |
68 | - | { | ||
69 | 10224 | 2,3 | assert(map != NULL); | |
70 | 10224 | 4 | munmap(map->data, map->len); | |
71 | - | |||
72 | 10224 | 5 | return 0; | |
73 | - | } | ||
74 | - | |||
75 | - | #endif | ||
76 | - |