| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2018 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | #include "android-base/mapped_file.h" | 
|  | 18 |  | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 19 | #include <errno.h> | 
|  | 20 |  | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 21 | namespace android { | 
|  | 22 | namespace base { | 
|  | 23 |  | 
|  | 24 | static off64_t InitPageSize() { | 
|  | 25 | #if defined(_WIN32) | 
|  | 26 | SYSTEM_INFO si; | 
|  | 27 | GetSystemInfo(&si); | 
|  | 28 | return si.dwAllocationGranularity; | 
|  | 29 | #else | 
|  | 30 | return sysconf(_SC_PAGE_SIZE); | 
|  | 31 | #endif | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | std::unique_ptr<MappedFile> MappedFile::FromFd(int fd, off64_t offset, size_t length, int prot) { | 
|  | 35 | static off64_t page_size = InitPageSize(); | 
|  | 36 | size_t slop = offset % page_size; | 
|  | 37 | off64_t file_offset = offset - slop; | 
|  | 38 | off64_t file_length = length + slop; | 
|  | 39 |  | 
|  | 40 | #if defined(_WIN32) | 
|  | 41 | HANDLE handle = | 
|  | 42 | CreateFileMapping(reinterpret_cast<HANDLE>(_get_osfhandle(fd)), nullptr, | 
|  | 43 | (prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY, 0, 0, nullptr); | 
|  | 44 | if (handle == nullptr) return nullptr; | 
|  | 45 | void* base = MapViewOfFile(handle, (prot & PROT_WRITE) ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ, 0, | 
|  | 46 | file_offset, file_length); | 
|  | 47 | if (base == nullptr) { | 
|  | 48 | CloseHandle(handle); | 
|  | 49 | return nullptr; | 
|  | 50 | } | 
|  | 51 | return std::unique_ptr<MappedFile>( | 
|  | 52 | new MappedFile{static_cast<char*>(base), length, slop, handle}); | 
|  | 53 | #else | 
|  | 54 | void* base = mmap(nullptr, file_length, prot, MAP_SHARED, fd, file_offset); | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 55 | if (base == MAP_FAILED) { | 
|  | 56 | // http://b/119818070 "app crashes when reading asset of zero length". | 
|  | 57 | // mmap fails with EINVAL for a zero length region. | 
|  | 58 | if (errno == EINVAL && length == 0) { | 
|  | 59 | return std::unique_ptr<MappedFile>(new MappedFile{nullptr, 0, 0}); | 
|  | 60 | } | 
|  | 61 | return nullptr; | 
|  | 62 | } | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 63 | return std::unique_ptr<MappedFile>(new MappedFile{static_cast<char*>(base), length, slop}); | 
|  | 64 | #endif | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | MappedFile::~MappedFile() { | 
|  | 68 | #if defined(_WIN32) | 
|  | 69 | if (base_ != nullptr) UnmapViewOfFile(base_); | 
|  | 70 | if (handle_ != nullptr) CloseHandle(handle_); | 
|  | 71 | #else | 
|  | 72 | if (base_ != nullptr) munmap(base_, size_); | 
|  | 73 | #endif | 
|  | 74 |  | 
|  | 75 | base_ = nullptr; | 
|  | 76 | offset_ = size_ = 0; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | }  // namespace base | 
|  | 80 | }  // namespace android |