23#ifndef WIN32_LEAN_AND_MEAN
24#define WIN32_LEAN_AND_MEAN
38 static_cast<std::uint64_t
>(4) * 1024 * 1024 * 1024;
63 explicit MappedFile(
const std::string &path,
bool enforce_size_limit =
true)
65 : file_handle_(CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ,
nullptr,
66 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr))
68 :
fd_(::open(path.c_str(), O_RDONLY, 0))
72 if (file_handle_ == INVALID_HANDLE_VALUE)
73 throw std::runtime_error(
"MappedFile: cannot open: " + path);
76 if (!GetFileSizeEx(file_handle_, &li)) {
77 CloseHandle(file_handle_);
78 throw std::runtime_error(
"MappedFile: cannot get size: " + path);
80 size_ =
static_cast<std::size_t
>(li.QuadPart);
83 CloseHandle(file_handle_);
84 throw std::runtime_error(
"MappedFile: file exceeds 4 GiB trajectory limit: " + path);
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);
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);
106 throw std::runtime_error(
"MappedFile: cannot open: " + path);
109 struct stat file_stat{};
110 if (::fstat(
fd_, &file_stat) != 0) {
112 throw std::runtime_error(
"MappedFile: cannot stat: " + path);
114 size_ =
static_cast<std::size_t
>(file_stat.st_size);
118 throw std::runtime_error(
"MappedFile: file exceeds 4 GiB trajectory limit: " + path);
124 data_ = ::mmap(
nullptr,
size_, PROT_READ, MAP_PRIVATE,
fd_, 0);
125 if (
data_ == MAP_FAILED) {
128 throw std::runtime_error(
"MappedFile: mmap failed: " + path);
141 file_handle_(other.file_handle_), mapping_(other.mapping_)
147 other.data_ =
nullptr;
150 other.file_handle_ = INVALID_HANDLE_VALUE;
151 other.mapping_ =
nullptr;
158 if (
this != &other) {
163 file_handle_ = other.file_handle_;
164 mapping_ = other.mapping_;
165 other.file_handle_ = INVALID_HANDLE_VALUE;
166 other.mapping_ =
nullptr;
171 other.data_ =
nullptr;
181 [[nodiscard]]
const char *
data() const noexcept {
return static_cast<const char *
>(
data_); }
184 [[nodiscard]] std::size_t
size() const noexcept {
return size_; }
187 [[nodiscard]]
const char *
end() const noexcept {
188 return static_cast<const char *
>(
data_) +
size_;
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_);
201 if (
data_ !=
nullptr) {
216 HANDLE file_handle_{INVALID_HANDLE_VALUE};
217 HANDLE mapping_{
nullptr};
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