diff options
Diffstat (limited to 'src/mapping.c')
-rw-r--r-- | src/mapping.c | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/mapping.c b/src/mapping.c index 19d7129..e4ce96c 100644 --- a/src/mapping.c +++ b/src/mapping.c | |||
@@ -5,13 +5,16 @@ | |||
5 | #include <errno.h> | 5 | #include <errno.h> |
6 | #include <unistd.h> | 6 | #include <unistd.h> |
7 | 7 | ||
8 | #include <stdio.h> | ||
9 | |||
8 | #include "mapping.h" | 10 | #include "mapping.h" |
9 | 11 | ||
10 | int map_file(const char* filename, void **addr, size_t *size) | 12 | static int _map_file(const char* filename, void **addr, size_t *size, int writable) |
11 | { | 13 | { |
12 | struct stat info; | 14 | struct stat info; |
13 | int error = 0; | 15 | int error = 0; |
14 | int fd; | 16 | int fd; |
17 | int flags = writable ? MAP_SHARED : MAP_PRIVATE; | ||
15 | 18 | ||
16 | error = stat(filename, &info); | 19 | error = stat(filename, &info); |
17 | if (!error) { | 20 | if (!error) { |
@@ -19,12 +22,19 @@ int map_file(const char* filename, void **addr, size_t *size) | |||
19 | if (info.st_size > 0) { | 22 | if (info.st_size > 0) { |
20 | fd = open(filename, O_RDWR); | 23 | fd = open(filename, O_RDWR); |
21 | if (fd >= 0) { | 24 | if (fd >= 0) { |
22 | *addr = mmap(NULL, *size, | 25 | *addr = mmap(NULL, *size, |
23 | PROT_READ | PROT_WRITE, | 26 | PROT_READ | PROT_WRITE, |
24 | MAP_PRIVATE, | 27 | flags, |
25 | fd, 0); | 28 | fd, 0); |
26 | if (*addr == MAP_FAILED) | 29 | if (*addr == MAP_FAILED) |
27 | error = -1; | 30 | error = -1; |
31 | else { | ||
32 | /* tell kernel to start getting the pages */ | ||
33 | error = madvise(*addr, *size, MADV_SEQUENTIAL | MADV_WILLNEED); | ||
34 | if (error) { | ||
35 | perror("madvise"); | ||
36 | } | ||
37 | } | ||
28 | close(fd); | 38 | close(fd); |
29 | } else | 39 | } else |
30 | error = fd; | 40 | error = fd; |
@@ -33,3 +43,14 @@ int map_file(const char* filename, void **addr, size_t *size) | |||
33 | } | 43 | } |
34 | return error; | 44 | return error; |
35 | } | 45 | } |
46 | |||
47 | |||
48 | int map_file(const char* filename, void **addr, size_t *size) | ||
49 | { | ||
50 | return _map_file(filename, addr, size, 0); | ||
51 | } | ||
52 | |||
53 | int map_file_rw(const char* filename, void **addr, size_t *size) | ||
54 | { | ||
55 | return _map_file(filename, addr, size, 1); | ||
56 | } | ||