blob: b9944c9f3b03713502f80625567d6ac5c4191c33 [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
Mark Salyzyn15085a62014-04-17 09:34:42 -070026#include <inttypes.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027#include <stdio.h>
28#include <stdlib.h>
29
30#ifdef HAVE_POSIX_FILEMAP
31#include <sys/mman.h>
32#endif
33
34#include <string.h>
35#include <memory.h>
36#include <errno.h>
37#include <assert.h>
38
39using namespace android;
40
41/*static*/ long FileMap::mPageSize = -1;
42
43
44/*
45 * Constructor. Create an empty object.
46 */
47FileMap::FileMap(void)
48 : mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
49 mDataPtr(NULL), mDataLength(0)
50{
51}
52
53/*
54 * Destructor.
55 */
56FileMap::~FileMap(void)
57{
58 assert(mRefCount == 0);
59
Mark Salyzyn15085a62014-04-17 09:34:42 -070060 //printf("+++ removing FileMap %p %zu\n", mDataPtr, mDataLength);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080061
62 mRefCount = -100; // help catch double-free
63 if (mFileName != NULL) {
64 free(mFileName);
65 }
66#ifdef HAVE_POSIX_FILEMAP
Jeff Brown1d618d62010-12-02 13:50:46 -080067 if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
Mark Salyzyn15085a62014-04-17 09:34:42 -070068 ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080069 }
70#endif
71#ifdef HAVE_WIN32_FILEMAP
Jeff Brown1d618d62010-12-02 13:50:46 -080072 if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
Mark Salyzyn15085a62014-04-17 09:34:42 -070073 ALOGD("UnmapViewOfFile(%p) failed, error = %" PRId32 "\n", mBasePtr,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080074 GetLastError() );
75 }
Jeff Brown1d618d62010-12-02 13:50:46 -080076 if (mFileMapping != INVALID_HANDLE_VALUE) {
77 CloseHandle(mFileMapping);
78 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080079 CloseHandle(mFileHandle);
80#endif
81}
82
83
84/*
85 * Create a new mapping on an open file.
86 *
87 * Closing the file descriptor does not unmap the pages, so we don't
88 * claim ownership of the fd.
89 *
90 * Returns "false" on failure.
91 */
Kenny Roote2fa7dc2010-11-24 12:56:06 -080092bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
93 bool readOnly)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080094{
95#ifdef HAVE_WIN32_FILEMAP
96 int adjust;
Kenny Roote2fa7dc2010-11-24 12:56:06 -080097 off64_t adjOffset;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080098 size_t adjLength;
99
100 if (mPageSize == -1) {
101 SYSTEM_INFO si;
102
103 GetSystemInfo( &si );
104 mPageSize = si.dwAllocationGranularity;
105 }
106
107 DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
108
109 mFileHandle = (HANDLE) _get_osfhandle(fd);
110 mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
111 if (mFileMapping == NULL) {
Mark Salyzyn15085a62014-04-17 09:34:42 -0700112 ALOGE("CreateFileMapping(%p, %" PRIx32 ") failed with error %" PRId32 "\n",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800113 mFileHandle, protect, GetLastError() );
114 return false;
115 }
116
117 adjust = offset % mPageSize;
118 adjOffset = offset - adjust;
119 adjLength = length + adjust;
120
121 mBasePtr = MapViewOfFile( mFileMapping,
122 readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
123 0,
124 (DWORD)(adjOffset),
125 adjLength );
126 if (mBasePtr == NULL) {
Mark Salyzyn15085a62014-04-17 09:34:42 -0700127 ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %" PRId32 "\n",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800128 adjOffset, adjLength, GetLastError() );
129 CloseHandle(mFileMapping);
130 mFileMapping = INVALID_HANDLE_VALUE;
131 return false;
132 }
133#endif
134#ifdef HAVE_POSIX_FILEMAP
135 int prot, flags, adjust;
Kenny Roote2fa7dc2010-11-24 12:56:06 -0800136 off64_t adjOffset;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800137 size_t adjLength;
138
139 void* ptr;
140
141 assert(mRefCount == 1);
142 assert(fd >= 0);
143 assert(offset >= 0);
144 assert(length > 0);
145
146 /* init on first use */
147 if (mPageSize == -1) {
148#if NOT_USING_KLIBC
149 mPageSize = sysconf(_SC_PAGESIZE);
150 if (mPageSize == -1) {
Steve Block1b781ab2012-01-06 19:20:56 +0000151 ALOGE("could not get _SC_PAGESIZE\n");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800152 return false;
153 }
154#else
155 /* this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM */
156 mPageSize = 4096;
157#endif
158 }
159
160 adjust = offset % mPageSize;
161try_again:
162 adjOffset = offset - adjust;
163 adjLength = length + adjust;
164
165 flags = MAP_SHARED;
166 prot = PROT_READ;
167 if (!readOnly)
168 prot |= PROT_WRITE;
169
170 ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset);
171 if (ptr == MAP_FAILED) {
172 // Cygwin does not seem to like file mapping files from an offset.
173 // So if we fail, try again with offset zero
174 if (adjOffset > 0) {
175 adjust = offset;
176 goto try_again;
177 }
178
Mark Salyzyn15085a62014-04-17 09:34:42 -0700179 ALOGE("mmap(%" PRId64 ",%zu) failed: %s\n",
180 adjOffset, adjLength, strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800181 return false;
182 }
183 mBasePtr = ptr;
184#endif /* HAVE_POSIX_FILEMAP */
185
186 mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
187 mBaseLength = adjLength;
188 mDataOffset = offset;
189 mDataPtr = (char*) mBasePtr + adjust;
190 mDataLength = length;
191
192 assert(mBasePtr != NULL);
193
Mark Salyzyn15085a62014-04-17 09:34:42 -0700194 ALOGV("MAP: base %p/%zu data %p/%zu\n",
195 mBasePtr, mBaseLength, mDataPtr, mDataLength);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800196
197 return true;
198}
199
200/*
201 * Provide guidance to the system.
202 */
203int FileMap::advise(MapAdvice advice)
204{
205#if HAVE_MADVISE
206 int cc, sysAdvice;
207
208 switch (advice) {
209 case NORMAL: sysAdvice = MADV_NORMAL; break;
210 case RANDOM: sysAdvice = MADV_RANDOM; break;
211 case SEQUENTIAL: sysAdvice = MADV_SEQUENTIAL; break;
212 case WILLNEED: sysAdvice = MADV_WILLNEED; break;
213 case DONTNEED: sysAdvice = MADV_DONTNEED; break;
214 default:
215 assert(false);
216 return -1;
217 }
218
219 cc = madvise(mBasePtr, mBaseLength, sysAdvice);
220 if (cc != 0)
Steve Block61d341b2012-01-05 23:22:43 +0000221 ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800222 return cc;
223#else
224 return -1;
225#endif // HAVE_MADVISE
226}