The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | // |
| 18 | // Shared file mapping class. |
| 19 | // |
| 20 | |
| 21 | #define LOG_TAG "filemap" |
| 22 | |
| 23 | #include <utils/FileMap.h> |
| 24 | #include <utils/Log.h> |
| 25 | |
Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 26 | #if defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO) |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 27 | # define PRId32 "I32d" |
| 28 | # define PRIx32 "I32x" |
| 29 | # define PRId64 "I64d" |
| 30 | #else |
Mark Salyzyn | 15085a6 | 2014-04-17 09:34:42 -0700 | [diff] [blame] | 31 | #include <inttypes.h> |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 32 | #endif |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | |
Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 36 | #if !defined(__MINGW32__) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <sys/mman.h> |
| 38 | #endif |
| 39 | |
| 40 | #include <string.h> |
| 41 | #include <memory.h> |
| 42 | #include <errno.h> |
| 43 | #include <assert.h> |
| 44 | |
| 45 | using namespace android; |
| 46 | |
| 47 | /*static*/ long FileMap::mPageSize = -1; |
| 48 | |
Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 49 | // Constructor. Create an empty object. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | FileMap::FileMap(void) |
| 51 | : mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0), |
| 52 | mDataPtr(NULL), mDataLength(0) |
| 53 | { |
| 54 | } |
| 55 | |
Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 56 | // Destructor. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | FileMap::~FileMap(void) |
| 58 | { |
| 59 | assert(mRefCount == 0); |
| 60 | |
Mark Salyzyn | 15085a6 | 2014-04-17 09:34:42 -0700 | [diff] [blame] | 61 | //printf("+++ removing FileMap %p %zu\n", mDataPtr, mDataLength); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
| 63 | mRefCount = -100; // help catch double-free |
| 64 | if (mFileName != NULL) { |
| 65 | free(mFileName); |
| 66 | } |
Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 67 | #if defined(__MINGW32__) |
Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 68 | if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) { |
Mark Salyzyn | 15085a6 | 2014-04-17 09:34:42 -0700 | [diff] [blame] | 69 | ALOGD("UnmapViewOfFile(%p) failed, error = %" PRId32 "\n", mBasePtr, |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | GetLastError() ); |
| 71 | } |
Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 72 | if (mFileMapping != INVALID_HANDLE_VALUE) { |
| 73 | CloseHandle(mFileMapping); |
| 74 | } |
Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 75 | #else |
| 76 | if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) { |
| 77 | ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength); |
| 78 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | #endif |
| 80 | } |
| 81 | |
| 82 | |
Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 83 | // Create a new mapping on an open file. |
| 84 | // |
| 85 | // Closing the file descriptor does not unmap the pages, so we don't |
| 86 | // claim ownership of the fd. |
| 87 | // |
| 88 | // Returns "false" on failure. |
Kenny Root | e2fa7dc | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 89 | bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length, |
| 90 | bool readOnly) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | { |
Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 92 | #if defined(__MINGW32__) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | int adjust; |
Kenny Root | e2fa7dc | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 94 | off64_t adjOffset; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | size_t adjLength; |
| 96 | |
| 97 | if (mPageSize == -1) { |
| 98 | SYSTEM_INFO si; |
Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 99 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | GetSystemInfo( &si ); |
| 101 | mPageSize = si.dwAllocationGranularity; |
| 102 | } |
| 103 | |
| 104 | DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE; |
Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 105 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | mFileHandle = (HANDLE) _get_osfhandle(fd); |
| 107 | mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL); |
| 108 | if (mFileMapping == NULL) { |
Mark Salyzyn | 15085a6 | 2014-04-17 09:34:42 -0700 | [diff] [blame] | 109 | ALOGE("CreateFileMapping(%p, %" PRIx32 ") failed with error %" PRId32 "\n", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | mFileHandle, protect, GetLastError() ); |
| 111 | return false; |
| 112 | } |
Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 113 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | adjust = offset % mPageSize; |
| 115 | adjOffset = offset - adjust; |
| 116 | adjLength = length + adjust; |
Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 117 | |
| 118 | mBasePtr = MapViewOfFile( mFileMapping, |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, |
| 120 | 0, |
| 121 | (DWORD)(adjOffset), |
| 122 | adjLength ); |
| 123 | if (mBasePtr == NULL) { |
Mark Salyzyn | 15085a6 | 2014-04-17 09:34:42 -0700 | [diff] [blame] | 124 | ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %" PRId32 "\n", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | adjOffset, adjLength, GetLastError() ); |
| 126 | CloseHandle(mFileMapping); |
| 127 | mFileMapping = INVALID_HANDLE_VALUE; |
| 128 | return false; |
| 129 | } |
Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 130 | #else // !defined(__MINGW32__) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | int prot, flags, adjust; |
Kenny Root | e2fa7dc | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 132 | off64_t adjOffset; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | size_t adjLength; |
| 134 | |
| 135 | void* ptr; |
| 136 | |
| 137 | assert(mRefCount == 1); |
| 138 | assert(fd >= 0); |
| 139 | assert(offset >= 0); |
| 140 | assert(length > 0); |
| 141 | |
Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 142 | // init on first use |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | if (mPageSize == -1) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | mPageSize = sysconf(_SC_PAGESIZE); |
| 145 | if (mPageSize == -1) { |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 146 | ALOGE("could not get _SC_PAGESIZE\n"); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | return false; |
| 148 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Narayan Kamath | 5955a9f | 2015-02-19 17:29:27 +0000 | [diff] [blame^] | 151 | adjust = offset % mPageSize; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | adjOffset = offset - adjust; |
| 153 | adjLength = length + adjust; |
| 154 | |
| 155 | flags = MAP_SHARED; |
| 156 | prot = PROT_READ; |
| 157 | if (!readOnly) |
| 158 | prot |= PROT_WRITE; |
| 159 | |
| 160 | ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset); |
| 161 | if (ptr == MAP_FAILED) { |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 162 | ALOGE("mmap(%lld,%zu) failed: %s\n", |
| 163 | (long long)adjOffset, adjLength, strerror(errno)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | mBasePtr = ptr; |
Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 167 | #endif // !defined(__MINGW32__) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
| 169 | mFileName = origFileName != NULL ? strdup(origFileName) : NULL; |
| 170 | mBaseLength = adjLength; |
| 171 | mDataOffset = offset; |
| 172 | mDataPtr = (char*) mBasePtr + adjust; |
| 173 | mDataLength = length; |
| 174 | |
| 175 | assert(mBasePtr != NULL); |
| 176 | |
Mark Salyzyn | 15085a6 | 2014-04-17 09:34:42 -0700 | [diff] [blame] | 177 | ALOGV("MAP: base %p/%zu data %p/%zu\n", |
| 178 | mBasePtr, mBaseLength, mDataPtr, mDataLength); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 183 | // Provide guidance to the system. |
Yabin Cui | 745c5f6 | 2014-11-18 20:00:41 -0800 | [diff] [blame] | 184 | #if !defined(_WIN32) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | int FileMap::advise(MapAdvice advice) |
| 186 | { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | int cc, sysAdvice; |
| 188 | |
| 189 | switch (advice) { |
| 190 | case NORMAL: sysAdvice = MADV_NORMAL; break; |
| 191 | case RANDOM: sysAdvice = MADV_RANDOM; break; |
| 192 | case SEQUENTIAL: sysAdvice = MADV_SEQUENTIAL; break; |
| 193 | case WILLNEED: sysAdvice = MADV_WILLNEED; break; |
| 194 | case DONTNEED: sysAdvice = MADV_DONTNEED; break; |
| 195 | default: |
| 196 | assert(false); |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | cc = madvise(mBasePtr, mBaseLength, sysAdvice); |
| 201 | if (cc != 0) |
Steve Block | 61d341b | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 202 | ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | return cc; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
Yabin Cui | 745c5f6 | 2014-11-18 20:00:41 -0800 | [diff] [blame] | 205 | |
| 206 | #else |
| 207 | int FileMap::advise(MapAdvice /* advice */) |
| 208 | { |
| 209 | return -1; |
| 210 | } |
| 211 | #endif |