-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathMappedFile.hpp
166 lines (142 loc) · 4.6 KB
/
MappedFile.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef MAPPEDFILE_HPP
#define MAPPEDFILE_HPP
#if defined(__linux__) || defined(__linux) || defined(linux) || \
defined(__gnu_linux__)
#define ON_LINUX
#elif defined(__APPLE__) && defined(__MACH__)
#define ON_MACOS
#elif defined(_WIN32) || defined(_WIN64)
#define ON_WINDOWS
#endif
#include <stdexcept>
#include <string>
#include <string_view>
#ifdef ON_LINUX
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#ifdef ON_WINDOWS
#include <windows.h>
#include <fileapi.h>
#include <memoryapi.h>
#include <winbase.h>
#endif
// This class is a RAII wrapper for a file mapping. Pass a string containing
// the name of the file to the constructor, and then access its contents as
// a std::string_view via getContents(). All the gnarly details of creating
// and destroying the mapping are taken care of. Portable to Windows and Linux.
class MappedFile {
public:
MappedFile() : m_content{nullptr}, m_size{0} {}
explicit inline MappedFile(const std::string &path);
MappedFile(const MappedFile &) = delete;
MappedFile &operator=(const MappedFile &) = delete;
inline MappedFile(MappedFile &&other) noexcept;
inline MappedFile &operator=(MappedFile &&other) noexcept;
~MappedFile() {
if (m_content)
dtorImpl();
}
[[nodiscard]] auto getContents() const -> std::string_view {
return {m_content, m_size};
}
private:
inline void dtorImpl() noexcept;
char *m_content;
std::size_t m_size;
#ifdef ON_WINDOWS
HANDLE m_file_handle;
HANDLE m_mapping_handle;
#endif
};
#ifdef ON_LINUX
MappedFile::MappedFile(const std::string &path) {
const int fd = open(path.c_str(), O_RDONLY | O_LARGEFILE);
if (fd == -1)
throw std::runtime_error{"Could not open the specified file"};
struct stat sb {};
if (fstat(fd, &sb))
throw std::runtime_error{"Could not obtain file info"};
m_size = static_cast<std::size_t>(sb.st_size);
void *const mapped_addr = mmap(NULL, m_size, PROT_READ, MAP_PRIVATE, fd,
0u); // NOLINT it's a C API...
if (mapped_addr == MAP_FAILED)
throw std::runtime_error{"Could not mmap file"};
m_content = static_cast<char *>(mapped_addr);
if (close(fd))
throw std::runtime_error{"Could not close file after mmap"};
}
MappedFile::MappedFile(MappedFile &&other) noexcept
: m_content{other.m_content}, m_size{other.m_size} {
other.m_content = nullptr;
}
MappedFile &MappedFile::operator=(MappedFile &&other) noexcept {
if (this != &other) {
dtorImpl();
m_content = other.m_content;
m_size = other.m_size;
other.m_content = nullptr;
}
return *this;
}
void MappedFile::dtorImpl() noexcept {
if (m_content)
munmap(m_content, m_size); // Ignore error - dtor shouldn't throw we can't
// meaningfully handle this error anyway
}
#endif
#ifdef ON_WINDOWS
MappedFile::MappedFile(const std::string &path) {
m_file_handle = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (m_file_handle == INVALID_HANDLE_VALUE)
throw std::runtime_error{"Could not open file"};
LARGE_INTEGER size{};
if (not GetFileSizeEx(m_file_handle, &size)) {
CloseHandle(m_file_handle);
throw std::runtime_error{"Could not obtain file size"};
}
m_size = size.QuadPart;
m_mapping_handle =
CreateFileMappingA(m_file_handle, NULL, PAGE_READONLY, 0, 0, NULL);
if (m_mapping_handle == NULL) {
CloseHandle(m_file_handle);
throw std::runtime_error{"Could not create file mapping"};
}
const auto map_content =
MapViewOfFile(m_mapping_handle, FILE_MAP_READ, 0, 0, 0);
if (map_content == NULL) {
CloseHandle(m_mapping_handle);
CloseHandle(m_file_handle);
throw std::runtime_error{"Could not map file into process address space"};
}
m_content = static_cast<char *>(map_content);
}
MappedFile::MappedFile(MappedFile &&other) noexcept
: m_content{other.m_content}, m_size{other.m_size},
m_file_handle{other.m_file_handle}, m_mapping_handle{
other.m_mapping_handle} {
other.m_content = nullptr;
}
MappedFile &MappedFile::operator=(MappedFile &&other) noexcept {
if (this != &other) {
dtorImpl();
m_content = other.m_content;
m_size = other.m_size;
m_file_handle = other.m_file_handle;
m_mapping_handle = other.m_mapping_handle;
other.m_content = nullptr;
}
return *this;
}
void MappedFile::dtorImpl() noexcept {
if (not m_content)
return;
UnmapViewOfFile(m_content);
CloseHandle(m_mapping_handle);
CloseHandle(m_file_handle);
}
#endif
#endif // MAPPEDFILE_HPP