Mercurial > hg > Applications > Grep
annotate c/blocked_mmap/main.cc @ 30:edecf5b459c9
rename mmap to blocked_mmap
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 11 May 2014 19:32:39 +0900 |
parents | c/mmap/main.cc@b9d005c23aaa |
children | 4580f792d4c6 |
rev | line source |
---|---|
3 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
3 #include <string.h> |
3 | 4 #include <unistd.h> |
5 #include <fcntl.h> | |
6 #include <sys/stat.h> | |
7 #include <sys/types.h> | |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
8 #include <sys/mman.h> |
3 | 9 |
10 | |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
11 const char *usr_help_str = "Usage : ./mmap [-file filename]"; |
3 | 12 |
13 int main(int argc, char *argv[]){ | |
14 | |
15 struct stat sb; | |
16 | |
17 char *filename = 0; | |
18 for (int i = 1; argv[i]; ++i) { | |
19 if (strcmp(argv[i], "-file") == 0){ | |
28
178cf2dfc45b
add make test
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
26
diff
changeset
|
20 filename = argv[i+1]; i++; |
3 | 21 } |
22 } | |
23 | |
24 if (filename == 0){ | |
25 puts(usr_help_str); | |
26 exit(1); | |
27 } | |
28 | |
29 int fd = -1; | |
30 if ((fd=open(filename,O_RDONLY,0666))==0){ | |
31 fprintf(stderr,"can't open %s\n",filename); | |
32 } | |
33 | |
34 if (fstat(fd,&sb)){ | |
35 fprintf(stderr,"can't open %s\n",filename); | |
36 } | |
37 | |
29
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
38 int file_size = sb.st_size; |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
39 int page_size = getpagesize(); |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
40 |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
41 int loop_num = file_size / page_size; |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
42 loop_num += (file_size % page_size != 0); |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
43 |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
44 char * file_mmap = NULL; |
30
edecf5b459c9
rename mmap to blocked_mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
45 char * file_head = NULL; // head of read file |
3 | 46 |
29
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
47 for (int i = 0; i < loop_num; i++) { |
30
edecf5b459c9
rename mmap to blocked_mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
48 // mmap 6th arg must be a multiple of the paging size. |
29
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
49 file_mmap = (char *)mmap(0, page_size, PROT_READ, MAP_PRIVATE, fd, i*page_size); |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
50 if (i == 0) file_head = file_mmap; |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
51 file_mmap += page_size; |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
52 } |
3 | 53 |
29
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
54 printf("%s\n",file_head); |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
55 |
b9d005c23aaa
inplement blocked mmap
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
56 munmap(file_mmap, file_size); |
3 | 57 close(fd); |
58 | |
59 return 0; | |
60 } |