blob: 7c65dc3c5bef4cdac22e0bfe7e04fe168ed9f163 [file] [log] [blame]
Elliott Hughese8f4b142018-10-19 16:09:39 -07001/*
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 Hugheseb0ef142019-02-06 14:28:32 -080019#include <errno.h>
20
Elliott Hughese8f4b142018-10-19 16:09:39 -070021namespace android {
22namespace base {
23
24static 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
34std::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);
Pirama Arumuga Nainar5d7f8412019-03-07 16:12:55 -080044 if (handle == nullptr) {
45 // http://b/119818070 "app crashes when reading asset of zero length".
46 // Return a MappedFile that's only valid for reading the size.
47 if (length == 0) {
48 return std::unique_ptr<MappedFile>(new MappedFile{nullptr, 0, 0, nullptr});
49 }
50 return nullptr;
51 }
Elliott Hughese8f4b142018-10-19 16:09:39 -070052 void* base = MapViewOfFile(handle, (prot & PROT_WRITE) ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ, 0,
53 file_offset, file_length);
54 if (base == nullptr) {
55 CloseHandle(handle);
56 return nullptr;
57 }
58 return std::unique_ptr<MappedFile>(
59 new MappedFile{static_cast<char*>(base), length, slop, handle});
60#else
61 void* base = mmap(nullptr, file_length, prot, MAP_SHARED, fd, file_offset);
Elliott Hugheseb0ef142019-02-06 14:28:32 -080062 if (base == MAP_FAILED) {
63 // http://b/119818070 "app crashes when reading asset of zero length".
64 // mmap fails with EINVAL for a zero length region.
65 if (errno == EINVAL && length == 0) {
66 return std::unique_ptr<MappedFile>(new MappedFile{nullptr, 0, 0});
67 }
68 return nullptr;
69 }
Elliott Hughese8f4b142018-10-19 16:09:39 -070070 return std::unique_ptr<MappedFile>(new MappedFile{static_cast<char*>(base), length, slop});
71#endif
72}
73
74MappedFile::~MappedFile() {
75#if defined(_WIN32)
76 if (base_ != nullptr) UnmapViewOfFile(base_);
77 if (handle_ != nullptr) CloseHandle(handle_);
78#else
79 if (base_ != nullptr) munmap(base_, size_);
80#endif
81
82 base_ = nullptr;
83 offset_ = size_ = 0;
84}
85
86} // namespace base
87} // namespace android