-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemorymap.cc
More file actions
277 lines (241 loc) · 6.39 KB
/
memorymap.cc
File metadata and controls
277 lines (241 loc) · 6.39 KB
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
/* -*- mode: C++; c-basic-offset: 4; tab-width: 8; -*-
* vi: set shiftwidth=4 tabstop=8:
* :indentSize=4:tabSize=8:
*/
/* Copyright (c) 2004..2008 Dirk Jagdmann <doj@cubic.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution. */
// TODO:
// - filelen on Win32
// - readwrite support for Win32
// - split this file into unix/ and win/ directory
#if defined(__APPLE__) && !defined(__unix__)
#define __unix__ 1
#endif
#ifdef __unix__
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#endif
#ifdef _WIN32
#pragma warning( disable : 4786 )
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "to_wide.h"
#endif
#include <iostream>
#include <map>
#include "memorymap.h"
using namespace std;
namespace doj
{
#ifdef __unix__
/**
data needed to handle a memory map on linux systems.
*/
struct mmap_info {
int fh;
uint64_t filelen;
string filename;
bool readonly;
};
#endif
#ifdef _WIN32
/**
data needed to handle a memory map on WIN32 systems.
*/
struct mmap_info {
HANDLE file;
HANDLE map;
uint64_t filelen;
string filename;
};
#endif
typedef map<void*, struct mmap_info> memorymap_registry_t; ///< registry type
/**
the memorymap registry contains all active memory maps and the
needed data. This information is needed for unmapping and to
retrieve the map size via the memorymapsize(...) function.
*/
static memorymap_registry_t memorymap_registry;
#ifdef __unix__
//lint -esym(952,filename)
void* memorymap(const char* filename, const bool readonly_)
{
struct mmap_info info;
info.filename=filename;
info.readonly=readonly_;
// get file size
struct stat buf;
if(stat(filename, &buf) < 0)
{
#ifdef DOJDEBUG
cerr << "memorymap(): could not fstat " << filename << " : " << strerror(errno) << endl;
#endif
return 0;
}
if(!S_ISREG(buf.st_mode))
{
#ifdef DOJDEBUG
cerr << "memorymap(): " << filename << " is not a regular file" << endl;
#endif
return 0;
}
info.filelen=static_cast<uint64_t>(buf.st_size);
// open file
info.fh=::open(filename, info.readonly?O_RDONLY:O_RDWR);
if(info.fh<0)
{
#ifdef DOJDEBUG
cerr << "memorymap(): could not open " << filename << " : " << strerror(errno) << endl;
#endif
return 0;
}
// memory map file
//lint -esym(953,w) w should be non const
void *w=mmap(NULL, static_cast<unsigned>(info.filelen), info.readonly?PROT_READ:(PROT_READ|PROT_WRITE), info.readonly?MAP_PRIVATE:MAP_SHARED, info.fh, static_cast<off_t>(0));
if(!w)
{
const int e=errno;
#ifdef DOJDEBUG
cerr << "memorymap(): could not mmap " << filename << " : " << strerror(errno) << endl;
#endif
::close(info.fh);
errno=e;
return 0;
}
// make an entry into registry
memorymap_registry[w]=info;
return w;
}
int memoryunmap(void *mem)
{
if(memorymap_registry.count(mem))
{
struct mmap_info info=memorymap_registry[mem];
//lint -e{534} ignore return value
memorymap_registry.erase(mem);
if(!info.readonly)
{
if(msync(mem, static_cast<unsigned>(info.filelen), 0) < 0)
{
#ifdef DOJDEBUG
clog << "memoryunmap(): could not msync()" << endl;
#endif
}
}
if(munmap(mem, static_cast<unsigned>(info.filelen)) < 0)
{
#ifdef DOJDEBUG
cerr << "memoryunmap(): could not unmap " << info.filename << " : " << strerror(errno) << endl;
#endif
}
if(::close(info.fh) < 0)
{
#ifdef DOJDEBUG
cerr << "memoryunmap(): could not close " << info.filename << " : " << strerror(errno) << endl;
#endif
}
return 0;
}
#ifdef DOJDEBUG
cerr << "memoryunmap(): pointer not found in registry" << endl;
#endif
return -1;
}
#endif // __unix__
#ifdef _WIN32
void* memorymap(const char* filename, const bool readonly)
{
struct mmap_info info;
info.filename=filename;
std::wstring fn = to_wide(filename);
info.file = CreateFile(fn.c_str(),
readonly ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE),
FILE_SHARE_READ,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
0);
if(info.file==INVALID_HANDLE_VALUE)
{
#ifdef DOJDEBUG
cerr << " could not open " << GetLastError() << endl;
#endif
return 0;
}
LARGE_INTEGER li;
if (!GetFileSizeEx(info.file, &li)) {
#ifdef DOJDEBUG
cerr << " could not determine file size " << GetLastError() << endl;
#endif
CloseHandle(info.file);
return 0;
}
info.filelen = li.QuadPart;
info.map = CreateFileMapping(info.file,
NULL,
readonly ? PAGE_READONLY : PAGE_READWRITE,
0,
0,
nullptr);
if(info.map==INVALID_HANDLE_VALUE || info.map == 0)
{
#ifdef DOJDEBUG
cerr << " could not create map " << GetLastError() << endl;
#endif
CloseHandle(info.file);
return 0;
}
void *mem = MapViewOfFile(info.map, readonly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, 0);
if(!mem)
{
#ifdef DOJDEBUG
cerr << " could not map file " << GetLastError() << endl;
#endif
CloseHandle(info.map);
CloseHandle(info.file);
return 0;
}
memorymap_registry[mem]=info;
return mem;
}
int memoryunmap(void *mem)
{
if(memorymap_registry.count(mem))
{
struct mmap_info info=memorymap_registry[mem];
memorymap_registry.erase(mem);
UnmapViewOfFile(mem);
CloseHandle(info.map);
CloseHandle(info.file);
return 0;
}
#ifdef DOJDEBUG
cerr << "memoryunmap(): pointer not found in registry" << endl;
#endif
return -1;
}
#endif // _WIN32
uint64_t memorymapsize(void *mem)
{
return (memorymap_registry.count(mem)) ? memorymap_registry[mem].filelen : 0;
}
}