|
| 1 | +#include <stdio.h> |
| 2 | +#include <sys/mman.h> |
| 3 | +#include <emscripten.h> |
| 4 | +#include <string.h> |
| 5 | +#include <assert.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <fcntl.h> |
| 8 | +#include <sys/types.h> |
| 9 | +#include <sys/stat.h> |
| 10 | +#include <unistd.h> |
| 11 | +#include <sys/io.h> |
| 12 | +#include <sys/mman.h> |
| 13 | + |
| 14 | +int main() { |
| 15 | + EM_ASM( |
| 16 | + FS.mkdir('yolo'); |
| 17 | + FS.writeFile('/yolo/in.txt', 'mmap ftw!'); |
| 18 | + ); |
| 19 | + |
| 20 | + // Use mmap to read in.txt |
| 21 | + { |
| 22 | + const char* path = "/yolo/in.txt"; |
| 23 | + int fd = open(path, O_RDONLY); |
| 24 | + assert(fd != -1); |
| 25 | + |
| 26 | + int filesize = 9; |
| 27 | + char* map = (char*)mmap(NULL, filesize, PROT_READ, MAP_PRIVATE, fd, 0); |
| 28 | + assert(map != MAP_FAILED); |
| 29 | + |
| 30 | + printf("/yolo/in.txt content="); |
| 31 | + for (int i = 0; i < filesize; i++) { |
| 32 | + printf("%c", map[i]); |
| 33 | + } |
| 34 | + printf("\n"); |
| 35 | + |
| 36 | + int rc = munmap(map, filesize); |
| 37 | + assert(rc == 0); |
| 38 | + |
| 39 | + close(fd); |
| 40 | + } |
| 41 | + |
| 42 | + // Use mmap to write out.txt |
| 43 | + { |
| 44 | + const char* text = "written mmap"; |
| 45 | + const char* path = "/yolo/out.txt"; |
| 46 | + |
| 47 | + int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600); |
| 48 | + assert(fd != -1); |
| 49 | + |
| 50 | + size_t textsize = strlen(text) + 1; // + \0 null character |
| 51 | + assert(lseek(fd, textsize - 1, SEEK_SET) != -1); |
| 52 | + |
| 53 | + // need to write something first to allow us to mmap |
| 54 | + assert(write(fd, "", 1) != -1); |
| 55 | + |
| 56 | + char *map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 57 | + assert(map != MAP_FAILED); |
| 58 | + |
| 59 | + for (size_t i = 0; i < textsize; i++) { |
| 60 | + map[i] = text[i]; |
| 61 | + } |
| 62 | + |
| 63 | + assert(msync(map, textsize, MS_SYNC) != -1); |
| 64 | + |
| 65 | + assert(munmap(map, textsize) != -1); |
| 66 | + |
| 67 | + close(fd); |
| 68 | + } |
| 69 | + |
| 70 | + { |
| 71 | + FILE* fd = fopen("/yolo/out.txt", "r"); |
| 72 | + if (fd == NULL) { |
| 73 | + printf("failed to open /yolo/out.txt\n"); |
| 74 | + return 1; |
| 75 | + } |
| 76 | + char buffer[13]; |
| 77 | + fread(buffer, 1, 14, fd); |
| 78 | + printf("/yolo/out.txt content=%s\n", buffer); |
| 79 | + fclose(fd); |
| 80 | + } |
| 81 | + |
| 82 | + // MAP_PRIVATE |
| 83 | + { |
| 84 | + const char* text = "written mmap"; |
| 85 | + const char* path = "/yolo/private.txt"; |
| 86 | + |
| 87 | + int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600); |
| 88 | + assert(fd != -1); |
| 89 | + |
| 90 | + size_t textsize = strlen(text) + 1; // + \0 null character |
| 91 | + assert(lseek(fd, textsize - 1, SEEK_SET) != -1); |
| 92 | + |
| 93 | + // need to write something first to allow us to mmap |
| 94 | + assert(write(fd, "", 1) != -1); |
| 95 | + |
| 96 | + char *map = (char*)mmap(0, textsize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 97 | + assert(map != MAP_FAILED); |
| 98 | + |
| 99 | + for (size_t i = 0; i < textsize; i++) { |
| 100 | + map[i] = text[i]; |
| 101 | + } |
| 102 | + |
| 103 | + assert(msync(map, textsize, MS_SYNC) != -1); |
| 104 | + |
| 105 | + assert(munmap(map, textsize) != -1); |
| 106 | + |
| 107 | + close(fd); |
| 108 | + } |
| 109 | + |
| 110 | + { |
| 111 | + FILE* fd = fopen("/yolo/private.txt", "r"); |
| 112 | + if (fd == NULL) { |
| 113 | + printf("failed to open /yolo/private.txt\n"); |
| 114 | + return 1; |
| 115 | + } |
| 116 | + char buffer[13]; |
| 117 | + fread(buffer, 1, 14, fd); |
| 118 | + printf("/yolo/private.txt content=%s\n", buffer); |
| 119 | + fclose(fd); |
| 120 | + } |
| 121 | + |
| 122 | + return 0; |
| 123 | +} |
0 commit comments