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
|
// mmap.cpp
#include "stdafx.h"
#include "mmap.h"
set<MemoryMappedFile*> mmfiles;
MemoryMappedFile::~MemoryMappedFile() {
close();
mmfiles.erase(this);
}
/*static*/
int closingAllFiles = 0;
void MemoryMappedFile::closeAllFiles() {
if( closingAllFiles ) {
cout << "warning closingAllFiles=" << closingAllFiles << endl;
return;
}
++closingAllFiles;
for( set<MemoryMappedFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ )
(*i)->close();
cout << " closeAllFiles() finished" << endl;
--closingAllFiles;
}
#if defined(_WIN32)
#include "windows.h"
MemoryMappedFile::MemoryMappedFile() {
fd = 0; maphandle = 0; view = 0;
mmfiles.insert(this);
}
void MemoryMappedFile::close() {
if( view )
UnmapViewOfFile(view);
view = 0;
if( maphandle )
CloseHandle(maphandle);
maphandle = 0;
if( fd )
CloseHandle(fd);
fd = 0;
}
std::wstring toWideString(const char *s) {
std::basic_ostringstream<TCHAR> buf;
buf << s;
return buf.str();
}
unsigned mapped = 0;
void* MemoryMappedFile::map(const char *filename, int length) {
std::wstring filenamew = toWideString(filename);
fd = CreateFile(
filenamew.c_str(), GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if( fd == INVALID_HANDLE_VALUE ) {
cout << "CreateFile failed " << filename << endl;
return 0;
}
#if defined(_WIN32)
if( mapped > 500000000 ) {
cout << "WARNING: too much mem mapped for win32" << endl;
// if( length > 50000000 )
// length = 50000000;
}
mapped += length;
#endif
maphandle = CreateFileMapping(fd, NULL, PAGE_READWRITE, 0, length, NULL);
if( maphandle == NULL ) {
cout << "CreateFileMapping failed " << filename << endl;
return 0;
}
view = MapViewOfFile(maphandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if( view == 0 )
cout << "MapViewOfFile failed " << filename << " errno:" << GetLastError() << endl;
return view;
}
void MemoryMappedFile::flush(bool) {
}
#else
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
MemoryMappedFile::MemoryMappedFile() {
fd = 0; maphandle = 0; view = 0; len = 0;
mmfiles.insert(this);
}
void MemoryMappedFile::close() {
if( view )
munmap(view, len);
view = 0;
if( fd )
::close(fd);
fd = 0;
}
#ifndef O_NOATIME
#warning NO O_NOATIME
#define O_NOATIME 0
#endif
void* MemoryMappedFile::map(const char *filename, int length) {
len = length;
fd = open(filename, O_CREAT | O_RDWR | O_NOATIME, S_IRUSR | S_IWUSR);
if( !fd ) {
cout << "couldn't open " << filename << ' ' << errno << endl;
return 0;
}
/* make sure the file is the full desired length */
off_t filelen = lseek(fd, 0, SEEK_END);
if( filelen < length ) {
cout << "map: file length=" << filelen << " want:" << length << endl;
if( filelen != 0 ) {
cout << " failing mapping" << endl;
return 0;
}
cout << " writing file to full length with zeroes..." << endl;
int z = 8192;
char buf[z];
memset(buf, 0, z);
int left = length;
while( 1 ) {
if( left <= z ) {
write(fd, buf, left);
break;
}
write(fd, buf, z);
left -= z;
}
cout << " done" << endl;
}
lseek(fd, length, SEEK_SET);
write(fd, "", 1);
view = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if( view == MAP_FAILED ) {
cout << " mmap() failed for " << filename << " len:" << length << " errno:" << errno << endl;
return 0;
}
return view;
}
void MemoryMappedFile::flush(bool sync) {
if( msync(view, len, sync ? MS_SYNC : MS_ASYNC) )
cout << "msync error " << errno << endl;
}
#endif
|