2
* Copyright (C) 2006 The Android Open Source Project
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
// Shared file mapping class.
21
#define LOG_TAG "filemap"
23
#include <utils/FileMap.h>
29
#ifdef HAVE_POSIX_FILEMAP
38
using namespace android;
40
/*static*/ long FileMap::mPageSize = -1;
44
* Constructor. Create an empty object.
46
FileMap::FileMap(void)
47
: mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
48
mDataPtr(NULL), mDataLength(0)
55
FileMap::~FileMap(void)
57
assert(mRefCount == 0);
59
//printf("+++ removing FileMap %p %u\n", mDataPtr, mDataLength);
61
mRefCount = -100; // help catch double-free
62
if (mFileName != NULL) {
65
#ifdef HAVE_POSIX_FILEMAP
66
if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
67
ALOGD("munmap(%p, %d) failed\n", mBasePtr, (int) mBaseLength);
70
#ifdef HAVE_WIN32_FILEMAP
71
if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
72
ALOGD("UnmapViewOfFile(%p) failed, error = %ld\n", mBasePtr,
75
if (mFileMapping != INVALID_HANDLE_VALUE) {
76
CloseHandle(mFileMapping);
78
CloseHandle(mFileHandle);
84
* Create a new mapping on an open file.
86
* Closing the file descriptor does not unmap the pages, so we don't
87
* claim ownership of the fd.
89
* Returns "false" on failure.
91
bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
94
#ifdef HAVE_WIN32_FILEMAP
99
if (mPageSize == -1) {
102
GetSystemInfo( &si );
103
mPageSize = si.dwAllocationGranularity;
106
DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
108
mFileHandle = (HANDLE) _get_osfhandle(fd);
109
mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
110
if (mFileMapping == NULL) {
111
ALOGE("CreateFileMapping(%p, %lx) failed with error %ld\n",
112
mFileHandle, protect, GetLastError() );
116
adjust = offset % mPageSize;
117
adjOffset = offset - adjust;
118
adjLength = length + adjust;
120
mBasePtr = MapViewOfFile( mFileMapping,
121
readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
125
if (mBasePtr == NULL) {
126
ALOGE("MapViewOfFile(%ld, %ld) failed with error %ld\n",
127
adjOffset, adjLength, GetLastError() );
128
CloseHandle(mFileMapping);
129
mFileMapping = INVALID_HANDLE_VALUE;
133
#ifdef HAVE_POSIX_FILEMAP
134
int prot, flags, adjust;
140
assert(mRefCount == 1);
145
/* init on first use */
146
if (mPageSize == -1) {
148
mPageSize = sysconf(_SC_PAGESIZE);
149
if (mPageSize == -1) {
150
ALOGE("could not get _SC_PAGESIZE\n");
154
/* this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM */
159
adjust = offset % mPageSize;
161
adjOffset = offset - adjust;
162
adjLength = length + adjust;
169
ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset);
170
if (ptr == MAP_FAILED) {
171
// Cygwin does not seem to like file mapping files from an offset.
172
// So if we fail, try again with offset zero
178
ALOGE("mmap(%ld,%ld) failed: %s\n",
179
(long) adjOffset, (long) adjLength, strerror(errno));
183
#endif /* HAVE_POSIX_FILEMAP */
185
mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
186
mBaseLength = adjLength;
187
mDataOffset = offset;
188
mDataPtr = (char*) mBasePtr + adjust;
189
mDataLength = length;
191
assert(mBasePtr != NULL);
193
ALOGV("MAP: base %p/%d data %p/%d\n",
194
mBasePtr, (int) mBaseLength, mDataPtr, (int) mDataLength);
200
* Provide guidance to the system.
202
int FileMap::advise(MapAdvice advice)
208
case NORMAL: sysAdvice = MADV_NORMAL; break;
209
case RANDOM: sysAdvice = MADV_RANDOM; break;
210
case SEQUENTIAL: sysAdvice = MADV_SEQUENTIAL; break;
211
case WILLNEED: sysAdvice = MADV_WILLNEED; break;
212
case DONTNEED: sysAdvice = MADV_DONTNEED; break;
218
cc = madvise(mBasePtr, mBaseLength, sysAdvice);
220
ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
224
#endif // HAVE_MADVISE