|
19 | 19 | * along with dwarfs. If not, see <https://www.gnu.org/licenses/>. |
20 | 20 | */ |
21 | 21 |
|
| 22 | +#include <cassert> |
22 | 23 | #include <cerrno> |
23 | 24 |
|
24 | | -#include <fcntl.h> |
| 25 | +#ifndef _WIN32 |
25 | 26 | #include <sys/mman.h> |
26 | | -#include <sys/stat.h> |
27 | | -#include <unistd.h> |
28 | | - |
29 | | -#include <fmt/format.h> |
| 27 | +#endif |
30 | 28 |
|
31 | 29 | #include "dwarfs/error.h" |
32 | 30 | #include "dwarfs/mmap.h" |
33 | 31 |
|
34 | 32 | namespace dwarfs { |
35 | 33 |
|
36 | | -namespace { |
37 | | - |
38 | | -int safe_open(const std::string& path) { |
39 | | - int fd = ::open(path.c_str(), O_RDONLY); |
40 | | - |
41 | | - if (fd == -1) { |
42 | | - DWARFS_THROW(system_error, fmt::format("open('{}')", path)); |
43 | | - } |
44 | | - |
45 | | - return fd; |
46 | | -} |
47 | | - |
48 | | -size_t safe_size(int fd) { |
49 | | - struct stat st; |
50 | | - if (::fstat(fd, &st) == -1) { |
51 | | - DWARFS_THROW(system_error, "fstat"); |
52 | | - } |
53 | | - return st.st_size; |
54 | | -} |
55 | | - |
56 | | -void* safe_mmap(int fd, size_t size) { |
57 | | - if (size == 0) { |
58 | | - DWARFS_THROW(runtime_error, "empty file"); |
59 | | - } |
60 | | - |
61 | | - void* addr = ::mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); |
62 | | - |
63 | | - if (addr == MAP_FAILED) { |
64 | | - DWARFS_THROW(system_error, "mmap"); |
65 | | - } |
66 | | - |
67 | | - return addr; |
68 | | -} |
69 | | - |
70 | | -} // namespace |
71 | | - |
72 | | -std::error_code mmap::lock(off_t offset, size_t size) { |
| 34 | +std::error_code |
| 35 | +mmap::lock(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) { |
73 | 36 | std::error_code ec; |
74 | | - auto addr = reinterpret_cast<uint8_t*>(addr_) + offset; |
75 | | - if (::mlock(addr, size) != 0) { |
| 37 | + |
| 38 | +#ifndef _WIN32 |
| 39 | + if (::mlock(mf_.const_data() + offset, size) != 0) { |
76 | 40 | ec.assign(errno, std::generic_category()); |
77 | 41 | } |
| 42 | +#endif |
| 43 | + |
78 | 44 | return ec; |
79 | 45 | } |
80 | 46 |
|
81 | | -std::error_code mmap::release(off_t offset, size_t size) { |
| 47 | +std::error_code |
| 48 | +mmap::release(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) { |
82 | 49 | std::error_code ec; |
| 50 | + |
| 51 | +#ifndef _WIN32 |
83 | 52 | auto misalign = offset % page_size_; |
84 | 53 |
|
85 | 54 | offset -= misalign; |
86 | 55 | size += misalign; |
87 | 56 | size -= size % page_size_; |
88 | 57 |
|
89 | | - auto addr = reinterpret_cast<uint8_t*>(addr_) + offset; |
90 | | - if (::madvise(addr, size, MADV_DONTNEED) != 0) { |
| 58 | + if (::madvise(mf_.data() + offset, size, MADV_DONTNEED) != 0) { |
91 | 59 | ec.assign(errno, std::generic_category()); |
92 | 60 | } |
| 61 | +#endif |
| 62 | + |
93 | 63 | return ec; |
94 | 64 | } |
95 | 65 |
|
96 | | -std::error_code mmap::release_until(off_t offset) { |
| 66 | +std::error_code mmap::release_until(off_t offset [[maybe_unused]]) { |
97 | 67 | std::error_code ec; |
98 | 68 |
|
| 69 | +#ifndef _WIN32 |
99 | 70 | offset -= offset % page_size_; |
100 | 71 |
|
101 | | - if (::madvise(addr_, offset, MADV_DONTNEED) != 0) { |
| 72 | + if (::madvise(mf_.data(), offset, MADV_DONTNEED) != 0) { |
102 | 73 | ec.assign(errno, std::generic_category()); |
103 | 74 | } |
| 75 | +#endif |
| 76 | + |
104 | 77 | return ec; |
105 | 78 | } |
106 | 79 |
|
107 | | -void const* mmap::addr() const { return addr_; } |
| 80 | +void const* mmap::addr() const { return mf_.const_data(); } |
108 | 81 |
|
109 | | -size_t mmap::size() const { return size_; } |
| 82 | +size_t mmap::size() const { return mf_.size(); } |
110 | 83 |
|
111 | 84 | mmap::mmap(const std::string& path) |
112 | | - : fd_(safe_open(path)) |
113 | | - , size_(safe_size(fd_)) |
114 | | - , addr_(safe_mmap(fd_, size_)) |
115 | | - , page_size_(::sysconf(_SC_PAGESIZE)) {} |
| 85 | + : mf_(path, boost::iostreams::mapped_file::readonly) |
| 86 | +#ifndef _WIN32 |
| 87 | + , page_size_(::sysconf(_SC_PAGESIZE)) |
| 88 | +#endif |
| 89 | +{ |
| 90 | + assert(mf_.is_open()); |
| 91 | +} |
116 | 92 |
|
117 | 93 | mmap::mmap(const std::string& path, size_t size) |
118 | | - : fd_(safe_open(path)) |
119 | | - , size_(size) |
120 | | - , addr_(safe_mmap(fd_, size_)) |
121 | | - , page_size_(::sysconf(_SC_PAGESIZE)) {} |
122 | | - |
123 | | -mmap::~mmap() noexcept { |
124 | | - ::munmap(addr_, size_); |
125 | | - ::close(fd_); |
| 94 | + : mf_(path, boost::iostreams::mapped_file::readonly, size) |
| 95 | +#ifndef _WIN32 |
| 96 | + , page_size_(::sysconf(_SC_PAGESIZE)) |
| 97 | +#endif |
| 98 | +{ |
| 99 | + assert(mf_.is_open()); |
126 | 100 | } |
| 101 | + |
127 | 102 | } // namespace dwarfs |
0 commit comments