@@ -88,8 +88,21 @@ struct file {
88
88
m_size = 0 ;
89
89
m_data = nullptr ;
90
90
}
91
+
92
+ void check_fd () {
93
+ if (m_fd == -1 ) throw std::runtime_error (" cannot open file" );
94
+ }
91
95
};
92
96
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
+
93
106
template <typename T>
94
107
struct file_source : public file <T const > {
95
108
typedef file<T const > base;
@@ -102,23 +115,13 @@ struct file_source : public file<T const> {
102
115
103
116
void open (std::string const & path, int adv = advice::normal) {
104
117
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 ();
109
119
struct stat fs;
110
120
if (fstat (base::m_fd, &fs) == -1 ) {
111
121
throw std::runtime_error (" cannot stat file" );
112
122
}
113
123
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);
122
125
if (posix_madvise ((void *)base::m_data, base::m_size, adv)) {
123
126
throw std::runtime_error (" madvise failed" );
124
127
}
@@ -131,28 +134,36 @@ struct file_sink : public file<T> {
131
134
132
135
file_sink () {}
133
136
137
+ file_sink (std::string const & path) {
138
+ open (path);
139
+ }
140
+
134
141
file_sink (std::string const & path, size_t n) {
135
142
open (path, n);
136
143
}
137
144
138
- void open (std::string const & path, size_t n ) {
145
+ void open (std::string const & path) {
139
146
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" );
143
152
}
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
+ }
144
157
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 ();
145
162
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
150
165
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);
156
167
}
157
168
};
158
169
0 commit comments