blob: f6fe03f78d836b2ae6c7dfad971d7416be41c1ce [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
Yabin Cui266092c2014-11-11 10:31:30 -080026#if defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO)
Mark Salyzyn5bed8032014-04-30 11:10:46 -070027# define PRId32 "I32d"
28# define PRIx32 "I32x"
29# define PRId64 "I64d"
30#else
Mark Salyzyn15085a62014-04-17 09:34:42 -070031#include <inttypes.h>
Mark Salyzyn5bed8032014-04-30 11:10:46 -070032#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080033#include <stdio.h>
34#include <stdlib.h>
35
Yabin Cui266092c2014-11-11 10:31:30 -080036#if !defined(__MINGW32__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080037#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
Mark Salyzynb6185762014-04-17 10:01:12 -070049// Constructor. Create an empty object.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050FileMap::FileMap(void)
Renaud Paquayb7a4f0b2017-05-10 17:48:59 -070051 : mFileName(NULL),
52 mBasePtr(NULL),
53 mBaseLength(0),
54 mDataPtr(NULL),
55 mDataLength(0)
56#if defined(__MINGW32__)
57 ,
58 mFileHandle(INVALID_HANDLE_VALUE),
59 mFileMapping(NULL)
60#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080061{
62}
63
Adam Lesinski6f8885b2015-09-22 18:42:19 -070064// Move Constructor.
65FileMap::FileMap(FileMap&& other)
66 : mFileName(other.mFileName), mBasePtr(other.mBasePtr), mBaseLength(other.mBaseLength),
67 mDataOffset(other.mDataOffset), mDataPtr(other.mDataPtr), mDataLength(other.mDataLength)
68#if defined(__MINGW32__)
69 , mFileHandle(other.mFileHandle), mFileMapping(other.mFileMapping)
70#endif
71{
72 other.mFileName = NULL;
73 other.mBasePtr = NULL;
74 other.mDataPtr = NULL;
75#if defined(__MINGW32__)
Renaud Paquayb7a4f0b2017-05-10 17:48:59 -070076 other.mFileHandle = INVALID_HANDLE_VALUE;
77 other.mFileMapping = NULL;
Adam Lesinski6f8885b2015-09-22 18:42:19 -070078#endif
79}
80
81// Move assign operator.
82FileMap& FileMap::operator=(FileMap&& other) {
83 mFileName = other.mFileName;
84 mBasePtr = other.mBasePtr;
85 mBaseLength = other.mBaseLength;
86 mDataOffset = other.mDataOffset;
87 mDataPtr = other.mDataPtr;
88 mDataLength = other.mDataLength;
89 other.mFileName = NULL;
90 other.mBasePtr = NULL;
91 other.mDataPtr = NULL;
92#if defined(__MINGW32__)
93 mFileHandle = other.mFileHandle;
94 mFileMapping = other.mFileMapping;
Renaud Paquayb7a4f0b2017-05-10 17:48:59 -070095 other.mFileHandle = INVALID_HANDLE_VALUE;
96 other.mFileMapping = NULL;
Adam Lesinski6f8885b2015-09-22 18:42:19 -070097#endif
98 return *this;
99}
100
Mark Salyzynb6185762014-04-17 10:01:12 -0700101// Destructor.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800102FileMap::~FileMap(void)
103{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800104 if (mFileName != NULL) {
105 free(mFileName);
106 }
Yabin Cui266092c2014-11-11 10:31:30 -0800107#if defined(__MINGW32__)
Jeff Brown1d618d62010-12-02 13:50:46 -0800108 if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
Colin Cross17b5b822016-09-15 18:15:37 -0700109 ALOGD("UnmapViewOfFile(%p) failed, error = %lu\n", mBasePtr,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800110 GetLastError() );
111 }
Renaud Paquayb7a4f0b2017-05-10 17:48:59 -0700112 if (mFileMapping != NULL) {
Jeff Brown1d618d62010-12-02 13:50:46 -0800113 CloseHandle(mFileMapping);
114 }
Yabin Cui266092c2014-11-11 10:31:30 -0800115#else
116 if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
117 ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength);
118 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800119#endif
120}
121
122
Mark Salyzynb6185762014-04-17 10:01:12 -0700123// Create a new mapping on an open file.
124//
125// Closing the file descriptor does not unmap the pages, so we don't
126// claim ownership of the fd.
127//
128// Returns "false" on failure.
Kenny Roote2fa7dc2010-11-24 12:56:06 -0800129bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
130 bool readOnly)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800131{
Yabin Cui266092c2014-11-11 10:31:30 -0800132#if defined(__MINGW32__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800133 int adjust;
Kenny Roote2fa7dc2010-11-24 12:56:06 -0800134 off64_t adjOffset;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800135 size_t adjLength;
136
137 if (mPageSize == -1) {
138 SYSTEM_INFO si;
Mark Salyzynb6185762014-04-17 10:01:12 -0700139
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800140 GetSystemInfo( &si );
141 mPageSize = si.dwAllocationGranularity;
142 }
143
144 DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
Mark Salyzynb6185762014-04-17 10:01:12 -0700145
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800146 mFileHandle = (HANDLE) _get_osfhandle(fd);
147 mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
148 if (mFileMapping == NULL) {
Colin Cross17b5b822016-09-15 18:15:37 -0700149 ALOGE("CreateFileMapping(%p, %lx) failed with error %lu\n",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800150 mFileHandle, protect, GetLastError() );
151 return false;
152 }
Mark Salyzynb6185762014-04-17 10:01:12 -0700153
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800154 adjust = offset % mPageSize;
155 adjOffset = offset - adjust;
156 adjLength = length + adjust;
Mark Salyzynb6185762014-04-17 10:01:12 -0700157
158 mBasePtr = MapViewOfFile( mFileMapping,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800159 readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
160 0,
161 (DWORD)(adjOffset),
162 adjLength );
163 if (mBasePtr == NULL) {
Colin Cross17b5b822016-09-15 18:15:37 -0700164 ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %lu\n",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800165 adjOffset, adjLength, GetLastError() );
166 CloseHandle(mFileMapping);
Renaud Paquayb7a4f0b2017-05-10 17:48:59 -0700167 mFileMapping = NULL;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800168 return false;
169 }
Yabin Cui266092c2014-11-11 10:31:30 -0800170#else // !defined(__MINGW32__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800171 assert(fd >= 0);
172 assert(offset >= 0);
173 assert(length > 0);
174
Mark Salyzynb6185762014-04-17 10:01:12 -0700175 // init on first use
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800176 if (mPageSize == -1) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800177 mPageSize = sysconf(_SC_PAGESIZE);
178 if (mPageSize == -1) {
Steve Block1b781ab2012-01-06 19:20:56 +0000179 ALOGE("could not get _SC_PAGESIZE\n");
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800180 return false;
181 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800182 }
183
Elliott Hughesf04eb1c2020-08-17 12:37:25 -0700184 int adjust = offset % mPageSize;
185 off64_t adjOffset = offset - adjust;
186 size_t adjLength;
Christopher Ferris88bf88e2020-05-26 10:33:18 -0700187 if (__builtin_add_overflow(length, adjust, &adjLength)) {
188 ALOGE("adjusted length overflow: length %zu adjust %d", length, adjust);
189 return false;
190 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800191
Elliott Hughesf04eb1c2020-08-17 12:37:25 -0700192 int flags = MAP_SHARED;
193 int prot = PROT_READ;
194 if (!readOnly) prot |= PROT_WRITE;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800195
Elliott Hughesf04eb1c2020-08-17 12:37:25 -0700196 void* ptr = mmap(nullptr, adjLength, prot, flags, fd, adjOffset);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800197 if (ptr == MAP_FAILED) {
Mark Salyzyn5bed8032014-04-30 11:10:46 -0700198 ALOGE("mmap(%lld,%zu) failed: %s\n",
199 (long long)adjOffset, adjLength, strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800200 return false;
201 }
202 mBasePtr = ptr;
Yabin Cui266092c2014-11-11 10:31:30 -0800203#endif // !defined(__MINGW32__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800204
205 mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
206 mBaseLength = adjLength;
207 mDataOffset = offset;
208 mDataPtr = (char*) mBasePtr + adjust;
209 mDataLength = length;
210
211 assert(mBasePtr != NULL);
212
Mark Salyzyn15085a62014-04-17 09:34:42 -0700213 ALOGV("MAP: base %p/%zu data %p/%zu\n",
214 mBasePtr, mBaseLength, mDataPtr, mDataLength);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800215
216 return true;
217}
218
Mark Salyzynb6185762014-04-17 10:01:12 -0700219// Provide guidance to the system.
Yabin Cui745c5f62014-11-18 20:00:41 -0800220#if !defined(_WIN32)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800221int FileMap::advise(MapAdvice advice)
222{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800223 int cc, sysAdvice;
224
225 switch (advice) {
226 case NORMAL: sysAdvice = MADV_NORMAL; break;
227 case RANDOM: sysAdvice = MADV_RANDOM; break;
228 case SEQUENTIAL: sysAdvice = MADV_SEQUENTIAL; break;
229 case WILLNEED: sysAdvice = MADV_WILLNEED; break;
230 case DONTNEED: sysAdvice = MADV_DONTNEED; break;
231 default:
232 assert(false);
233 return -1;
234 }
235
236 cc = madvise(mBasePtr, mBaseLength, sysAdvice);
237 if (cc != 0)
Steve Block61d341b2012-01-05 23:22:43 +0000238 ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800239 return cc;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800240}
Yabin Cui745c5f62014-11-18 20:00:41 -0800241
242#else
243int FileMap::advise(MapAdvice /* advice */)
244{
245 return -1;
246}
247#endif