| 126 | #elif defined(_WIN32) |
| 127 | if (mapped_ != nullptr) { |
| 128 | UnmapViewOfFile(mapped_); |
| 129 | mapped_ = nullptr; |
| 130 | size_ = 0; |
| 131 | } |
| 132 | #else |
| 133 | mapped_ = nullptr; |
| 134 | size_ = 0; |
| 135 | #endif |
| 136 | owned_.clear(); |
| 137 | owned_.shrink_to_fit(); |
| 138 | } |
| 139 | |
| 140 | BinaryBlob read_binary_blob(const std::filesystem::path & path) { |
| 141 | #if defined(__unix__) || defined(__APPLE__) |
| 142 | const int fd = open(path.c_str(), O_RDONLY); |
| 143 | if (fd >= 0) { |
| 144 | struct stat st {}; |
| 145 | if (fstat(fd, &st) == 0 && st.st_size >= 0) { |
| 146 | const size_t size = static_cast<size_t>(st.st_size); |
| 147 | if (size == 0) { |
| 148 | close(fd); |
| 149 | return BinaryBlob(); |
| 150 | } |
| 151 | void * mapped = mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 152 | close(fd); |
| 153 | if (mapped != MAP_FAILED) { |
| 154 | return BinaryBlob(static_cast<const std::byte *>(mapped), size); |
| 155 | } |
| 156 | } else { |
| 157 | close(fd); |
| 158 | } |
| 159 | } |
| 160 | #elif defined(_WIN32) |
| 161 | const HANDLE file = CreateFileW( |
| 162 | path.c_str(), |
| 163 | GENERIC_READ, |
| 164 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 165 | nullptr, |
| 166 | OPEN_EXISTING, |
| 167 | FILE_ATTRIBUTE_NORMAL, |
no test coverage detected