]> git.proxmox.com Git - libgit2.git/blob - src/unix/map.c
Merge branch 'cmn/zlib-update' into cmn/update-zlib
[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 long git__page_size(void)
17 {
18 return sysconf(_SC_PAGE_SIZE);
19 }
20
21 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
22 {
23 int mprot = 0;
24 int mflag = 0;
25
26 GIT_MMAP_VALIDATE(out, len, prot, flags);
27
28 out->data = NULL;
29 out->len = 0;
30
31 if (prot & GIT_PROT_WRITE)
32 mprot = PROT_WRITE;
33 else if (prot & GIT_PROT_READ)
34 mprot = PROT_READ;
35
36 if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
37 mflag = MAP_SHARED;
38 else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
39 mflag = MAP_PRIVATE;
40 else
41 mflag = MAP_SHARED;
42
43 out->data = mmap(NULL, len, mprot, mflag, fd, offset);
44
45 if (!out->data || out->data == MAP_FAILED) {
46 giterr_set(GITERR_OS, "Failed to mmap. Could not write data");
47 return -1;
48 }
49
50 out->len = len;
51
52 return 0;
53 }
54
55 int p_munmap(git_map *map)
56 {
57 assert(map != NULL);
58 munmap(map->data, map->len);
59
60 return 0;
61 }
62
63 #endif
64