-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathsc_mmap.c
360 lines (286 loc) · 6.96 KB
/
sc_mmap.c
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
351
352
353
354
355
356
357
358
359
360
/*
* BSD-3-Clause
*
* Copyright 2021 Ozan Tezcan
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the copyright holder 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.
*/
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#include "sc_mmap.h"
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#if defined(_WIN32)
#include <io.h>
#include <stdint.h>
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
static void sc_mmap_errstr(struct sc_mmap *m)
{
int rc;
DWORD err = GetLastError();
LPSTR errstr = 0;
rc = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err, 0, (LPSTR) &errstr, 0, NULL);
if (rc != 0) {
strncpy(m->err, errstr, sizeof(m->err) - 1);
LocalFree(errstr);
}
}
int sc_mmap_init(struct sc_mmap *m, const char *name, int file_flags, int prot,
int map_flags, size_t offset, size_t len)
{
const int mode = prot & PROT_WRITE ? _S_IREAD | _S_IWRITE : _S_IREAD;
struct _stat64 st;
int fd, rc, saved_err = 0;
void *p = NULL;
*m = (struct sc_mmap){
.ptr = NULL,
.fd = -1,
.len = 0,
};
fd = _open(name, file_flags, mode);
if (fd == -1) {
goto error;
}
rc = _stat64(name, &st);
if (rc != 0) {
goto cleanup_fd;
}
len = (len == 0) ? (size_t) st.st_size - offset : len;
HANDLE fm, h = INVALID_HANDLE_VALUE;
const size_t max_size = offset + len;
const DWORD offset_low = (offset & 0xFFFFFFFFL);
const DWORD offset_high = ((uint64_t) offset >> 32) & 0xFFFFFFFFL;
const DWORD size_low = (max_size & 0xFFFFFFFFL);
const DWORD size_high = ((uint64_t) max_size >> 32) & 0xFFFFFFFFL;
const DWORD protect = (prot & PROT_WRITE) ? PAGE_READWRITE :
PAGE_READONLY;
if ((map_flags & MAP_ANONYMOUS) == 0) {
h = (HANDLE) _get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
goto cleanup_fd;
}
}
fm = CreateFileMapping(h, NULL, protect, size_high, size_low, NULL);
if (fm == NULL) {
goto cleanup_fd;
}
p = MapViewOfFile(fm, prot, offset_high, offset_low, len);
CloseHandle(fm);
if (p == NULL) {
goto cleanup_fd;
}
m->fd = fd;
m->ptr = p;
m->len = len;
return 0;
cleanup_fd:
saved_err = GetLastError();
_close(fd);
SetLastError(saved_err);
error:
sc_mmap_errstr(m);
return -1;
}
int sc_mmap_msync(struct sc_mmap *m, size_t offset, size_t len)
{
BOOL b;
char *p = (char *) m->ptr + offset;
b = FlushViewOfFile(p, len);
if (b == 0) {
sc_mmap_errstr(m);
return -1;
}
return 0;
}
int sc_mmap_mlock(struct sc_mmap *m, size_t offset, size_t len)
{
BOOL b;
char *p = (char *) m->ptr + offset;
b = VirtualLock((LPVOID) p, len);
if (b == 0) {
sc_mmap_errstr(m);
return -1;
}
return 0;
}
int sc_mmap_munlock(struct sc_mmap *m, size_t offset, size_t len)
{
BOOL b;
char *p = (char *) m->ptr + offset;
b = VirtualUnlock((LPVOID) p, len);
if (b == 0) {
sc_mmap_errstr(m);
return -1;
}
return 0;
}
int sc_mmap_term(struct sc_mmap *m)
{
BOOL b;
int rc = 0;
if (m->fd == -1) {
return 0;
}
_close(m->fd);
b = UnmapViewOfFile(m->ptr);
if (b == 0) {
sc_mmap_errstr(m);
rc = -1;
}
m->fd = -1;
m->ptr = NULL;
m->len = 0;
return rc;
}
#else
#include <unistd.h>
int sc_mmap_init(struct sc_mmap *m, const char *name, int file_flags, int prot,
int map_flags, size_t offset, size_t len)
{
const int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
int fd, rc, saved_errno;
long page_size;
void *p = NULL;
struct stat st;
*m = (struct sc_mmap){
.ptr = NULL,
.fd = -1,
.len = 0,
};
page_size = sysconf(_SC_PAGESIZE);
if (page_size < 0) {
goto error;
}
m->page_size = page_size;
fd = open(name, file_flags, mode);
if (fd == -1) {
goto error;
}
rc = stat(name, &st);
if (rc != 0) {
goto cleanup_fd;
}
len = (len == 0) ? st.st_size - offset : len;
if (prot & PROT_WRITE) {
#if defined(__APPLE__)
int block = st.st_blksize;
size_t pos = (st.st_size / block) * block + block - 1;
for (; pos < len + block - 1; pos += block) {
if (pos >= len) {
pos = len - 1;
}
ssize_t seek = lseek(fd, pos, SEEK_SET);
if (seek == -1) {
goto cleanup_fd;
}
ssize_t written = write(fd, "", 1);
if (written != 1) {
goto cleanup_fd;
}
}
#else
do {
rc = posix_fallocate(fd, (off_t) offset, (off_t) len);
} while (rc == EINTR);
if (rc != 0) {
errno = rc;
goto cleanup_fd;
}
#endif
}
p = mmap(NULL, len, prot, map_flags, fd, (off_t) offset);
if (p == MAP_FAILED) {
goto cleanup_fd;
}
m->fd = fd;
m->ptr = p;
m->len = len;
return 0;
cleanup_fd:
saved_errno = errno;
close(fd);
errno = saved_errno;
error:
strncpy(m->err, strerror(errno), sizeof(m->err) - 1);
return -1;
}
int sc_mmap_term(struct sc_mmap *m)
{
int rc;
if (m->fd == -1) {
return 0;
}
close(m->fd);
rc = munmap(m->ptr, m->len);
if (rc != 0) {
strncpy(m->err, strerror(errno), sizeof(m->err) - 1);
}
m->fd = -1;
m->ptr = NULL;
m->len = 0;
return rc;
}
int sc_mmap_msync(struct sc_mmap *m, size_t offset, size_t len)
{
int rc;
char *p = (char *) m->ptr + (offset & ~(m->page_size - 1));
rc = msync(p, len, MS_SYNC);
if (rc != 0) {
strncpy(m->err, strerror(errno), sizeof(m->err) - 1);
}
return rc;
}
int sc_mmap_mlock(struct sc_mmap *m, size_t offset, size_t len)
{
int rc;
char *p = (char *) m->ptr + offset;
rc = mlock(p, len);
if (rc != 0) {
strncpy(m->err, strerror(errno), sizeof(m->err) - 1);
}
return rc;
}
int sc_mmap_munlock(struct sc_mmap *m, size_t offset, size_t len)
{
int rc;
char *p = (char *) m->ptr + offset;
rc = munlock(p, len);
if (rc != 0) {
strncpy(m->err, strerror(errno), sizeof(m->err) - 1);
}
return rc;
}
const char *sc_mmap_err(struct sc_mmap *m)
{
return m->err;
}
#endif