-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathio.hpp
More file actions
217 lines (178 loc) · 6.14 KB
/
io.hpp
File metadata and controls
217 lines (178 loc) · 6.14 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Maximilian Kuschewski, 2023
#pragma once
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <string>
namespace p2c {
template<typename T = char>
struct FileMapping {
uintptr_t file_size;
int handle;
T *mapping;
using iterator = T *;
public:
FileMapping() : file_size(0), handle(-1), mapping(nullptr) {}
FileMapping(const std::string &filename, int flags = 0, uintptr_t size = 0) : FileMapping() {
open(filename.data(), flags, size);
}
FileMapping(uintptr_t size, int flags = 0) : FileMapping() { open(nullptr, flags, size); }
FileMapping(FileMapping &&other) { *this = std::move(other); }
~FileMapping() { close(); }
FileMapping &operator=(FileMapping &&other) {
handle = other.handle;
file_size = other.file_size;
mapping = other.mapping;
other.handle = -1;
other.file_size = 0;
other.mapping = nullptr;
return *this;
}
inline bool is_backed() { return handle != -1; }
inline void open(const char *file, int flags, std::size_t size) {
close();
int h = -1;
if (file) {
h = ::open(file, flags, 0655);
if (h < 0) {
auto err = errno;
throw std::logic_error("Could not open file " + std::string(file) + ":" +
std::string(strerror(err)));
}
if (size == 0) {
lseek(h, 0, SEEK_END);
size = lseek(h, 0, SEEK_CUR);
} else {
auto res = ::ftruncate(h, size);
if (res < 0) {
auto err = errno;
throw std::logic_error("Could not resize file: " + std::string(strerror(err)));
}
}
}
file_size = size;
auto mmap_perm = (flags == 0) ? PROT_READ : (flags & O_WRONLY) ? PROT_WRITE : PROT_READ | PROT_WRITE;
auto mmap_type = h == -1 ? MAP_PRIVATE | MAP_ANONYMOUS : MAP_SHARED;
auto m = mmap(nullptr, file_size, mmap_perm, mmap_type, h, 0);
if (m == MAP_FAILED) {
auto err = errno;
::close(h);
throw std::logic_error("Could not map file: " + std::string(strerror(err)));
}
if (file_size > 1024 * 1024) {
madvise(m, file_size, MADV_HUGEPAGE);
}
handle = h;
mapping = static_cast<T *>(m);
}
inline void close() {
if (mapping) {
::munmap(mapping, file_size);
mapping = nullptr;
}
if (handle >= 0) {
::close(handle);
handle = -1;
}
}
inline void flush() const {
if (handle >= 0) {
::fdatasync(handle);
}
}
inline T *data() const { return mapping; }
inline const iterator begin() const { return data(); }
inline const iterator end() const { return data() + (this->file_size / sizeof(T)); }
};
struct fixed_size {
static constexpr bool IS_VARIABLE = false;
};
struct variable_size {
static constexpr bool IS_VARIABLE = true;
struct StringIndexSlot {
uint64_t size;
uint64_t offset;
};
struct StringData {
uint64_t count;
StringIndexSlot slot[];
};
};
template<typename T>
struct DataColumn : FileMapping<T> {
using size_tag = fixed_size;
using iterator = T *;
using value_type = T;
static constexpr uintptr_t GLOBAL_OVERHEAD = 0;
static constexpr uintptr_t PER_ITEM_OVERHEAD = 0;
uintptr_t count = 0ul;
DataColumn() : FileMapping<T>() {}
DataColumn(const char *filename, int flags = 0, uintptr_t size = 0)
: FileMapping<T>(filename, flags, size),
count(this->file_size / sizeof(T)) {
assert(this->file_size % sizeof(T) == 0);
}
DataColumn(const std::string &filename, int flags = 0, uintptr_t size = 0)
: DataColumn(filename.data(), flags, size) {}
DataColumn(const DataColumn &) = delete;
DataColumn(DataColumn &&other) { *this = std::move(other); }
DataColumn &operator=(DataColumn &&o) {
FileMapping<T>::operator=(std::move(o));
count = o.count;
o.count = 0;
return *this;
}
uintptr_t size() const { return count; }
const T &operator[](std::size_t idx) const { return this->data()[idx]; }
};
template<>
struct DataColumn<std::string_view> : FileMapping<variable_size::StringData> {
using size_tag = variable_size;
using Data = typename variable_size::StringData;
using value_type = std::string_view;
static constexpr uintptr_t PER_ITEM_OVERHEAD = sizeof(variable_size::StringIndexSlot);
static constexpr uintptr_t GLOBAL_OVERHEAD = sizeof(uint64_t);
struct iterator {
uint64_t idx;
Data *data;
inline iterator &operator++() {
++idx;
return *this;
}
inline iterator operator++(int) {
auto prev = *this;
++idx;
return prev;
}
std::string_view operator*() {
auto slot = data->slot[idx];
return std::string_view(reinterpret_cast<char *>(data) + slot.offset, slot.size);
}
bool operator==(const iterator &rhs) { return this->idx == rhs.idx && this->data == rhs.data; }
};
DataColumn() : FileMapping<Data>() {}
DataColumn(const char *filename, int flags = 0, uintptr_t size = 0)
: FileMapping<Data>(filename, flags, size) {}
DataColumn(const std::string &filename, int flags = 0, uintptr_t size = 0)
: DataColumn(filename.data(), flags, size) {}
DataColumn(const DataColumn &other) = delete;
DataColumn(DataColumn &&other) : FileMapping<Data>(std::move(other)) {}
DataColumn &operator=(DataColumn &&o) {
FileMapping<Data>::operator=(std::move(o));
return *this;
};
Data *data() const { return this->mapping; }
uintptr_t size() const { return data()->count; }
inline const iterator begin() const { return iterator{0, data()}; }
inline const iterator end() const { return iterator{data()->count, data()}; }
inline variable_size::StringIndexSlot &slot_at(std::size_t idx) const { return data()->slot[idx]; }
inline std::string_view operator[](std::size_t idx) const {
auto slot = data()->slot[idx];
return std::string_view(reinterpret_cast<char *>(data()) + slot.offset, slot.size);
}
};
} // namespace p2c