Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
MappedFile.hpp
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <cstddef>
18#include <cstdint>
19#include <stdexcept>
20#include <string>
21
22#ifdef _WIN32
23#ifndef WIN32_LEAN_AND_MEAN
24#define WIN32_LEAN_AND_MEAN
25#endif
26#include <windows.h>
27#else
28#include <fcntl.h>
29#include <sys/mman.h>
30#include <sys/stat.h>
31#include <unistd.h>
32#endif
33
34namespace correlation::core {
35
37inline constexpr std::uint64_t K_MAX_TRAJECTORY_BYTES =
38 static_cast<std::uint64_t>(4) * 1024 * 1024 * 1024;
39
55public:
63 explicit MappedFile(const std::string &path, bool enforce_size_limit = true)
64#ifdef _WIN32
65 : file_handle_(CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
66 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr))
67#else
68 : fd_(::open(path.c_str(), O_RDONLY, 0))
69#endif
70 {
71#ifdef _WIN32
72 if (file_handle_ == INVALID_HANDLE_VALUE)
73 throw std::runtime_error("MappedFile: cannot open: " + path);
74
75 LARGE_INTEGER li;
76 if (!GetFileSizeEx(file_handle_, &li)) {
77 CloseHandle(file_handle_);
78 throw std::runtime_error("MappedFile: cannot get size: " + path);
79 }
80 size_ = static_cast<std::size_t>(li.QuadPart);
81
82 if (enforce_size_limit && static_cast<std::uint64_t>(size_) > K_MAX_TRAJECTORY_BYTES) {
83 CloseHandle(file_handle_);
84 throw std::runtime_error("MappedFile: file exceeds 4 GiB trajectory limit: " + path);
85 }
86
87 if (size_ == 0) {
88 mapping_ = nullptr;
89 data_ = nullptr;
90 } else {
91 mapping_ = CreateFileMappingA(file_handle_, nullptr, PAGE_READONLY, 0, 0, nullptr);
92 if (mapping_ == nullptr) {
93 CloseHandle(file_handle_);
94 throw std::runtime_error("MappedFile: CreateFileMapping failed: " + path);
95 }
96
97 data_ = MapViewOfFile(mapping_, FILE_MAP_READ, 0, 0, 0);
98 if (data_ == nullptr) {
99 CloseHandle(mapping_);
100 CloseHandle(file_handle_);
101 throw std::runtime_error("MappedFile: MapViewOfFile failed: " + path);
102 }
103 }
104#else
105 if (fd_ < 0) {
106 throw std::runtime_error("MappedFile: cannot open: " + path);
107 }
108
109 struct stat file_stat{};
110 if (::fstat(fd_, &file_stat) != 0) {
111 ::close(fd_);
112 throw std::runtime_error("MappedFile: cannot stat: " + path);
113 }
114 size_ = static_cast<std::size_t>(file_stat.st_size);
115
116 if (enforce_size_limit && static_cast<std::uint64_t>(size_) > K_MAX_TRAJECTORY_BYTES) {
117 ::close(fd_);
118 throw std::runtime_error("MappedFile: file exceeds 4 GiB trajectory limit: " + path);
119 }
120
121 if (size_ == 0) {
122 data_ = nullptr;
123 } else {
124 data_ = ::mmap(nullptr, size_, PROT_READ, MAP_PRIVATE, fd_, 0);
125 if (data_ == MAP_FAILED) {
126 data_ = nullptr;
127 ::close(fd_);
128 throw std::runtime_error("MappedFile: mmap failed: " + path);
129 }
130 }
131#endif
132 }
133
135
136 // Move-only.
137 MappedFile(MappedFile &&other) noexcept
138 : data_(other.data_), size_(other.size_)
139#ifdef _WIN32
140 ,
141 file_handle_(other.file_handle_), mapping_(other.mapping_)
142#else
143 ,
144 fd_(other.fd_)
145#endif
146 {
147 other.data_ = nullptr;
148 other.size_ = 0;
149#ifdef _WIN32
150 other.file_handle_ = INVALID_HANDLE_VALUE;
151 other.mapping_ = nullptr;
152#else
153 other.fd_ = -1;
154#endif
155 }
156
157 MappedFile &operator=(MappedFile &&other) noexcept {
158 if (this != &other) {
159 release();
160 data_ = other.data_;
161 size_ = other.size_;
162#ifdef _WIN32
163 file_handle_ = other.file_handle_;
164 mapping_ = other.mapping_;
165 other.file_handle_ = INVALID_HANDLE_VALUE;
166 other.mapping_ = nullptr;
167#else
168 fd_ = other.fd_;
169 other.fd_ = -1;
170#endif
171 other.data_ = nullptr;
172 other.size_ = 0;
173 }
174 return *this;
175 }
176
177 MappedFile(const MappedFile &) = delete;
178 MappedFile &operator=(const MappedFile &) = delete;
179
181 [[nodiscard]] const char *data() const noexcept { return static_cast<const char *>(data_); }
182
184 [[nodiscard]] std::size_t size() const noexcept { return size_; }
185
187 [[nodiscard]] const char *end() const noexcept {
188 return static_cast<const char *>(data_) + size_;
189 }
190
191private:
192 void release() noexcept {
193#ifdef _WIN32
194 if (data_ != nullptr)
195 UnmapViewOfFile(data_);
196 if (mapping_ != nullptr)
197 CloseHandle(mapping_);
198 if (file_handle_ != INVALID_HANDLE_VALUE)
199 CloseHandle(file_handle_);
200#else
201 if (data_ != nullptr) {
202 ::munmap(data_, size_);
203 }
204 if (fd_ >= 0) {
205 ::close(fd_);
206 }
207#endif
208 data_ = nullptr;
209 size_ = 0;
210 }
211
212 void *data_{nullptr};
213 std::size_t size_{0};
214
215#ifdef _WIN32
216 HANDLE file_handle_{INVALID_HANDLE_VALUE};
217 HANDLE mapping_{nullptr};
218#else
219 int fd_{-1};
220#endif
221};
222
223} // namespace correlation::core
MappedFile & operator=(MappedFile &&other) noexcept
Definition MappedFile.hpp:157
void release() noexcept
Definition MappedFile.hpp:192
~MappedFile()
Definition MappedFile.hpp:134
MappedFile(const MappedFile &)=delete
const char * end() const noexcept
Convenience: pointer past the end.
Definition MappedFile.hpp:187
std::size_t size() const noexcept
Size of the mapped region in bytes.
Definition MappedFile.hpp:184
MappedFile(const std::string &path, bool enforce_size_limit=true)
Opens and memory-maps a file for reading.
Definition MappedFile.hpp:63
int fd_
Definition MappedFile.hpp:219
std::size_t size_
Definition MappedFile.hpp:213
const char * data() const noexcept
Pointer to the start of the mapped file contents.
Definition MappedFile.hpp:181
MappedFile(MappedFile &&other) noexcept
Definition MappedFile.hpp:137
MappedFile & operator=(const MappedFile &)=delete
void * data_
Definition MappedFile.hpp:212
Definition ITrajectoryCalculator.hpp:13
constexpr std::uint64_t K_MAX_TRAJECTORY_BYTES
Maximum trajectory file size (4 GiB).
Definition MappedFile.hpp:37