-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathMemoryMapped.cpp
322 lines (261 loc) · 6.01 KB
/
MemoryMapped.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// //////////////////////////////////////////////////////////
// MemoryMapped.cpp
// Copyright (c) 2013 Stephan Brumme. All rights reserved.
// see http://create.stephan-brumme.com/disclaimer.html
//
#include "MemoryMapped.h"
#include <stdexcept>
#include <cstdio>
// OS-specific
#ifdef _MSC_VER
// Windows
#include <windows.h>
#else
// Linux
// enable large file support on 32 bit systems
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#ifdef _FILE_OFFSET_BITS
#undef _FILE_OFFSET_BITS
#endif
#define _FILE_OFFSET_BITS 64
// and include needed headers
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#endif
/// do nothing, must use open()
MemoryMapped::MemoryMapped()
: _filename (),
_filesize (0),
_hint (Normal),
_mappedBytes(0),
_file (0),
#ifdef _MSC_VER
_mappedFile (NULL),
#endif
_mappedView (NULL)
{
}
/// open file, mappedBytes = 0 maps the whole file
MemoryMapped::MemoryMapped(const std::string& filename, size_t mappedBytes, CacheHint hint)
: _filename (filename),
_filesize (0),
_hint (hint),
_mappedBytes(mappedBytes),
_file (0),
#ifdef _MSC_VER
_mappedFile (NULL),
#endif
_mappedView (NULL)
{
open(filename, mappedBytes, hint);
}
/// close file (see close() )
MemoryMapped::~MemoryMapped()
{
close();
}
/// open file
bool MemoryMapped::open(const std::string& filename, size_t mappedBytes, CacheHint hint)
{
// already open ?
if (isValid())
return false;
_file = 0;
_filesize = 0;
_hint = hint;
#ifdef _MSC_VER
_mappedFile = NULL;
#endif
_mappedView = NULL;
#ifdef _MSC_VER
// Windows
DWORD winHint = 0;
switch (_hint)
{
case Normal: winHint = FILE_ATTRIBUTE_NORMAL; break;
case SequentialScan: winHint = FILE_FLAG_SEQUENTIAL_SCAN; break;
case RandomAccess: winHint = FILE_FLAG_RANDOM_ACCESS; break;
default: break;
}
// open file
_file = ::CreateFileA(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, winHint, NULL);
if (!_file)
return false;
// file size
LARGE_INTEGER result;
if (!GetFileSizeEx(_file, &result))
return false;
_filesize = static_cast<uint64_t>(result.QuadPart);
// convert to mapped mode
_mappedFile = ::CreateFileMapping(_file, NULL, PAGE_READONLY, 0, 0, NULL);
if (!_mappedFile)
return false;
#else
// Linux
// open file
_file = ::open(filename.c_str(), O_RDONLY | O_LARGEFILE);
if (_file == -1)
{
_file = 0;
return false;
}
// file size
struct stat64 statInfo;
if (fstat64(_file, &statInfo) < 0)
return false;
_filesize = statInfo.st_size;
#endif
// initial mapping
remap(0, mappedBytes);
if (!_mappedView)
return false;
// everything's fine
return true;
}
/// close file
void MemoryMapped::close()
{
// kill pointer
if (_mappedView)
{
#ifdef _MSC_VER
::UnmapViewOfFile(_mappedView);
#else
::munmap(_mappedView, _filesize);
#endif
_mappedView = NULL;
}
#ifdef _MSC_VER
if (_mappedFile)
{
::CloseHandle(_mappedFile);
_mappedFile = NULL;
}
#endif
// close underlying file
if (_file)
{
#ifdef _MSC_VER
::CloseHandle(_file);
#else
::close(_file);
#endif
_file = 0;
}
_filesize = 0;
}
/// access position, no range checking (faster)
unsigned char MemoryMapped::operator[](size_t offset) const
{
return ((unsigned char*)_mappedView)[offset];
}
/// access position, including range checking
unsigned char MemoryMapped::at(size_t offset) const
{
// checks
if (!_mappedView)
throw std::invalid_argument("No view mapped");
if (offset >= _filesize)
throw std::out_of_range("View is not large enough");
return operator[](offset);
}
/// raw access
const unsigned char* MemoryMapped::getData() const
{
return (const unsigned char*)_mappedView;
}
/// true, if file successfully opened
bool MemoryMapped::isValid() const
{
return _mappedView != NULL;
}
/// get file size
uint64_t MemoryMapped::size() const
{
return _filesize;
}
/// get number of actually mapped bytes
size_t MemoryMapped::mappedSize() const
{
return _mappedBytes;
}
/// replace mapping by a new one of the same file, offset MUST be a multiple of the page size
bool MemoryMapped::remap(uint64_t offset, size_t mappedBytes)
{
if (!_file)
return false;
if (mappedBytes == WholeFile)
mappedBytes = _filesize;
// close old mapping
if (_mappedView)
{
#ifdef _MSC_VER
::UnmapViewOfFile(_mappedView);
#else
::munmap(_mappedView, _mappedBytes);
#endif
_mappedView = NULL;
}
// don't go further than end of file
if (offset > _filesize)
return false;
if (offset + mappedBytes > _filesize)
mappedBytes = size_t(_filesize - offset);
#ifdef _MSC_VER
// Windows
DWORD offsetLow = DWORD(offset & 0xFFFFFFFF);
DWORD offsetHigh = DWORD(offset >> 32);
_mappedBytes = mappedBytes;
// get memory address
_mappedView = ::MapViewOfFile(_mappedFile, FILE_MAP_READ, offsetHigh, offsetLow, mappedBytes);
if (_mappedView == NULL)
{
_mappedBytes = 0;
_mappedView = NULL;
return false;
}
return true;
#else
// Linux
// new mapping
_mappedView = ::mmap64(NULL, mappedBytes, PROT_READ, MAP_SHARED, _file, offset);
if (_mappedView == MAP_FAILED)
{
_mappedBytes = 0;
_mappedView = NULL;
return false;
}
_mappedBytes = mappedBytes;
// tweak performance
int linuxHint = 0;
switch (_hint)
{
case Normal: linuxHint = MADV_NORMAL; break;
case SequentialScan: linuxHint = MADV_SEQUENTIAL; break;
case RandomAccess: linuxHint = MADV_RANDOM; break;
default: break;
}
// assume that file will be accessed soon
//linuxHint |= MADV_WILLNEED;
// assume that file will be large
//linuxHint |= MADV_HUGEPAGE;
::madvise(_mappedView, _mappedBytes, linuxHint);
return true;
#endif
}
/// get OS page size (for remap)
int MemoryMapped::getpagesize()
{
#ifdef _MSC_VER
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
return sysInfo.dwAllocationGranularity;
#else
return sysconf(_SC_PAGESIZE); //::getpagesize();
#endif
}