Repository navigation
Expand file tree
/
Copy pathmmappedfile.h
More file actions
119 lines (102 loc) · 4.1 KB
/
Copy pathmmappedfile.h
File metadata and controls
119 lines (102 loc) · 4.1 KB
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
/*
Copyright (c) 2017-2019 Sven Willner <sven.willner@gmail.com>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef MMAPPED_FILE_H
#define MMAPPED_FILE_H
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <iomanip>
#include <string>
#include <vector>
template<typename T>
inline T* open_mmapped_file(const std::string& filename, std::size_t size, char mode, int& fd) {
T* res = nullptr;
switch (mode) {
case 'r': {
fd = open(filename.c_str(), O_RDONLY | O_CLOEXEC); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg,hicpp-signed-bitwise)
if (fd < 0) {
throw std::runtime_error("could not open file " + filename);
}
res = static_cast<T*>(mmap(nullptr, size * sizeof(T), PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0)); // NOLINT(hicpp-signed-bitwise)
} break;
case 'w': {
fd = open(filename.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg,hicpp-signed-bitwise)
static_cast<mode_t>(0600));
if (fd < 0) {
throw std::runtime_error("could not create file " + filename);
}
int rc = lseek(fd, size * sizeof(T) - 1, SEEK_SET);
if (rc < 0) {
throw std::runtime_error("lseek failed");
}
rc = write(fd, "", 1);
if (rc < 0) {
throw std::runtime_error("write failed");
}
res = static_cast<T*>(mmap(nullptr, size * sizeof(T), PROT_WRITE, MAP_SHARED, fd, 0));
} break;
default:
throw std::runtime_error("unknown file mode");
}
if (res == MAP_FAILED) { // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
throw std::runtime_error("mmap failed");
}
int rc = madvise(res, size * sizeof(T), MADV_WILLNEED | MADV_SEQUENTIAL); // NOLINT(hicpp-signed-bitwise)
if (rc < 0) {
delete[] res;
throw std::runtime_error("madvice failed");
}
return res;
}
template<typename T>
inline void close_mmapped_file(T* p, std::size_t size, int fd) {
munmap(p, size * sizeof(T));
close(fd);
}
template<typename T = char>
class MMappedFile {
protected:
int fd = 0;
std::unique_ptr<T[]> data;
std::size_t size_m = 0;
public:
MMappedFile() = default;
MMappedFile(const MMappedFile&) = delete;
MMappedFile(MMappedFile&&) noexcept = default;
void open(const std::string& filename, std::size_t size_p, char mode = '\r') {
close();
data.reset(open_mmapped_file<T>(filename, size_p, mode, fd));
size_m = size_p;
}
void close() {
if (data) {
close_mmapped_file<T>(data.get(), size_m, fd);
data.release();
size_m = 0;
}
}
~MMappedFile() { close(); }
constexpr operator bool() const { return data; } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
constexpr T* get() { return data.get(); }
constexpr T* get() const { return data.get(); }
constexpr std::size_t size() const { return size_m; }
};
#endif