Itoyori  v0.0.1
file_mem_manager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <unordered_map>
4 
5 #include "ityr/common/util.hpp"
6 #include "ityr/ori/util.hpp"
7 #include "ityr/ori/file_mem.hpp"
8 
10 
12 public:
13  file_mem& get(void* addr) {
14  auto it = file_mem_entries_.find(addr);
15  if (it != file_mem_entries_.end()) {
16  return it->second.fm;
17  } else {
18  common::die("File pointer %p was passed but not allocated by Itoyori", addr);
19  }
20  }
21 
22  void* create(const std::string& fpath, bool mlock) {
23  entry e(fpath, mlock);
24 
25  ITYR_CHECK(file_mem_entries_.find(e.vm.addr()) == file_mem_entries_.end());
26  void* addr = e.vm.addr();
27 
28  file_mem_entries_[addr] = std::move(e);
29  return addr;
30  }
31 
32  void destroy(void* addr) {
33  auto it = file_mem_entries_.find(addr);
34  if (it != file_mem_entries_.end()) {
35  file_mem_entries_.erase(it);
36  } else {
37  common::die("File pointer %p was passed but not allocated by Itoyori", addr);
38  }
39  }
40 
41 private:
42  struct entry {
43  entry() {}
44  entry(const std::string& fpath, bool mlock)
45  : fm(fpath),
46  vm(common::reserve_same_vm_coll(fm.size(), common::get_page_size())) {
47  fm.map_to_vm(vm.addr(), vm.size(), 0);
48  if (mlock) {
49  if (::mlock2(vm.addr(), vm.size(), MLOCK_ONFAULT) == -1) {
50  perror("mlock2");
51  common::die("mlock2() failed");
52  }
53  }
54  }
55 
56  file_mem fm;
57  common::virtual_mem vm;
58  };
59 
60  std::unordered_map<void*, entry> file_mem_entries_;
61 };
62 
64 
65 }
Definition: util.hpp:176
Definition: file_mem_manager.hpp:11
void destroy(void *addr)
Definition: file_mem_manager.hpp:32
void * create(const std::string &fpath, bool mlock)
Definition: file_mem_manager.hpp:22
file_mem & get(void *addr)
Definition: file_mem_manager.hpp:13
Definition: file_mem.hpp:12
#define ITYR_CHECK(cond)
Definition: util.hpp:48
std::size_t get_page_size()
Definition: util.hpp:170
virtual_mem reserve_same_vm_coll(std::size_t size, std::size_t alignment=alignof(max_align_t))
Definition: virtual_mem.hpp:170
Definition: file_mem_manager.hpp:9
constexpr auto size(const checkout_span< T, Mode > &cs) noexcept
Definition: checkout_span.hpp:178
ForwardIteratorD move(const ExecutionPolicy &policy, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIteratorD first_d)
Move a range to another.
Definition: parallel_loop.hpp:934