29 #include YFM_YCLib_MemoryMapping
30 #include YFM_YCLib_FileSystem // for platform::uopen, platform::GetFileSizeOf;
37 # include <sys/stat.h>
42 #define MAP_FAILED (reinterpret_cast<void*>(-1))
45 map_file(
size_t len,
int fd)
51 const auto h(::HANDLE(::_get_osfhandle(fd)));
53 if(h != INVALID_HANDLE_VALUE)
54 if(::HANDLE fm = ::CreateFileMapping(h,
nullptr, PAGE_READONLY, 0,
57 p_mapped = ::MapViewOfFile(fm, FILE_MAP_READ, 0, 0, len);
61 return p_mapped ? p_mapped : MAP_FAILED;
67 # include <sys/mman.h>
68 # include <sys/stat.h>
70 # error "Unsupported platform found."
77 : fd(
uopen(path, O_RDONLY, S_IRUSR | S_IWUSR)), size([](int fd){
89 const auto p(map_file(
size, fd));
91 const auto p(::mmap(0,
size, PROT_READ, MAP_PRIVATE, fd, 0));
95 throw std::runtime_error(
"Mapping failed.");
105 ::UnmapViewOfFile(
addr);
YF_API std::uint64_t GetFileSizeOf(int)
取文件的大小。
YF_API int uopen(const char *filename, int oflag) ynothrow
以 UTF-8 文件名无缓冲打开文件。