Skip to content

Commit d08e850

Browse files
committed
mmap existing file for read/write
1 parent e2fc3ae commit d08e850

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ AllowAllParametersOfDeclarationOnNextLine: true
1212
AllowShortBlocksOnASingleLine: false
1313
AllowShortCaseLabelsOnASingleLine: false
1414
AllowShortFunctionsOnASingleLine: Empty
15-
AllowShortIfStatementsOnASingleLine: false
15+
AllowShortIfStatementsOnASingleLine: true
1616
AllowShortLoopsOnASingleLine: true
1717
AlwaysBreakAfterDefinitionReturnType: None
1818
AlwaysBreakAfterReturnType: None

include/mm_file/mm_file.hpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,21 @@ struct file {
8888
m_size = 0;
8989
m_data = nullptr;
9090
}
91+
92+
void check_fd() {
93+
if (m_fd == -1) throw std::runtime_error("cannot open file");
94+
}
9195
};
9296

97+
template <typename Pointer>
98+
Pointer mmap(int fd, size_t size, int prot) {
99+
static const size_t offset = 0;
100+
Pointer p =
101+
static_cast<Pointer>(::mmap(NULL, size, prot, MAP_SHARED, fd, offset));
102+
if (p == MAP_FAILED) throw std::runtime_error("mmap failed");
103+
return p;
104+
}
105+
93106
template <typename T>
94107
struct file_source : public file<T const> {
95108
typedef file<T const> base;
@@ -102,23 +115,13 @@ struct file_source : public file<T const> {
102115

103116
void open(std::string const& path, int adv = advice::normal) {
104117
base::m_fd = ::open(path.c_str(), O_RDONLY);
105-
if (base::m_fd == -1) {
106-
throw std::runtime_error("cannot open file");
107-
}
108-
118+
base::check_fd();
109119
struct stat fs;
110120
if (fstat(base::m_fd, &fs) == -1) {
111121
throw std::runtime_error("cannot stat file");
112122
}
113123
base::m_size = fs.st_size;
114-
115-
// map entire file starting from the beginning (offset 0)
116-
base::m_data = static_cast<T const*>(
117-
mmap(NULL, base::m_size, PROT_READ, MAP_SHARED, base::m_fd, 0));
118-
if (base::m_data == MAP_FAILED) {
119-
throw std::runtime_error("mmap failed");
120-
}
121-
124+
base::m_data = mmap<T const*>(base::m_fd, base::m_size, PROT_READ);
122125
if (posix_madvise((void*)base::m_data, base::m_size, adv)) {
123126
throw std::runtime_error("madvise failed");
124127
}
@@ -131,28 +134,36 @@ struct file_sink : public file<T> {
131134

132135
file_sink() {}
133136

137+
file_sink(std::string const& path) {
138+
open(path);
139+
}
140+
134141
file_sink(std::string const& path, size_t n) {
135142
open(path, n);
136143
}
137144

138-
void open(std::string const& path, size_t n) {
145+
void open(std::string const& path) {
139146
static const mode_t mode = 0600; // read/write
140-
base::m_fd = ::open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC, mode);
141-
if (base::m_fd == -1) {
142-
throw std::runtime_error("cannot open file");
147+
base::m_fd = ::open(path.c_str(), O_RDWR, mode);
148+
base::check_fd();
149+
struct stat fs;
150+
if (fstat(base::m_fd, &fs) == -1) {
151+
throw std::runtime_error("cannot stat file");
143152
}
153+
base::m_size = fs.st_size;
154+
base::m_data =
155+
mmap<T*>(base::m_fd, base::m_size, PROT_READ | PROT_WRITE);
156+
}
144157

158+
void open(std::string const& path, size_t n) {
159+
static const mode_t mode = 0600; // read/write
160+
base::m_fd = ::open(path.c_str(), O_RDWR | O_CREAT | O_TRUNC, mode);
161+
base::check_fd();
145162
base::m_size = n * sizeof(T);
146-
// truncate the file at the new size
147-
ftruncate(base::m_fd, base::m_size);
148-
149-
// map [m_size] bytes starting from the beginning (offset 0)
163+
ftruncate(base::m_fd,
164+
base::m_size); // truncate the file at the new size
150165
base::m_data =
151-
static_cast<T*>(mmap(NULL, base::m_size, PROT_READ | PROT_WRITE,
152-
MAP_SHARED, base::m_fd, 0));
153-
if (base::m_data == MAP_FAILED) {
154-
throw std::runtime_error("mmap failed");
155-
}
166+
mmap<T*>(base::m_fd, base::m_size, PROT_READ | PROT_WRITE);
156167
}
157168
};
158169

0 commit comments

Comments
 (0)