]>
Commit | Line | Data |
---|---|---|
ee9c93b9 JF |
1 | /* Minimal - the simplest thing that could possibly work |
2 | * Copyright (C) 2007 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* | |
6 | * Redistribution and use in source and binary | |
7 | * forms, with or without modification, are permitted | |
8 | * provided that the following conditions are met: | |
9 | * | |
10 | * 1. Redistributions of source code must retain the | |
11 | * above copyright notice, this list of conditions | |
12 | * and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the | |
14 | * above copyright notice, this list of conditions | |
15 | * and the following disclaimer in the documentation | |
16 | * and/or other materials provided with the | |
17 | * distribution. | |
18 | * 3. The name of the author may not be used to endorse | |
19 | * or promote products derived from this software | |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
24 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
29 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
32 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
33 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
34 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
35 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
36 | */ | |
37 | ||
64c5d5aa JF |
38 | #ifndef MINIMAL_MAPPING_H |
39 | #define MINIMAL_MAPPING_H | |
40 | ||
d4987f7d JF |
41 | #include <sys/mman.h> |
42 | ||
43 | #include <errno.h> | |
44 | #include <unistd.h> | |
45 | ||
46 | #include <sys/types.h> | |
47 | #include <sys/stat.h> | |
48 | #include <fcntl.h> | |
49 | ||
20187605 JF |
50 | #include "minimal/stdlib.h" |
51 | ||
c7409a79 | 52 | void *map(const char *path, size_t offset, size_t size, size_t *psize, bool ro) { |
d4987f7d JF |
53 | int fd; |
54 | _syscall(fd = open(path, ro ? O_RDONLY : O_RDWR)); | |
55 | ||
3ed54889 JF |
56 | if (size == _not(size_t)) { |
57 | struct stat stat; | |
58 | _syscall(fstat(fd, &stat)); | |
59 | size = stat.st_size; | |
60 | } | |
d4987f7d | 61 | |
3ed54889 JF |
62 | if (psize != NULL) |
63 | *psize = size; | |
d4987f7d JF |
64 | |
65 | void *base; | |
3ed54889 | 66 | _syscall(base = mmap(NULL, size, ro ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset)); |
d4987f7d JF |
67 | |
68 | _syscall(close(fd)); | |
69 | return base; | |
70 | } | |
64c5d5aa JF |
71 | |
72 | #endif/*MINIMAL_MAPPING_H*/ |