blob: f49b4f94c5f0cd7ec1cb7bb2873e5889a069412e [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Cui266092c2014-11-11 10:31:30 -080026#if defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO)
Mark Salyzyn5bed8032014-04-30 11:10:46 -070027# define PRId32 "I32d"
28# define PRIx32 "I32x"
29# define PRId64 "I64d"
30#else
Mark Salyzyn15085a62014-04-17 09:34:42 -070031#include <inttypes.h>
Mark Salyzyn5bed8032014-04-30 11:10:46 -070032#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080033#include <stdio.h>
34#include <stdlib.h>
35
Yabin Cui266092c2014-11-11 10:31:30 -080036#if !defined(__MINGW32__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080037#include <sys/mman.h>
38#endif
39
40#include <string.h>
41#include <memory.h>
42#include <errno.h>
43#include <assert.h>
44
45using namespace android;
46
47/*static*/ long FileMap::mPageSize = -1;
48
Mark Salyzynb6185762014-04-17 10:01:12 -070049// Constructor. Create an empty object.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050FileMap::FileMap(void)
51 : mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
52 mDataPtr(NULL), mDataLength(0)
53{
54}
55
Mark Salyzynb6185762014-04-17 10:01:12 -070056// Destructor.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080057FileMap::~FileMap(void)
58{
59 assert(mRefCount == 0);
60
Mark Salyzyn15085a62014-04-17 09:34:42 -070061 //printf("+++ removing FileMap %p %zu\n", mDataPtr, mDataLength);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080062
63 mRefCount = -100; // help catch double-free
64 if (mFileName != NULL) {
65 free(mFileName);
66 }
Yabin Cui266092c2014-11-11 10:31:30 -080067#if defined(__MINGW32__)
Jeff Brown1d618d62010-12-02 13:50:46 -080068 if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
Mark Salyzyn15085a62014-04-17 09:34:42 -070069 ALOGD("UnmapViewOfFile(%p) failed, error = %" PRId32 "\n", mBasePtr,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080070 GetLastError() );
71 }
Jeff Brown1d618d62010-12-02 13:50:46 -080072 if (mFileMapping != INVALID_HANDLE_VALUE) {
73 CloseHandle(mFileMapping);
74 }
Yabin Cui266092c2014-11-11 10:31:30 -080075#else
76 if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
77 ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength);
78 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080079#endif
80}
81
82
Mark Salyzynb6185762014-04-17 10:01:12 -070083// 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 Roote2fa7dc2010-11-24 12:56:06 -080089bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
90 bool readOnly)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080091{
Yabin Cui266092c2014-11-11 10:31:30 -080092#if defined(__MINGW32__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080093 int adjust;
Kenny Roote2fa7dc2010-11-24 12:56:06 -080094 off64_t adjOffset;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080095 size_t adjLength;
96
97 if (mPageSize == -1) {
98 SYSTEM_INFO si;
Mark Salyzynb6185762014-04-17 10:01:12 -070099
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800100 GetSystemInfo( &si );
101 mPageSize = si.dwAllocationGranularity;
102 }
103
104 DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
Mark Salyzynb6185762014-04-17 10:01:12 -0700105
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800106 mFileHandle = (HANDLE) _get_osfhandle(fd);
107 mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
108 if (mFileMapping == NULL) {
Mark Salyzyn15085a62014-04-17 09:34:42 -0700109 ALOGE("CreateFileMapping(%p, %" PRIx32 ") failed with error %" PRId32 "\n",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800110 mFileHandle, protect, GetLastError() );
111 return false;
112 }
Mark Salyzynb6185762014-04-17 10:01:12 -0700113
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800114 adjust = offset % mPageSize;
115 adjOffset = offset - adjust;
116 adjLength = length + adjust;
Mark Salyzynb6185762014-04-17 10:01:12 -0700117
118 mBasePtr = MapViewOfFile( mFileMapping,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800119 readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
120 0,
121 (DWORD)(adjOffset),
122 adjLength );
123 if (mBasePtr == NULL) {
Mark Salyzyn15085a62014-04-17 09:34:42 -0700124 ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %" PRId32 "\n",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800125 adjOffset, adjLength, GetLastError() );
126 CloseHandle(mFileMapping);
127 mFileMapping = INVALID_HANDLE_VALUE;
128 return false;
129 }
Yabin Cui266092c2014-11-11 10:31:30 -0800130#else // !defined(__MINGW32__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800131 int prot, flags, adjust;
Kenny Roote2fa7dc2010-11-24 12:56:06 -0800132 off64_t adjOffset;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800133 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 Salyzynb6185762014-04-17 10:01:12 -0700142 // init on first use
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800143 if (mPageSize == -1) {
144#if NOT_USING_KLIBC
145 mPageSize = sysconf(_SC_PAGESIZE);
146 if (mPageSize == -1) {
Steve Block1b781ab2012-01-06 19:20:56 +0000147 ALOGE("could not get _SC_PAGESIZE\n");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800148 return false;
149 }
150#else
Mark Salyzynb6185762014-04-17 10:01:12 -0700151 // this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800152 mPageSize = 4096;
153#endif
154 }
155
156 adjust = offset % mPageSize;
157try_again:
158 adjOffset = offset - adjust;
159 adjLength = length + adjust;
160
161 flags = MAP_SHARED;
162 prot = PROT_READ;
163 if (!readOnly)
164 prot |= PROT_WRITE;
165
166 ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset);
167 if (ptr == MAP_FAILED) {
Mark Salyzynb6185762014-04-17 10:01:12 -0700168 // Cygwin does not seem to like file mapping files from an offset.
169 // So if we fail, try again with offset zero
170 if (adjOffset > 0) {
171 adjust = offset;
172 goto try_again;
173 }
174
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700175 ALOGE("mmap(%lld,%zu) failed: %s\n",
176 (long long)adjOffset, adjLength, strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800177 return false;
178 }
179 mBasePtr = ptr;
Yabin Cui266092c2014-11-11 10:31:30 -0800180#endif // !defined(__MINGW32__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800181
182 mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
183 mBaseLength = adjLength;
184 mDataOffset = offset;
185 mDataPtr = (char*) mBasePtr + adjust;
186 mDataLength = length;
187
188 assert(mBasePtr != NULL);
189
Mark Salyzyn15085a62014-04-17 09:34:42 -0700190 ALOGV("MAP: base %p/%zu data %p/%zu\n",
191 mBasePtr, mBaseLength, mDataPtr, mDataLength);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800192
193 return true;
194}
195
Mark Salyzynb6185762014-04-17 10:01:12 -0700196// Provide guidance to the system.
Yabin Cui745c5f62014-11-18 20:00:41 -0800197#if !defined(_WIN32)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800198int FileMap::advise(MapAdvice advice)
199{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800200 int cc, sysAdvice;
201
202 switch (advice) {
203 case NORMAL: sysAdvice = MADV_NORMAL; break;
204 case RANDOM: sysAdvice = MADV_RANDOM; break;
205 case SEQUENTIAL: sysAdvice = MADV_SEQUENTIAL; break;
206 case WILLNEED: sysAdvice = MADV_WILLNEED; break;
207 case DONTNEED: sysAdvice = MADV_DONTNEED; break;
208 default:
209 assert(false);
210 return -1;
211 }
212
213 cc = madvise(mBasePtr, mBaseLength, sysAdvice);
214 if (cc != 0)
Steve Block61d341b2012-01-05 23:22:43 +0000215 ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800216 return cc;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800217}
Yabin Cui745c5f62014-11-18 20:00:41 -0800218
219#else
220int FileMap::advise(MapAdvice /* advice */)
221{
222 return -1;
223}
224#endif