Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
mapped_file.cpp
Go to the documentation of this file.
2#include <filesystem>
3#include <stdexcept>
4#include <system_error>
5#include <utility>
6
7#if defined(_WIN32) || defined(_WIN64)
8#ifndef WIN32_LEAN_AND_MEAN
9#define WIN32_LEAN_AND_MEAN
10#endif
11#ifndef NOMINMAX
12#define NOMINMAX
13#endif
14#include <windows.h>
15#else
16#include <cerrno>
17#include <cstring>
18#include <fcntl.h>
19#include <sys/mman.h>
20#include <sys/stat.h>
21#include <unistd.h>
22#endif
23
24namespace slick::trader {
25
26namespace {
27
28[[noreturn]] void fail(const std::string& path, const char* what, long long code) {
29 throw std::runtime_error("MappedFile: cannot " + std::string(what) + " " + path + " (error " +
30 std::to_string(code) + ")");
31}
32
33} // namespace
34
35MappedFile::MappedFile(const std::string& path, std::size_t size) {
36 if (size == 0) {
37 throw std::runtime_error("MappedFile: " + path + " needs a size");
38 }
39 const std::filesystem::path file_path(path);
40 if (file_path.has_parent_path()) {
41 std::error_code ec;
42 std::filesystem::create_directories(file_path.parent_path(), ec); // open() reports a failure
43 }
44
45#if defined(_WIN32) || defined(_WIN64)
46 HANDLE file = CreateFileW(file_path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS,
47 FILE_ATTRIBUTE_NORMAL, nullptr);
48 if (file == INVALID_HANDLE_VALUE) {
49 fail(path, "open", GetLastError());
50 }
51 created_ = GetLastError() != ERROR_ALREADY_EXISTS;
52
53 LARGE_INTEGER existing{};
54 if (!GetFileSizeEx(file, &existing)) {
55 const DWORD error = GetLastError();
56 CloseHandle(file);
57 fail(path, "size", error);
58 }
59 size_ = static_cast<std::uint64_t>(existing.QuadPart) > size ? static_cast<std::size_t>(existing.QuadPart) : size;
60
61 // A mapping larger than the file extends it, with zeros.
62 const auto wide = static_cast<std::uint64_t>(size_);
63 HANDLE mapping = CreateFileMappingW(file, nullptr, PAGE_READWRITE, static_cast<DWORD>(wide >> 32),
64 static_cast<DWORD>(wide & 0xFFFFFFFFu), nullptr);
65 if (!mapping) {
66 const DWORD error = GetLastError();
67 CloseHandle(file);
68 fail(path, "map", error);
69 }
70 void* view = MapViewOfFile(mapping, FILE_MAP_ALL_ACCESS, 0, 0, size_);
71 if (!view) {
72 const DWORD error = GetLastError();
73 CloseHandle(mapping);
74 CloseHandle(file);
75 fail(path, "map", error);
76 }
77 file_ = file;
78 mapping_ = mapping;
79 data_ = static_cast<std::uint8_t*>(view);
80#else
81 int fd = ::open(path.c_str(), O_RDWR | O_CREAT | O_EXCL, 0644);
82 created_ = fd >= 0;
83 if (fd < 0 && errno == EEXIST) {
84 fd = ::open(path.c_str(), O_RDWR);
85 }
86 if (fd < 0) {
87 fail(path, "open", errno);
88 }
89 struct stat info {};
90 if (::fstat(fd, &info) != 0) {
91 const int error = errno;
92 ::close(fd);
93 fail(path, "size", error);
94 }
95 size_ = static_cast<std::size_t>(info.st_size) > size ? static_cast<std::size_t>(info.st_size) : size;
96 if (static_cast<std::size_t>(info.st_size) < size_ && ::ftruncate(fd, static_cast<off_t>(size_)) != 0) {
97 const int error = errno;
98 ::close(fd);
99 fail(path, "grow", error);
100 }
101 void* view = ::mmap(nullptr, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
102 if (view == MAP_FAILED) {
103 const int error = errno;
104 ::close(fd);
105 fail(path, "map", error);
106 }
107 fd_ = fd;
108 data_ = static_cast<std::uint8_t*>(view);
109#endif
110}
111
115
117 *this = std::move(other);
118}
119
121 if (this != &other) {
122 close();
123 data_ = std::exchange(other.data_, nullptr);
124 size_ = std::exchange(other.size_, 0);
125 created_ = std::exchange(other.created_, false);
126#if defined(_WIN32) || defined(_WIN64)
127 file_ = std::exchange(other.file_, nullptr);
128 mapping_ = std::exchange(other.mapping_, nullptr);
129#else
130 fd_ = std::exchange(other.fd_, -1);
131#endif
132 }
133 return *this;
134}
135
136void MappedFile::flush() noexcept {
137 if (!data_) {
138 return;
139 }
140#if defined(_WIN32) || defined(_WIN64)
141 FlushViewOfFile(data_, size_);
142 FlushFileBuffers(static_cast<HANDLE>(file_));
143#else
144 ::msync(data_, size_, MS_SYNC);
145#endif
146}
147
148void MappedFile::close() noexcept {
149 if (!data_) {
150 return;
151 }
152#if defined(_WIN32) || defined(_WIN64)
153 UnmapViewOfFile(data_);
154 CloseHandle(static_cast<HANDLE>(mapping_));
155 CloseHandle(static_cast<HANDLE>(file_));
156 mapping_ = nullptr;
157 file_ = nullptr;
158#else
159 ::munmap(data_, size_);
160 ::close(fd_);
161 fd_ = -1;
162#endif
163 data_ = nullptr;
164 size_ = 0;
165}
166
167} // namespace slick::trader
std::size_t size() const noexcept
MappedFile & operator=(MappedFile &&other) noexcept