commit 3fe6badd5b87cccec277ac0ffbf2c1055ece9f32
parent 1500b851595aa13e5e4a4e085c2b545974ddd5b9
Author: David Freifeld <freifeld.david@gmail.com>
Date: Tue, 23 Jun 2020 11:55:04 -0700
Experiments with mmap
Diffstat:
| A | utils.cpp | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
1 file changed, 39 insertions(+), 0 deletions(-)
diff --git a/utils.cpp b/utils.cpp
@@ -0,0 +1,39 @@
+#include <iostream>
+#include <cstdlib>
+#include <cstdio>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+static uintmax_t wc(char const *fname)
+{
+ static const auto BUFFER_SIZE = 16*1024;
+ int fd = open(fname, O_RDONLY);
+ if(fd == -1)
+ exit(1);
+
+ /* Advise the kernel of our access pattern. */
+ // posix_fadvise(fd, 0, 0, 1); // FDADVICE_SEQUENTIAL
+
+ char buf[BUFFER_SIZE + 1];
+ uintmax_t lines = 0;
+
+ while(size_t bytes_read = read(fd, buf, BUFFER_SIZE))
+ {
+ if(bytes_read == (size_t)-1)
+ exit(1);
+ if (!bytes_read)
+ break;
+
+ for(char *p = buf; (p = (char*) memchr(p, '\n', (buf + bytes_read) - p)); ++p)
+ printf("Line? %s END\n", buf);
+ ++lines;
+ }
+
+ return lines;
+}
+
+int main()
+{
+ wc("./shuffled.txt");
+}