| 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 |  | 
|  | 19 | namespace android { | 
|  | 20 | namespace base { | 
|  | 21 |  | 
|  | 22 | static off64_t InitPageSize() { | 
|  | 23 | #if defined(_WIN32) | 
|  | 24 | SYSTEM_INFO si; | 
|  | 25 | GetSystemInfo(&si); | 
|  | 26 | return si.dwAllocationGranularity; | 
|  | 27 | #else | 
|  | 28 | return sysconf(_SC_PAGE_SIZE); | 
|  | 29 | #endif | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | std::unique_ptr<MappedFile> MappedFile::FromFd(int fd, off64_t offset, size_t length, int prot) { | 
|  | 33 | static off64_t page_size = InitPageSize(); | 
|  | 34 | size_t slop = offset % page_size; | 
|  | 35 | off64_t file_offset = offset - slop; | 
|  | 36 | off64_t file_length = length + slop; | 
|  | 37 |  | 
|  | 38 | #if defined(_WIN32) | 
|  | 39 | HANDLE handle = | 
|  | 40 | CreateFileMapping(reinterpret_cast<HANDLE>(_get_osfhandle(fd)), nullptr, | 
|  | 41 | (prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY, 0, 0, nullptr); | 
|  | 42 | if (handle == nullptr) return nullptr; | 
|  | 43 | void* base = MapViewOfFile(handle, (prot & PROT_WRITE) ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ, 0, | 
|  | 44 | file_offset, file_length); | 
|  | 45 | if (base == nullptr) { | 
|  | 46 | CloseHandle(handle); | 
|  | 47 | return nullptr; | 
|  | 48 | } | 
|  | 49 | return std::unique_ptr<MappedFile>( | 
|  | 50 | new MappedFile{static_cast<char*>(base), length, slop, handle}); | 
|  | 51 | #else | 
|  | 52 | void* base = mmap(nullptr, file_length, prot, MAP_SHARED, fd, file_offset); | 
|  | 53 | if (base == MAP_FAILED) return nullptr; | 
|  | 54 | return std::unique_ptr<MappedFile>(new MappedFile{static_cast<char*>(base), length, slop}); | 
|  | 55 | #endif | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | MappedFile::~MappedFile() { | 
|  | 59 | #if defined(_WIN32) | 
|  | 60 | if (base_ != nullptr) UnmapViewOfFile(base_); | 
|  | 61 | if (handle_ != nullptr) CloseHandle(handle_); | 
|  | 62 | #else | 
|  | 63 | if (base_ != nullptr) munmap(base_, size_); | 
|  | 64 | #endif | 
|  | 65 |  | 
|  | 66 | base_ = nullptr; | 
|  | 67 | offset_ = size_ = 0; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | }  // namespace base | 
|  | 71 | }  // namespace android |