Debian Codegraph @

lib/memory_map.c

raw symbols SCIP
1 /*
2 Copyright (C) 2025 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn.h"
20
21 #include "grn_ctx.h"
22 #include "grn_error.h"
23
24 #include <stdint.h>
25 #include <stdio.h>
26
27 #ifdef _WIN32
28 # include <windows.h>
29 #else
30 # include <fcntl.h>
31 # include <sys/mman.h>
32 # include <sys/stat.h>
33 # include <sys/types.h>
34 #endif
35
36 struct grn_memory_map {
37 char *path;
38 void *address;
39 uint64_t offset;
40 size_t length;
41 #ifdef _WIN32
42 HANDLE file;
43 HANDLE mapping;
44 #else
45 int fd;
46 #endif
47 };
48
49 #ifdef _WIN32
50 /* TODO: Not tested. */
51 grn_memory_map *
52 grn_memory_map_open(grn_ctx *ctx,
53 const char *path,
54 grn_memory_map_flags flags,
55 uint64_t offset,
56 size_t length)
57 {
58 const char *tag = "[memory-map][open]";
59
60 grn_memory_map *map = GRN_MALLOC(sizeof(grn_memory_map));
61 if (!map) {
62 char message[GRN_CTX_MSGSIZE];
63 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
64 ERR(GRN_NO_MEMORY_AVAILABLE,
65 "%s failed to allocate: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U
66 "): %s",
67 tag,
68 path,
69 length,
70 offset,
71 message);
72 GRN_API_RETURN(NULL);
73 }
74
75 map->offset = offset;
76 map->length = length;
77
78 {
79 /* CreateFileMapping() doesn't provide write only mode. We always
80 * need read access. */
81 DWORD dwDesiredAccess = GENERIC_READ;
82 if (flags & GRN_MEMORY_MAP_WRITE) {
83 dwDesiredAccess |= GENERIC_WRITE;
84 }
85 DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
86 DWORD dwCreationDisposition;
87 if ((flags & GRN_MEMORY_MAP_WRITE) && (flags & GRN_MEMORY_MAP_EXCLUDE)) {
88 dwCreationDisposition = CREATE_NEW;
89 } else {
90 dwCreationDisposition = OPEN_ALWAYS;
91 }
92 DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
93 map->file = CreateFile(path,
94 dwDesiredAccess,
95 dwShareMode,
96 NULL,
97 dwCreationDisposition,
98 dwFlagsAndAttributes,
99 NULL);
100 if (map->file == INVALID_HANDLE_VALUE) {
101 SERR("%s failed to open: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U
102 "): %cread|%cwrite|%cexclude",
103 tag,
104 path,
105 length,
106 offset,
107 (flags & GRN_MEMORY_MAP_READ) ? '+' : '-',
108 (flags & GRN_MEMORY_MAP_WRITE) ? '+' : '-',
109 (flags & GRN_MEMORY_MAP_EXCLUDE) ? '+' : '-');
110 GRN_FREE(map);
111 GRN_API_RETURN(NULL);
112 }
113 }
114
115 {
116 DWORD flProtect = 0;
117 if (flags & GRN_MEMORY_MAP_WRITE) {
118 flProtect = PAGE_READWRITE;
119 } else {
120 flProtect = PAGE_READONLY;
121 }
122 size_t size = offset + length;
123 DWORD dwMaximumSizeHigh = size >> 32;
124 DWORD dwMaximumSizeLow = size & UINT32_MAX;
125 map->mapping = CreateFileMapping(map->file,
126 NULL,
127 flProtect,
128 dwMaximumSizeHigh,
129 dwMaximumSizeLow,
130 NULL);
131 if (!map->mapping) {
132 SERR("%s failed to create mapping: <%s> (%" GRN_FMT_SIZE
133 "+%" GRN_FMT_INT64U "): %cread|%cwrite|%cexclude",
134 tag,
135 path,
136 length,
137 offset,
138 (flags & GRN_MEMORY_MAP_READ) ? '+' : '-',
139 (flags & GRN_MEMORY_MAP_WRITE) ? '+' : '-',
140 (flags & GRN_MEMORY_MAP_EXCLUDE) ? '+' : '-');
141 CloseHandle(map->file);
142 GRN_FREE(map);
143 GRN_API_RETURN(NULL);
144 }
145 }
146
147 {
148 DWORD dwDesiredAccess = 0;
149 if (flags & GRN_MEMORY_MAP_WRITE) {
150 dwDesiredAccess |= FILE_MAP_WRITE;
151 } else {
152 dwDesiredAccess |= FILE_MAP_READ;
153 }
154 DWORD dwFileOffsetHigh = offset >> 32;
155 DWORD dwFileOffsetLow = offset & UINT32_MAX;
156 map->address = MapViewOfFile(map->mapping,
157 dwDesiredAccess,
158 dwFileOffsetHigh,
159 dwFileOffsetLow,
160 length);
161 if (!map->address) {
162 SERR("%s failed to map: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U
163 "): %cread|%cwrite|%cexclude",
164 tag,
165 path,
166 length,
167 offset,
168 (flags & GRN_MEMORY_MAP_READ) ? '+' : '-',
169 (flags & GRN_MEMORY_MAP_WRITE) ? '+' : '-',
170 (flags & GRN_MEMORY_MAP_EXCLUDE) ? '+' : '-');
171 CloseHandle(map->mapping);
172 CloseHandle(map->file);
173 GRN_FREE(map);
174 GRN_API_RETURN(NULL);
175 }
176 }
177
178 map->path = GRN_STRDUP(path);
179 GRN_API_RETURN(map);
180 }
181
182 void
183 grn_memory_map_close(grn_ctx *ctx, grn_memory_map *map)
184 {
185 const char *tag = "[memory-map][close]";
186
187 GRN_API_ENTER;
188 if (!FlushViewOfFile(map->address, map->length)) {
189 SERR("%s failed to flush: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U ")",
190 tag,
191 map->path,
192 map->length,
193 map->offset);
194 }
195 if (!UnmapViewOfFile(map->address)) {
196 SERR("%s failed to unmap: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U ")",
197 tag,
198 map->path,
199 map->length,
200 map->offset);
201 }
202 if (!CloseHandle(map->mapping)) {
203 SERR("%s failed to close mapping: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U
204 ")",
205 tag,
206 map->path,
207 map->length,
208 map->offset);
209 }
210 if (!CloseHandle(map->file)) {
211 SERR("%s failed to close: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U ")",
212 tag,
213 map->path,
214 map->length,
215 map->offset);
216 }
217 GRN_FREE(map->path);
218 GRN_FREE(map);
219 GRN_API_RETURN();
220 }
221 #else
222 /* TODO: offset must be a multiple of the page size as returned by
223 * sysconf(_SC_PAGE_SIZE). */
224 grn_memory_map *
225 grn_memory_map_open(grn_ctx *ctx,
226 const char *path,
227 grn_memory_map_flags flags,
228 uint64_t offset,
229 size_t length)
230 {
231 const char *tag = "[memory-map][open]";
232
233 GRN_API_ENTER;
234
235 grn_memory_map *map = GRN_MALLOC(sizeof(grn_memory_map));
236 if (!map) {
237 char message[GRN_CTX_MSGSIZE];
238 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
239 ERR(GRN_NO_MEMORY_AVAILABLE,
240 "%s failed to allocate: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U
241 "): %s",
242 tag,
243 path,
244 length,
245 offset,
246 message);
247 GRN_API_RETURN(NULL);
248 }
249
250 map->offset = offset;
251 map->length = length;
252
253 int open_flags = 0;
254 if ((flags & GRN_MEMORY_MAP_READ) && (flags & GRN_MEMORY_MAP_WRITE)) {
255 open_flags |= O_RDWR | O_CREAT;
256 } else if (flags & GRN_MEMORY_MAP_READ) {
257 open_flags |= O_RDONLY;
258 } else if (flags & GRN_MEMORY_MAP_WRITE) {
259 open_flags |= O_WRONLY | O_CREAT;
260 }
261 if ((open_flags & O_CREAT) && (flags & GRN_MEMORY_MAP_EXCLUDE)) {
262 open_flags |= O_EXCL;
263 }
264 grn_open(map->fd, path, open_flags);
265 if (map->fd == -1) {
266 SERR("%s failed to open: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U
267 "): %cread|%cwrite|%cexclude",
268 tag,
269 path,
270 length,
271 offset,
272 (flags & GRN_MEMORY_MAP_READ) ? '+' : '-',
273 (flags & GRN_MEMORY_MAP_WRITE) ? '+' : '-',
274 (flags & GRN_MEMORY_MAP_EXCLUDE) ? '+' : '-');
275 GRN_FREE(map);
276 GRN_API_RETURN(NULL);
277 }
278
279 if (flags & GRN_MEMORY_MAP_WRITE) {
280 struct stat s;
281 if (fstat(map->fd, &s) == -1) {
282 SERR("%s failed to stat: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U
283 "): %cread|%cwrite|%cexclude",
284 tag,
285 path,
286 length,
287 offset,
288 (flags & GRN_MEMORY_MAP_READ) ? '+' : '-',
289 (flags & GRN_MEMORY_MAP_WRITE) ? '+' : '-',
290 (flags & GRN_MEMORY_MAP_EXCLUDE) ? '+' : '-');
291 close(map->fd);
292 GRN_FREE(map);
293 GRN_API_RETURN(NULL);
294 }
295
296 off64_t total_size = offset + length;
297 if (s.st_size < total_size) {
298 if (ftruncate(map->fd, total_size) == -1) {
299 SERR("%s failed to expand: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U
300 "): %cread|%cwrite|%cexclude",
301 tag,
302 path,
303 length,
304 offset,
305 (flags & GRN_MEMORY_MAP_READ) ? '+' : '-',
306 (flags & GRN_MEMORY_MAP_WRITE) ? '+' : '-',
307 (flags & GRN_MEMORY_MAP_EXCLUDE) ? '+' : '-');
308 close(map->fd);
309 GRN_FREE(map);
310 GRN_API_RETURN(NULL);
311 }
312 }
313 }
314
315 int mmap_prot = 0;
316 int mmap_flags = MAP_SHARED;
317 if (flags & GRN_MEMORY_MAP_READ) {
318 mmap_prot |= PROT_READ;
319 }
320 if (flags & GRN_MEMORY_MAP_WRITE) {
321 mmap_prot |= PROT_WRITE;
322 }
323 map->address = mmap(NULL, length, mmap_prot, mmap_flags, map->fd, offset);
324 if (map->address == MAP_FAILED) {
325 SERR("%s failed to mmap: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U ")",
326 tag,
327 path,
328 length,
329 offset);
330 close(map->fd);
331 GRN_FREE(map);
332 GRN_API_RETURN(NULL);
333 }
334
335 map->path = GRN_STRDUP(path);
336 GRN_API_RETURN(map);
337 }
338
339 void
340 grn_memory_map_close(grn_ctx *ctx, grn_memory_map *map)
341 {
342 const char *tag = "[memory-map][close]";
343
344 GRN_API_ENTER;
345 if (munmap(map->address, map->length) == -1) {
346 SERR("%s failed to munmap: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U ")",
347 tag,
348 map->path,
349 map->length,
350 map->offset);
351 }
352 if (close(map->fd) == -1) {
353 SERR("%s failed to close: <%s> (%" GRN_FMT_SIZE "+%" GRN_FMT_INT64U ")",
354 tag,
355 map->path,
356 map->length,
357 map->offset);
358 }
359 GRN_FREE(map->path);
360 GRN_FREE(map);
361 GRN_API_RETURN();
362 }
363 #endif
364
365 void *
366 grn_memory_map_get_address(grn_ctx *ctx, grn_memory_map *map)
367 {
368 return map->address;
369 }