]> git.proxmox.com Git - libgit2.git/blob - src/unix/map.c
Resolve comments from pull request
[libgit2.git] / src / unix / map.c
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
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 #ifndef GIT_WIN32
10
11 #include "map.h"
12 #include <sys/mman.h>
13 #include <errno.h>
14
15 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
16 {
17 int mprot = 0;
18 int mflag = 0;
19
20 GIT_MMAP_VALIDATE(out, len, prot, flags);
21
22 out->data = NULL;
23 out->len = 0;
24
25 if (prot & GIT_PROT_WRITE)
26 mprot = PROT_WRITE;
27 else if (prot & GIT_PROT_READ)
28 mprot = PROT_READ;
29
30 if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
31 mflag = MAP_SHARED;
32 else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
33 mflag = MAP_PRIVATE;
34
35 out->data = mmap(NULL, len, mprot, mflag, fd, offset);
36 if (!out->data || out->data == MAP_FAILED) {
37 giterr_set(GITERR_OS, "Failed to mmap. Could not write data");
38 return -1;
39 }
40
41 out->len = len;
42
43 return 0;
44 }
45
46 int p_munmap(git_map *map)
47 {
48 assert(map != NULL);
49 munmap(map->data, map->len);
50 return 0;
51 }
52
53 #endif
54