Created
October 22, 2014 00:16
-
-
Save b4n/5949af712d4a6927fc96 to your computer and use it in GitHub Desktop.
MIO file vs. mmap()ed file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "mio/mio.h" | |
int | |
main (int argc, | |
char **argv) | |
{ | |
int ret = 0; | |
int i; | |
for (i = 1; ret == 0 && i < argc; i++) { | |
FILE *fp; | |
if (! (fp = fopen (argv[1], "r"))) { | |
perror ("fopen()"); | |
ret = 1; | |
} else { | |
MIO *mio = mio_new_fp (fp, NULL); | |
if (! mio) { | |
perror ("mio_new_fp()"); | |
ret = 2; | |
} else { | |
while (! mio_eof (mio)) { | |
int c = mio_getc (mio); | |
} | |
mio_free (mio); | |
} | |
fclose (fp); | |
} | |
} | |
return ret; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fcntl.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/mman.h> | |
#include "mio/mio.h" | |
int | |
main (int argc, | |
char **argv) | |
{ | |
int ret = 0; | |
int i; | |
for (i = 1; ret == 0 && i < argc; i++) { | |
int fd; | |
if ((fd = open (argv[1], O_RDONLY)) < 0) { | |
perror ("open()"); | |
ret = 2; | |
} else { | |
size_t len = lseek (fd, 0, SEEK_END); | |
void *map = mmap (NULL, len, PROT_READ, MAP_PRIVATE, fd, 0); | |
if (map == MAP_FAILED) { | |
perror ("mmap()"); | |
ret = 3; | |
} else { | |
MIO *mio = mio_new_memory (map, len, NULL, NULL); | |
if (! mio) { | |
perror ("mio_new_memory()"); | |
ret = 4; | |
} else { | |
while (! mio_eof (mio)) { | |
int c = mio_getc (mio); | |
} | |
mio_free (mio); | |
} | |
munmap (map, len); | |
} | |
close (fd); | |
} | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment