| 10 | |
| 11 | namespace utils{ |
| 12 | void *setup_mmap(const std::string &file_path, size_t *size) { |
| 13 | int fd; |
| 14 | struct stat st {}; |
| 15 | void *mapped_file; |
| 16 | |
| 17 | // set up mmap region |
| 18 | if ((fd = open(file_path.c_str(), O_RDONLY)) < 0) { |
| 19 | ERROR("Unable to open '%s', %s\n", file_path.c_str(), strerror(errno)); |
| 20 | exit(1); |
| 21 | } |
| 22 | |
| 23 | if ((fstat(fd, &st)) < 0) { |
| 24 | close(fd); |
| 25 | ERROR("Unable to fstat '%s', %s\n", file_path.c_str(), strerror(errno)); |
| 26 | exit(1); |
| 27 | } |
| 28 | |
| 29 | *size = st.st_size; |
| 30 | mapped_file = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 31 | if ((mapped_file) == MAP_FAILED) { |
| 32 | close(fd); |
| 33 | mapped_file = nullptr; |
| 34 | ERROR("Unable to allocate %llu bytes of memory, %s\n", (unsigned long long)st.st_size, strerror(errno)); |
| 35 | abort(); |
| 36 | } |
| 37 | |
| 38 | #ifdef MADV_HUGEPAGE |
| 39 | int mstatus = madvise(mapped_file, st.st_size, MADV_HUGEPAGE | MADV_SEQUENTIAL); |
| 40 | if (mstatus != 0) { |
| 41 | WARN("cannot turn on hugepage %s\n", strerror(errno)); |
| 42 | } |
| 43 | #endif |
| 44 | |
| 45 | close(fd); |
| 46 | return mapped_file; |
| 47 | } |
| 48 | |
| 49 | } |
no outgoing calls
no test coverage detected