|
66 | 66 |
|
67 | 67 | #ifdef __cplusplus |
68 | 68 |
|
69 | | -#if NCNN_THREADS |
70 | 69 | #if defined _WIN32 |
71 | 70 | #define WIN32_LEAN_AND_MEAN |
72 | 71 | #include <windows.h> |
| 72 | +#elif defined __ANDROID__ || defined __OHOS__ || defined __linux__ || __APPLE__ |
| 73 | +#include <sys/types.h> |
| 74 | +#include <sys/stat.h> |
| 75 | +#include <sys/mman.h> |
| 76 | +#include <fcntl.h> |
| 77 | +#include <unistd.h> |
| 78 | +#endif |
| 79 | + |
| 80 | +#if NCNN_THREADS |
| 81 | +#if defined _WIN32 |
73 | 82 | #include <process.h> |
74 | 83 | #else |
75 | 84 | #include <pthread.h> |
|
82 | 91 | #endif |
83 | 92 | #endif // __ANDROID_API__ >= 26 |
84 | 93 |
|
| 94 | +#include <stddef.h> |
| 95 | + |
85 | 96 | namespace ncnn { |
86 | 97 |
|
87 | 98 | #if NCNN_THREADS |
@@ -282,6 +293,117 @@ private: |
282 | 293 | Mutex& mutex; |
283 | 294 | }; |
284 | 295 |
|
| 296 | +#if defined _WIN32 |
| 297 | +class NCNN_EXPORT MappedFile |
| 298 | +{ |
| 299 | +public: |
| 300 | + MappedFile() { ptr = 0; _size = 0; file = INVALID_HANDLE_VALUE; mapping = 0; } |
| 301 | + ~MappedFile() { close(); } |
| 302 | + int open(const char* path) |
| 303 | + { |
| 304 | + close(); |
| 305 | + |
| 306 | + file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 307 | + if (file == INVALID_HANDLE_VALUE) return -1; |
| 308 | + |
| 309 | + LARGE_INTEGER liSize; |
| 310 | + if (!GetFileSizeEx(file, &liSize)) { close(); return -1; } |
| 311 | + |
| 312 | + _size = (size_t)liSize.QuadPart; |
| 313 | + if (_size == 0) { close(); return -1; } |
| 314 | + |
| 315 | + mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL); |
| 316 | + if (!mapping) { close(); return -1; } |
| 317 | + |
| 318 | + ptr = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0); |
| 319 | + if (!ptr) { close(); return -1; } |
| 320 | + return 0; |
| 321 | + } |
| 322 | + int open(const wchar_t* path) |
| 323 | + { |
| 324 | + close(); |
| 325 | + |
| 326 | + file = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 327 | + if (file == INVALID_HANDLE_VALUE) return -1; |
| 328 | + |
| 329 | + LARGE_INTEGER liSize; |
| 330 | + if (!GetFileSizeEx(file, &liSize)) { close(); return -1; } |
| 331 | + |
| 332 | + _size = (size_t)liSize.QuadPart; |
| 333 | + if (_size == 0) { close(); return -1; } |
| 334 | + |
| 335 | + mapping = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL); |
| 336 | + if (!mapping) { close(); return -1; } |
| 337 | + |
| 338 | + ptr = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0); |
| 339 | + if (!ptr) { close(); return -1; } |
| 340 | + return 0; |
| 341 | + } |
| 342 | + void close() |
| 343 | + { |
| 344 | + if (ptr) { UnmapViewOfFile(ptr); ptr = 0; } |
| 345 | + if (mapping) { CloseHandle(mapping); mapping = 0; } |
| 346 | + if (file != INVALID_HANDLE_VALUE) { CloseHandle(file); file = INVALID_HANDLE_VALUE; } |
| 347 | + _size = 0; |
| 348 | + } |
| 349 | + const void* mapped_ptr() const { return ptr; } |
| 350 | + size_t size() const { return _size; } |
| 351 | +private: |
| 352 | + void* ptr; |
| 353 | + size_t _size; |
| 354 | + HANDLE file; |
| 355 | + HANDLE mapping; |
| 356 | +}; |
| 357 | +#elif defined __ANDROID__ || defined __OHOS__ || defined __linux__ || __APPLE__ |
| 358 | +class NCNN_EXPORT MappedFile |
| 359 | +{ |
| 360 | +public: |
| 361 | + MappedFile() { ptr = 0; _size = 0; fd = -1; } |
| 362 | + ~MappedFile() { close(); } |
| 363 | + int open(const char* path) |
| 364 | + { |
| 365 | + close(); |
| 366 | + |
| 367 | + fd = ::open(path, O_RDONLY); |
| 368 | + if (fd < 0) return -1; |
| 369 | + |
| 370 | + struct stat st; |
| 371 | + if (fstat(fd, &st) < 0) { close(); return -1; } |
| 372 | + |
| 373 | + _size = (size_t)st.st_size; |
| 374 | + if (_size == 0) { close(); return -1; } |
| 375 | + |
| 376 | + ptr = mmap(NULL, _size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 377 | + if (ptr == MAP_FAILED) { close(); return -1; } |
| 378 | + return 0; |
| 379 | + } |
| 380 | + void close() |
| 381 | + { |
| 382 | + if (ptr && ptr != MAP_FAILED) { munmap(ptr, _size); } |
| 383 | + ptr = 0; |
| 384 | + if (fd >= 0) { ::close(fd); fd = -1; } |
| 385 | + _size = 0; |
| 386 | + } |
| 387 | + const void* mapped_ptr() const { return ptr; } |
| 388 | + size_t size() const { return _size; } |
| 389 | +private: |
| 390 | + void* ptr; |
| 391 | + size_t _size; |
| 392 | + int fd; |
| 393 | +}; |
| 394 | +#else // defined _WIN32 || __ANDROID__ || defined __OHOS__ || defined __linux__ || __APPLE__ |
| 395 | +class NCNN_EXPORT MappedFile |
| 396 | +{ |
| 397 | +public: |
| 398 | + MappedFile() {} |
| 399 | + ~MappedFile() {} |
| 400 | + int open(const char* /*path*/) { return -1; } |
| 401 | + void close() {} |
| 402 | + const void* mapped_ptr() const { return 0; } |
| 403 | + size_t size() const { return 0; } |
| 404 | +}; |
| 405 | +#endif // defined _WIN32 || __ANDROID__ || defined __OHOS__ || defined __linux__ || __APPLE__ |
| 406 | + |
285 | 407 | static inline void swap_endianness_16(void* x) |
286 | 408 | { |
287 | 409 | unsigned char* xx = (unsigned char*)x; |
|
0 commit comments