blob: f1f8bdadffeb2edbd2ace22af0d1e11a14435617 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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
26#include <stdio.h>
27#include <stdlib.h>
28
29#ifdef HAVE_POSIX_FILEMAP
30#include <sys/mman.h>
31#endif
32
33#include <string.h>
34#include <memory.h>
35#include <errno.h>
36#include <assert.h>
37
38using namespace android;
39
40/*static*/ long FileMap::mPageSize = -1;
41
42
43/*
44 * Constructor. Create an empty object.
45 */
46FileMap::FileMap(void)
47 : mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
48 mDataPtr(NULL), mDataLength(0)
49{
50}
51
52/*
53 * Destructor.
54 */
55FileMap::~FileMap(void)
56{
57 assert(mRefCount == 0);
58
59 //printf("+++ removing FileMap %p %u\n", mDataPtr, mDataLength);
60
61 mRefCount = -100; // help catch double-free
62 if (mFileName != NULL) {
63 free(mFileName);
64 }
65#ifdef HAVE_POSIX_FILEMAP
Jeff Browndb360642010-12-02 13:50:46 -080066 if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 LOGD("munmap(%p, %d) failed\n", mBasePtr, (int) mBaseLength);
68 }
69#endif
70#ifdef HAVE_WIN32_FILEMAP
Jeff Browndb360642010-12-02 13:50:46 -080071 if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 LOGD("UnmapViewOfFile(%p) failed, error = %ld\n", mBasePtr,
73 GetLastError() );
74 }
Jeff Browndb360642010-12-02 13:50:46 -080075 if (mFileMapping != INVALID_HANDLE_VALUE) {
76 CloseHandle(mFileMapping);
77 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 CloseHandle(mFileHandle);
79#endif
80}
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.
90 */
91bool FileMap::create(const char* origFileName, int fd, off_t offset, size_t length, bool readOnly)
92{
93#ifdef HAVE_WIN32_FILEMAP
94 int adjust;
95 off_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 LOGE("CreateFileMapping(%p, %lx) failed with error %ld\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 LOGE("MapViewOfFile(%ld, %ld) failed with error %ld\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 off_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 LOGE("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;
159try_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 LOGE("mmap(%ld,%ld) failed: %s\n",
178 (long) adjOffset, (long) 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 LOGV("MAP: base %p/%d data %p/%d\n",
193 mBasePtr, (int) mBaseLength, mDataPtr, (int) mDataLength);
194
195 return true;
196}
197
198/*
199 * Provide guidance to the system.
200 */
201int FileMap::advise(MapAdvice advice)
202{
203#if HAVE_MADVISE
204 int cc, sysAdvice;
205
206 switch (advice) {
207 case NORMAL: sysAdvice = MADV_NORMAL; break;
208 case RANDOM: sysAdvice = MADV_RANDOM; break;
209 case SEQUENTIAL: sysAdvice = MADV_SEQUENTIAL; break;
210 case WILLNEED: sysAdvice = MADV_WILLNEED; break;
211 case DONTNEED: sysAdvice = MADV_DONTNEED; break;
212 default:
213 assert(false);
214 return -1;
215 }
216
217 cc = madvise(mBasePtr, mBaseLength, sysAdvice);
218 if (cc != 0)
219 LOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
220 return cc;
221#else
222 return -1;
223#endif // HAVE_MADVISE
224}