-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMappedFile.cpp
351 lines (258 loc) · 6.16 KB
/
MappedFile.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
Copyright (c) 2013 Game Closure. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of GCIF nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "MappedFile.hpp"
using namespace cat;
#ifdef CAT_COMPILE_MMAP
#if defined(CAT_OS_LINUX) || defined(CAT_OS_OSX)
# include <sys/mman.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <errno.h>
#endif
#if defined(CAT_OS_WINDOWS)
# include "WindowsInclude.hpp"
#elif defined(CAT_OS_LINUX) || defined(CAT_OS_AIX) || defined(CAT_OS_SOLARIS) || defined(CAT_OS_IRIX)
# include <unistd.h>
#elif defined(CAT_OS_OSX) || defined(CAT_OS_BSD)
# include <sys/sysctl.h>
# include <unistd.h>
#elif defined(CAT_OS_HPUX)
# include <sys/mpctl.h>
#endif
static u32 GetAllocationGranularity()
{
u32 alloc_gran = 0;
#if defined(CAT_OS_WINDOWS)
SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info);
alloc_gran = sys_info.dwAllocationGranularity;
#elif defined(CAT_OS_OSX) || defined(CAT_OS_BSD)
alloc_gran = (u32)getpagesize();
#else
alloc_gran = (u32)sysconf(_SC_PAGE_SIZE);
#endif
return alloc_gran > 0 ? alloc_gran : CAT_DEFAULT_ALLOCATION_GRANULARITY;
}
//// MappedFile
MappedFile::MappedFile()
{
_len = 0;
#if defined(CAT_OS_WINDOWS)
_file = INVALID_HANDLE_VALUE;
#else
_file = -1;
#endif
}
MappedFile::~MappedFile()
{
Close();
}
bool MappedFile::OpenRead(const char *path, bool read_ahead, bool no_cache)
{
Close();
_readonly = true;
#if defined(CAT_OS_WINDOWS)
u32 access_pattern = !read_ahead ? FILE_FLAG_RANDOM_ACCESS : FILE_FLAG_SEQUENTIAL_SCAN;
_file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, access_pattern, 0);
if (_file == INVALID_HANDLE_VALUE)
{
return false;
}
if (!GetFileSizeEx(_file, (LARGE_INTEGER*)&_len))
{
return false;
}
#else
_file = open(path, O_RDONLY, (mode_t)0444);
if (_file == -1) {
return false;
} else {
_len = lseek(_file, 0, SEEK_END);
if (_len <= 0) {
return false;
} else {
#ifdef F_RDAHEAD
if (read_ahead) {
fcntl(_file, F_RDAHEAD, 1);
}
#endif
#ifdef F_NOCACHE
if (no_cache) {
fcntl(_file, F_NOCACHE, 1);
}
#endif
}
}
#endif
return true;
}
bool MappedFile::OpenWrite(const char *path, u64 size)
{
Close();
_readonly = false;
_len = size;
#if defined(CAT_OS_WINDOWS)
const u32 access_pattern = FILE_FLAG_SEQUENTIAL_SCAN;
_file = CreateFileA(path, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, access_pattern, 0);
if (_file == INVALID_HANDLE_VALUE)
{
return false;
}
// Set file size
if (!SetFilePointerEx(_file, *(LARGE_INTEGER*)&_len, 0, FILE_BEGIN)) {
return false;
}
if (!SetEndOfFile(_file)) {
return false;
}
#else
_file = open(path, O_RDWR|O_CREAT|O_TRUNC, (mode_t)0666);
if (_file == -1) {
return false;
} else {
if (-1 == lseek(_file, size - 1, SEEK_SET)) {
return false;
} else {
if (1 != write(_file, "", 1)) {
return false;
}
}
}
#endif
return true;
}
void MappedFile::Close()
{
#if defined(CAT_OS_WINDOWS)
if (_file != INVALID_HANDLE_VALUE)
{
CloseHandle(_file);
_file = INVALID_HANDLE_VALUE;
}
#else
if (_file != -1) {
close(_file);
_file = -1;
}
#endif
_len = 0;
}
//// MappedView
MappedView::MappedView()
{
_data = 0;
_length = 0;
_offset = 0;
#if defined(CAT_OS_WINDOWS)
_map = 0;
#else
_map = MAP_FAILED;
#endif
}
MappedView::~MappedView()
{
Close();
}
bool MappedView::Open(MappedFile *file)
{
Close();
if (!file || !file->IsValid()) return false;
_file = file;
#if defined(CAT_OS_WINDOWS)
const u32 flags = file->IsReadOnly() ? PAGE_READONLY : PAGE_READWRITE;
_map = CreateFileMapping(file->_file, 0, flags, 0, 0, 0);
if (!_map)
{
return false;
}
#endif
return true;
}
u8 *MappedView::MapView(u64 offset, u32 length)
{
if (length == 0) {
length = static_cast<u32>( _file->GetLength() );
}
if (offset) {
u32 granularity = GetAllocationGranularity();
// Bring offset back to the previous allocation granularity
u32 mask = granularity - 1;
u32 masked = (u32)offset & mask;
if (masked)
{
offset -= masked;
length += masked;
}
}
#if defined(CAT_OS_WINDOWS)
u32 flags = FILE_MAP_READ;
if (!_file->IsReadOnly()) {
flags |= FILE_MAP_WRITE;
}
_data = (u8*)MapViewOfFile(_map, flags, (u32)(offset >> 32), (u32)offset, length);
if (!_data)
{
return 0;
}
#else
int prot = PROT_READ;
if (!_file->_readonly) {
prot |= PROT_WRITE;
}
_map = mmap(0, length, prot, MAP_SHARED, _file->_file, offset);
if (_map == MAP_FAILED) {
return 0;
}
_data = reinterpret_cast<u8*>( _map );
#endif
_offset = offset;
_length = length;
return _data;
}
void MappedView::Close()
{
#if defined(CAT_OS_WINDOWS)
if (_data)
{
UnmapViewOfFile(_data);
_data = 0;
}
if (_map)
{
CloseHandle(_map);
_map = 0;
}
#else
if (_map != MAP_FAILED)
{
munmap(_map, _length);
_map = MAP_FAILED;
}
_data = 0;
#endif
_length = 0;
_offset = 0;
}
#endif // CAT_COMPILE_MMAP