blob: f689bfad7903fad03c5c385c4073bcb495ec295f [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
Josh Gao27241a72019-04-25 14:04:57 -070021#include "android-base/unique_fd.h"
22
Elliott Hughese8f4b142018-10-19 16:09:39 -070023namespace android {
24namespace base {
25
26static off64_t InitPageSize() {
27#if defined(_WIN32)
28 SYSTEM_INFO si;
29 GetSystemInfo(&si);
30 return si.dwAllocationGranularity;
31#else
32 return sysconf(_SC_PAGE_SIZE);
33#endif
34}
35
Josh Gao27241a72019-04-25 14:04:57 -070036std::unique_ptr<MappedFile> MappedFile::FromFd(borrowed_fd fd, off64_t offset, size_t length,
37 int prot) {
Elliott Hughese8f4b142018-10-19 16:09:39 -070038 static off64_t page_size = InitPageSize();
39 size_t slop = offset % page_size;
40 off64_t file_offset = offset - slop;
41 off64_t file_length = length + slop;
42
43#if defined(_WIN32)
44 HANDLE handle =
Josh Gao27241a72019-04-25 14:04:57 -070045 CreateFileMapping(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), nullptr,
Elliott Hughese8f4b142018-10-19 16:09:39 -070046 (prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY, 0, 0, nullptr);
Pirama Arumuga Nainar5d7f8412019-03-07 16:12:55 -080047 if (handle == nullptr) {
48 // http://b/119818070 "app crashes when reading asset of zero length".
49 // Return a MappedFile that's only valid for reading the size.
50 if (length == 0) {
51 return std::unique_ptr<MappedFile>(new MappedFile{nullptr, 0, 0, nullptr});
52 }
53 return nullptr;
54 }
Elliott Hughese8f4b142018-10-19 16:09:39 -070055 void* base = MapViewOfFile(handle, (prot & PROT_WRITE) ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ, 0,
56 file_offset, file_length);
57 if (base == nullptr) {
58 CloseHandle(handle);
59 return nullptr;
60 }
61 return std::unique_ptr<MappedFile>(
62 new MappedFile{static_cast<char*>(base), length, slop, handle});
63#else
Josh Gao27241a72019-04-25 14:04:57 -070064 void* base = mmap(nullptr, file_length, prot, MAP_SHARED, fd.get(), file_offset);
Elliott Hugheseb0ef142019-02-06 14:28:32 -080065 if (base == MAP_FAILED) {
66 // http://b/119818070 "app crashes when reading asset of zero length".
67 // mmap fails with EINVAL for a zero length region.
68 if (errno == EINVAL && length == 0) {
69 return std::unique_ptr<MappedFile>(new MappedFile{nullptr, 0, 0});
70 }
71 return nullptr;
72 }
Elliott Hughese8f4b142018-10-19 16:09:39 -070073 return std::unique_ptr<MappedFile>(new MappedFile{static_cast<char*>(base), length, slop});
74#endif
75}
76
77MappedFile::~MappedFile() {
78#if defined(_WIN32)
79 if (base_ != nullptr) UnmapViewOfFile(base_);
80 if (handle_ != nullptr) CloseHandle(handle_);
81#else
82 if (base_ != nullptr) munmap(base_, size_);
83#endif
84
85 base_ = nullptr;
86 offset_ = size_ = 0;
87}
88
89} // namespace base
90} // namespace android