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
26 #if defined(HAVE_WIN32_FILEMAP) && !defined(__USE_MINGW_ANSI_STDIO)
27 # define PRId32 "I32d"
28 # define PRIx32 "I32x"
29 # define PRId64 "I64d"
30 #else
31 #include <inttypes.h>
32 #endif
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #ifdef HAVE_POSIX_FILEMAP
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
49 // Constructor. Create an empty object.
FileMap(void)50 FileMap::FileMap(void)
51 : mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
52 mDataPtr(NULL), mDataLength(0)
53 {
54 }
55
56 // Destructor.
~FileMap(void)57 FileMap::~FileMap(void)
58 {
59 assert(mRefCount == 0);
60
61 //printf("+++ removing FileMap %p %zu\n", mDataPtr, mDataLength);
62
63 mRefCount = -100; // help catch double-free
64 if (mFileName != NULL) {
65 free(mFileName);
66 }
67 #ifdef HAVE_POSIX_FILEMAP
68 if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
69 ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength);
70 }
71 #endif
72 #ifdef HAVE_WIN32_FILEMAP
73 if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
74 ALOGD("UnmapViewOfFile(%p) failed, error = %" PRId32 "\n", mBasePtr,
75 GetLastError() );
76 }
77 if (mFileMapping != INVALID_HANDLE_VALUE) {
78 CloseHandle(mFileMapping);
79 }
80 #endif
81 }
82
83
84 // Create a new mapping on an open file.
85 //
86 // Closing the file descriptor does not unmap the pages, so we don't
87 // claim ownership of the fd.
88 //
89 // Returns "false" on failure.
create(const char * origFileName,int fd,off64_t offset,size_t length,bool readOnly)90 bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
91 bool readOnly)
92 {
93 #ifdef HAVE_WIN32_FILEMAP
94 int adjust;
95 off64_t adjOffset;
96 size_t adjLength;
97
98 if (mPageSize == -1) {
99 SYSTEM_INFO si;
100
101 GetSystemInfo( &si );
102 mPageSize = si.dwAllocationGranularity;
103 }
104
105 DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
106
107 mFileHandle = (HANDLE) _get_osfhandle(fd);
108 mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
109 if (mFileMapping == NULL) {
110 ALOGE("CreateFileMapping(%p, %" PRIx32 ") failed with error %" PRId32 "\n",
111 mFileHandle, protect, GetLastError() );
112 return false;
113 }
114
115 adjust = offset % mPageSize;
116 adjOffset = offset - adjust;
117 adjLength = length + adjust;
118
119 mBasePtr = MapViewOfFile( mFileMapping,
120 readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
121 0,
122 (DWORD)(adjOffset),
123 adjLength );
124 if (mBasePtr == NULL) {
125 ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %" PRId32 "\n",
126 adjOffset, adjLength, GetLastError() );
127 CloseHandle(mFileMapping);
128 mFileMapping = INVALID_HANDLE_VALUE;
129 return false;
130 }
131 #endif
132 #ifdef HAVE_POSIX_FILEMAP
133 int prot, flags, adjust;
134 off64_t adjOffset;
135 size_t adjLength;
136
137 void* ptr;
138
139 assert(mRefCount == 1);
140 assert(fd >= 0);
141 assert(offset >= 0);
142 assert(length > 0);
143
144 // init on first use
145 if (mPageSize == -1) {
146 #if NOT_USING_KLIBC
147 mPageSize = sysconf(_SC_PAGESIZE);
148 if (mPageSize == -1) {
149 ALOGE("could not get _SC_PAGESIZE\n");
150 return false;
151 }
152 #else
153 // this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM
154 mPageSize = 4096;
155 #endif
156 }
157
158 adjust = offset % mPageSize;
159 try_again:
160 adjOffset = offset - adjust;
161 adjLength = length + adjust;
162
163 flags = MAP_SHARED;
164 prot = PROT_READ;
165 if (!readOnly)
166 prot |= PROT_WRITE;
167
168 ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset);
169 if (ptr == MAP_FAILED) {
170 // Cygwin does not seem to like file mapping files from an offset.
171 // So if we fail, try again with offset zero
172 if (adjOffset > 0) {
173 adjust = offset;
174 goto try_again;
175 }
176
177 ALOGE("mmap(%lld,%zu) failed: %s\n",
178 (long long)adjOffset, adjLength, strerror(errno));
179 return false;
180 }
181 mBasePtr = ptr;
182 #endif // HAVE_POSIX_FILEMAP
183
184 mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
185 mBaseLength = adjLength;
186 mDataOffset = offset;
187 mDataPtr = (char*) mBasePtr + adjust;
188 mDataLength = length;
189
190 assert(mBasePtr != NULL);
191
192 ALOGV("MAP: base %p/%zu data %p/%zu\n",
193 mBasePtr, mBaseLength, mDataPtr, mDataLength);
194
195 return true;
196 }
197
198 // Provide guidance to the system.
advise(MapAdvice advice)199 int FileMap::advise(MapAdvice advice)
200 {
201 #if HAVE_MADVISE
202 int cc, sysAdvice;
203
204 switch (advice) {
205 case NORMAL: sysAdvice = MADV_NORMAL; break;
206 case RANDOM: sysAdvice = MADV_RANDOM; break;
207 case SEQUENTIAL: sysAdvice = MADV_SEQUENTIAL; break;
208 case WILLNEED: sysAdvice = MADV_WILLNEED; break;
209 case DONTNEED: sysAdvice = MADV_DONTNEED; break;
210 default:
211 assert(false);
212 return -1;
213 }
214
215 cc = madvise(mBasePtr, mBaseLength, sysAdvice);
216 if (cc != 0)
217 ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
218 return cc;
219 #else
220 return -1;
221 #endif // HAVE_MADVISE
222 }
223