Skip to content

Instantly share code, notes, and snippets.

@b4n
Created October 22, 2014 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b4n/5949af712d4a6927fc96 to your computer and use it in GitHub Desktop.
Save b4n/5949af712d4a6927fc96 to your computer and use it in GitHub Desktop.
MIO file vs. mmap()ed file
#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;
}
#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