MCPcopy Create free account
hub / github.com/1a1a11a/libCacheSim / setup_mmap

Function setup_mmap

libCacheSim/bin/traceUtils/utils.cpp:12–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10
11namespace utils{
12void *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}

Callers 2

_reverse_fileFunction · 0.85
_reverse_fileFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected