changeset 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 b9d005c23aaa
children 4580f792d4c6
files c/blocked_mmap/Makefile c/blocked_mmap/main.cc c/mmap/Makefile c/mmap/main.cc
diffstat 4 files changed, 73 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/c/blocked_mmap/Makefile	Sun May 11 19:32:39 2014 +0900
@@ -0,0 +1,13 @@
+TARGET= mmap
+OPTION= -Wall -O0 -g
+
+$(TARGET):main.cc
+	clang $(OPTION) -o $(TARGET) main.cc
+
+clean:
+	rm -f $(TARGET)
+	rm -r $(TARGET).dSYM
+	rm -f *~ \#*
+
+test:
+	./mmap -file main.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/c/blocked_mmap/main.cc	Sun May 11 19:32:39 2014 +0900
@@ -0,0 +1,60 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+
+
+const char *usr_help_str = "Usage : ./mmap [-file filename]";
+
+int main(int argc, char *argv[]){
+
+    struct stat sb;
+
+    char *filename = 0;
+    for (int i = 1; argv[i]; ++i) {
+        if (strcmp(argv[i], "-file") == 0){
+            filename = argv[i+1]; i++;
+        }
+    }
+
+    if (filename == 0){
+        puts(usr_help_str);
+        exit(1);
+    }
+
+    int fd = -1;
+    if ((fd=open(filename,O_RDONLY,0666))==0){
+        fprintf(stderr,"can't open %s\n",filename);
+    }
+
+    if (fstat(fd,&sb)){
+        fprintf(stderr,"can't open %s\n",filename);
+    }
+
+    int file_size = sb.st_size;
+    int page_size = getpagesize();
+
+    int loop_num = file_size / page_size;
+    loop_num += (file_size % page_size != 0);
+
+    char * file_mmap = NULL;
+    char * file_head = NULL; // head of read file
+
+    for (int i = 0; i < loop_num; i++) {
+        // mmap 6th arg must be a multiple of the paging size.
+        file_mmap = (char *)mmap(0, page_size, PROT_READ, MAP_PRIVATE, fd, i*page_size);
+        if (i == 0) file_head = file_mmap;
+        file_mmap += page_size;
+    }
+
+    printf("%s\n",file_head);
+
+    munmap(file_mmap, file_size);
+    close(fd);
+
+    return 0;
+}
--- a/c/mmap/Makefile	Sun May 11 19:02:56 2014 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,13 +0,0 @@
-TARGET= mmap
-OPTION= -Wall -O0 -g
-
-$(TARGET):main.cc
-	clang $(OPTION) -o $(TARGET) main.cc
-
-clean:
-	rm -f $(TARGET)
-	rm -r $(TARGET).dSYM
-	rm -f *~ \#*
-
-test:
-	./mmap -file main.cc
--- a/c/mmap/main.cc	Sun May 11 19:02:56 2014 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,59 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/mman.h>
-
-
-const char *usr_help_str = "Usage : ./mmap [-file filename]";
-
-int main(int argc, char *argv[]){
-
-    struct stat sb;
-
-    char *filename = 0;
-    for (int i = 1; argv[i]; ++i) {
-        if (strcmp(argv[i], "-file") == 0){
-            filename = argv[i+1]; i++;
-        }
-    }
-
-    if (filename == 0){
-        puts(usr_help_str);
-        exit(1);
-    }
-
-    int fd = -1;
-    if ((fd=open(filename,O_RDONLY,0666))==0){
-        fprintf(stderr,"can't open %s\n",filename);
-    }
-
-    if (fstat(fd,&sb)){
-        fprintf(stderr,"can't open %s\n",filename);
-    }
-
-    int file_size = sb.st_size;
-    int page_size = getpagesize();
-
-    int loop_num = file_size / page_size;
-    loop_num += (file_size % page_size != 0);
-
-    char * file_mmap = NULL;
-    char * file_head = NULL;
-
-    for (int i = 0; i < loop_num; i++) {
-        file_mmap = (char *)mmap(0, page_size, PROT_READ, MAP_PRIVATE, fd, i*page_size);
-        if (i == 0) file_head = file_mmap;
-        file_mmap += page_size;
-    }
-
-    printf("%s\n",file_head);
-
-    munmap(file_mmap, file_size);
-    close(fd);
-
-    return 0;
-}