| 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"cxx . . $ `<file>/lib/grn.h`/ |
| 20 | |
| 21 | #include "grn_ctx.h"cxx . . $ `<file>/lib/grn_ctx.h`/ |
| 22 | #include "grn_error.h"cxx . . $ `<file>/lib/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_mapcxx . . $ grn_memory_map# { |
| 37 | char *pathcxx . . $ grn_memory_map#path.; |
| 38 | void *addresscxx . . $ grn_memory_map#address.; |
| 39 | uint64_tcxx . . $ uint64_t# offsetcxx . . $ grn_memory_map#offset.; |
| 40 | size_t lengthcxx . . $ grn_memory_map#length.; |
| 41 | #ifdef _WIN32 |
| 42 | HANDLE file; |
| 43 | HANDLE mapping; |
| 44 | #else |
| 45 | int fdcxx . . $ grn_memory_map#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_mapcxx . . $ grn_memory_map# * |
| 225 | grn_memory_map_opencxx . . $ grn_memory_map_open(2a069f25df63803).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 226 | const char *path, |
| 227 | grn_memory_map_flagscxx . . $ grn_memory_map_flags# flags, |
| 228 | uint64_tcxx . . $ uint64_t# offset, |
| 229 | size_t length) |
| 230 | { |
| 231 | const char *tag = "[memory-map][open]"; |
| 232 | |
| 233 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 234 | |
| 235 | grn_memory_mapcxx . . $ grn_memory_map# *map = GRN_MALLOCcxx . . $ `lib/grn_alloc.h:41:9`!(sizeof(grn_memory_map)); |
| 236 | if (!map) { |
| 237 | char message[GRN_CTX_MSGSIZEcxx . . $ `include/groonga/groonga.h:334:9`!]; |
| 238 | grn_strcpycxx . . $ `include/groonga/portability.h:101:11`!(message, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 239 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_NO_MEMORY_AVAILABLE, |
| 240 | "%s failed to allocate: <%s> (%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! "+%" GRN_FMT_INT64Ucxx . . $ `lib/grn.h:370:11`! |
| 241 | "): %s", |
| 242 | tag, |
| 243 | path, |
| 244 | length, |
| 245 | offset, |
| 246 | message); |
| 247 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 248 | } |
| 249 | |
| 250 | map->offsetcxx . . $ grn_memory_map#offset. = offset; |
| 251 | map->lengthcxx . . $ grn_memory_map#length. = length; |
| 252 | |
| 253 | int open_flags = 0; |
| 254 | if ((flags & GRN_MEMORY_MAP_READcxx . . $ `include/groonga/memory_map.h:30:9`!) && (flags & GRN_MEMORY_MAP_WRITEcxx . . $ `include/groonga/memory_map.h:31:9`!)) { |
| 255 | open_flags |= O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!; |
| 256 | } else if (flags & GRN_MEMORY_MAP_READcxx . . $ `include/groonga/memory_map.h:30:9`!) { |
| 257 | open_flags |= O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!; |
| 258 | } else if (flags & GRN_MEMORY_MAP_WRITEcxx . . $ `include/groonga/memory_map.h:31:9`!) { |
| 259 | open_flags |= O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`! | O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!; |
| 260 | } |
| 261 | if ((open_flags & O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!) && (flags & GRN_MEMORY_MAP_EXCLUDEcxx . . $ `include/groonga/memory_map.h:32:9`!)) { |
| 262 | open_flags |= O_EXCLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:50:10`!; |
| 263 | } |
| 264 | grn_opencxx . . $ `include/groonga/portability.h:172:11`!(map->fd, path, open_flags); |
| 265 | if (map->fdcxx . . $ grn_memory_map#fd. == -1) { |
| 266 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed to open: <%s> (%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! "+%" GRN_FMT_INT64Ucxx . . $ `lib/grn.h:370:11`! |
| 267 | "): %cread|%cwrite|%cexclude", |
| 268 | tag, |
| 269 | path, |
| 270 | length, |
| 271 | offset, |
| 272 | (flags & GRN_MEMORY_MAP_READcxx . . $ `include/groonga/memory_map.h:30:9`!) ? '+' : '-', |
| 273 | (flags & GRN_MEMORY_MAP_WRITEcxx . . $ `include/groonga/memory_map.h:31:9`!) ? '+' : '-', |
| 274 | (flags & GRN_MEMORY_MAP_EXCLUDEcxx . . $ `include/groonga/memory_map.h:32:9`!) ? '+' : '-'); |
| 275 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(map); |
| 276 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 277 | } |
| 278 | |
| 279 | if (flags & GRN_MEMORY_MAP_WRITEcxx . . $ `include/groonga/memory_map.h:31:9`!) { |
| 280 | struct statcxx . . $ stat# s; |
| 281 | if (fstatcxx . . $ fstat(a43660ce2f600cb5).(map->fdcxx . . $ grn_memory_map#fd., &s) == -1) { |
| 282 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed to stat: <%s> (%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! "+%" GRN_FMT_INT64Ucxx . . $ `lib/grn.h:370:11`! |
| 283 | "): %cread|%cwrite|%cexclude", |
| 284 | tag, |
| 285 | path, |
| 286 | length, |
| 287 | offset, |
| 288 | (flags & GRN_MEMORY_MAP_READcxx . . $ `include/groonga/memory_map.h:30:9`!) ? '+' : '-', |
| 289 | (flags & GRN_MEMORY_MAP_WRITEcxx . . $ `include/groonga/memory_map.h:31:9`!) ? '+' : '-', |
| 290 | (flags & GRN_MEMORY_MAP_EXCLUDEcxx . . $ `include/groonga/memory_map.h:32:9`!) ? '+' : '-'); |
| 291 | closecxx . . $ close(7864480464b09eea).(map->fdcxx . . $ grn_memory_map#fd.); |
| 292 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(map); |
| 293 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 294 | } |
| 295 | |
| 296 | off64_tcxx . . $ off64_t# total_size = offset + length; |
| 297 | if (s.st_sizecxx . . $ stat#st_size. < total_size) { |
| 298 | if (ftruncatecxx . . $ ftruncate(bba9d6e9fb34fad7).(map->fdcxx . . $ grn_memory_map#fd., total_size) == -1) { |
| 299 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed to expand: <%s> (%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! "+%" GRN_FMT_INT64Ucxx . . $ `lib/grn.h:370:11`! |
| 300 | "): %cread|%cwrite|%cexclude", |
| 301 | tag, |
| 302 | path, |
| 303 | length, |
| 304 | offset, |
| 305 | (flags & GRN_MEMORY_MAP_READcxx . . $ `include/groonga/memory_map.h:30:9`!) ? '+' : '-', |
| 306 | (flags & GRN_MEMORY_MAP_WRITEcxx . . $ `include/groonga/memory_map.h:31:9`!) ? '+' : '-', |
| 307 | (flags & GRN_MEMORY_MAP_EXCLUDEcxx . . $ `include/groonga/memory_map.h:32:9`!) ? '+' : '-'); |
| 308 | closecxx . . $ close(7864480464b09eea).(map->fdcxx . . $ grn_memory_map#fd.); |
| 309 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(map); |
| 310 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | int mmap_prot = 0; |
| 316 | int mmap_flags = MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!; |
| 317 | if (flags & GRN_MEMORY_MAP_READcxx . . $ `include/groonga/memory_map.h:30:9`!) { |
| 318 | mmap_prot |= PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`!; |
| 319 | } |
| 320 | if (flags & GRN_MEMORY_MAP_WRITEcxx . . $ `include/groonga/memory_map.h:31:9`!) { |
| 321 | mmap_prot |= PROT_WRITEcxx . . $ `<external>/17226767460805440860/mman-linux.h:33:9`!; |
| 322 | } |
| 323 | map->addresscxx . . $ grn_memory_map#address. = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, length, mmap_prot, mmap_flags, map->fdcxx . . $ grn_memory_map#fd., offset); |
| 324 | if (map->addresscxx . . $ grn_memory_map#address. == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) { |
| 325 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed to mmap: <%s> (%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! "+%" GRN_FMT_INT64Ucxx . . $ `lib/grn.h:370:11`! ")", |
| 326 | tag, |
| 327 | path, |
| 328 | length, |
| 329 | offset); |
| 330 | closecxx . . $ close(7864480464b09eea).(map->fdcxx . . $ grn_memory_map#fd.); |
| 331 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(map); |
| 332 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 333 | } |
| 334 | |
| 335 | map->pathcxx . . $ grn_memory_map#path. = GRN_STRDUPcxx . . $ `lib/grn_alloc.h:44:9`!(path); |
| 336 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(map); |
| 337 | } |
| 338 | |
| 339 | void |
| 340 | grn_memory_map_closecxx . . $ grn_memory_map_close(b17adfb11015b4a5).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_memory_mapcxx . . $ grn_memory_map# *map) |
| 341 | { |
| 342 | const char *tag = "[memory-map][close]"; |
| 343 | |
| 344 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 345 | if (munmapcxx . . $ munmap(c0a5f96701d3f89).(map->addresscxx . . $ grn_memory_map#address., map->lengthcxx . . $ grn_memory_map#length.) == -1) { |
| 346 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed to munmap: <%s> (%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! "+%" GRN_FMT_INT64Ucxx . . $ `lib/grn.h:370:11`! ")", |
| 347 | tag, |
| 348 | map->path, |
| 349 | map->length, |
| 350 | map->offset); |
| 351 | } |
| 352 | if (closecxx . . $ close(7864480464b09eea).(map->fdcxx . . $ grn_memory_map#fd.) == -1) { |
| 353 | SERRcxx . . $ `lib/grn_error.h:411:11`!("%s failed to close: <%s> (%" GRN_FMT_SIZEcxx . . $ `lib/grn.h:408:11`! "+%" GRN_FMT_INT64Ucxx . . $ `lib/grn.h:370:11`! ")", |
| 354 | tag, |
| 355 | map->path, |
| 356 | map->length, |
| 357 | map->offset); |
| 358 | } |
| 359 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(map->path); |
| 360 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(map); |
| 361 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(); |
| 362 | } |
| 363 | #endif |
| 364 | |
| 365 | void * |
| 366 | grn_memory_map_get_addresscxx . . $ grn_memory_map_get_address(abdd550bc61f2996).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_memory_mapcxx . . $ grn_memory_map# *map) |
| 367 | { |
| 368 | return map->addresscxx . . $ grn_memory_map#address.; |
| 369 | } |