FileMap.cpp revision 5bed8036644f552210a7cfcbed2d6d20cf2981b0
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
45using namespace android;
46
47/*static*/ long FileMap::mPageSize = -1;
48
49// Constructor.  Create an empty object.
50FileMap::FileMap(void)
51    : mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
52      mDataPtr(NULL), mDataLength(0)
53{
54}
55
56// Destructor.
57FileMap::~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    CloseHandle(mFileHandle);
81#endif
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.
91bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
92        bool readOnly)
93{
94#ifdef HAVE_WIN32_FILEMAP
95    int     adjust;
96    off64_t adjOffset;
97    size_t  adjLength;
98
99    if (mPageSize == -1) {
100        SYSTEM_INFO  si;
101
102        GetSystemInfo( &si );
103        mPageSize = si.dwAllocationGranularity;
104    }
105
106    DWORD  protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
107
108    mFileHandle  = (HANDLE) _get_osfhandle(fd);
109    mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
110    if (mFileMapping == NULL) {
111        ALOGE("CreateFileMapping(%p, %" PRIx32 ") failed with error %" PRId32 "\n",
112              mFileHandle, protect, GetLastError() );
113        return false;
114    }
115
116    adjust    = offset % mPageSize;
117    adjOffset = offset - adjust;
118    adjLength = length + adjust;
119
120    mBasePtr = MapViewOfFile( mFileMapping,
121                              readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
122                              0,
123                              (DWORD)(adjOffset),
124                              adjLength );
125    if (mBasePtr == NULL) {
126        ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %" PRId32 "\n",
127              adjOffset, adjLength, GetLastError() );
128        CloseHandle(mFileMapping);
129        mFileMapping = INVALID_HANDLE_VALUE;
130        return false;
131    }
132#endif
133#ifdef HAVE_POSIX_FILEMAP
134    int     prot, flags, adjust;
135    off64_t adjOffset;
136    size_t  adjLength;
137
138    void* ptr;
139
140    assert(mRefCount == 1);
141    assert(fd >= 0);
142    assert(offset >= 0);
143    assert(length > 0);
144
145    // init on first use
146    if (mPageSize == -1) {
147#if NOT_USING_KLIBC
148        mPageSize = sysconf(_SC_PAGESIZE);
149        if (mPageSize == -1) {
150            ALOGE("could not get _SC_PAGESIZE\n");
151            return false;
152        }
153#else
154        // this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM
155        mPageSize = 4096;
156#endif
157    }
158
159    adjust   = offset % mPageSize;
160try_again:
161    adjOffset = offset - adjust;
162    adjLength = length + adjust;
163
164    flags = MAP_SHARED;
165    prot = PROT_READ;
166    if (!readOnly)
167        prot |= PROT_WRITE;
168
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
173        if (adjOffset > 0) {
174            adjust = offset;
175            goto try_again;
176        }
177
178        ALOGE("mmap(%lld,%zu) failed: %s\n",
179            (long long)adjOffset, adjLength, strerror(errno));
180        return false;
181    }
182    mBasePtr = ptr;
183#endif // HAVE_POSIX_FILEMAP
184
185    mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
186    mBaseLength = adjLength;
187    mDataOffset = offset;
188    mDataPtr = (char*) mBasePtr + adjust;
189    mDataLength = length;
190
191    assert(mBasePtr != NULL);
192
193    ALOGV("MAP: base %p/%zu data %p/%zu\n",
194        mBasePtr, mBaseLength, mDataPtr, mDataLength);
195
196    return true;
197}
198
199// Provide guidance to the system.
200int FileMap::advise(MapAdvice advice)
201{
202#if HAVE_MADVISE
203    int cc, sysAdvice;
204
205    switch (advice) {
206        case NORMAL:        sysAdvice = MADV_NORMAL;        break;
207        case RANDOM:        sysAdvice = MADV_RANDOM;        break;
208        case SEQUENTIAL:    sysAdvice = MADV_SEQUENTIAL;    break;
209        case WILLNEED:      sysAdvice = MADV_WILLNEED;      break;
210        case DONTNEED:      sysAdvice = MADV_DONTNEED;      break;
211        default:
212                            assert(false);
213                            return -1;
214    }
215
216    cc = madvise(mBasePtr, mBaseLength, sysAdvice);
217    if (cc != 0)
218        ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
219    return cc;
220#else
221    return -1;
222#endif // HAVE_MADVISE
223}
224