21 #if defined(ION_PLATFORM_WINDOWS)
24 #elif !defined(ION_PLATFORM_NACL)
28 #include <sys/types.h>
30 #define POSIX_LIKE_ENOUGH
31 #endif // !WINDOWS && !NACL
37 : data_(NULL), length_(0) {
38 #if defined(ION_PLATFORM_WINDOWS)
39 const std::wstring wide = Utf8ToWide(path);
40 HANDLE
handle = ::CreateFileW(wide.c_str(), GENERIC_READ,
41 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
42 OPEN_EXISTING, 0, NULL);
43 if (handle != INVALID_HANDLE_VALUE) {
44 mapping_ = ::CreateFileMapping(handle, NULL, PAGE_READONLY, 0, 0, NULL);
46 data_ = ::MapViewOfFile(mapping_, FILE_MAP_READ, 0, 0, 0);
48 if (::GetFileSizeEx(handle, &size)) {
49 length_ =
static_cast<size_t>(size.QuadPart);
51 ::UnmapViewOfFile(data_);
55 ::CloseHandle(handle);
57 #elif defined(POSIX_LIKE_ENOUGH)
58 struct stat path_stat = {0};
59 if (stat(path.c_str(), &path_stat))
61 length_ =
static_cast<size_t>(path_stat.st_size);
62 int fd = open(path.c_str(), O_RDONLY);
65 data_ = mmap(NULL, length_, PROT_READ, MAP_PRIVATE, fd, 0);
66 if (data_ == MAP_FAILED || close(fd)) {
74 #if defined(ION_PLATFORM_WINDOWS)
76 ::UnmapViewOfFile(data_);
78 ::CloseHandle(mapping_);
79 #elif defined(POSIX_LIKE_ENOUGH)
81 munmap(data_, length_);
82 #endif // POSIX_LIKE_ENOUGH
size_t GetLength() const
Returns the length of the mapped region.
MemoryMappedFile(const std::string &path)
Maps the file at path.
const void * GetData() const
Returns a pointer to the head of the mapped region.