Create a new mapping on an open file. Closing the file descriptor does not unmap the pages, so we don't claim ownership of the fd. Returns "false" on failure.
| 127 | // |
| 128 | // Returns "false" on failure. |
| 129 | bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length, |
| 130 | bool readOnly) |
| 131 | { |
| 132 | #if defined(__MINGW32__) |
| 133 | int adjust; |
| 134 | off64_t adjOffset; |
| 135 | size_t adjLength; |
| 136 | |
| 137 | if (mPageSize == -1) { |
| 138 | SYSTEM_INFO si; |
| 139 | |
| 140 | GetSystemInfo( &si ); |
| 141 | mPageSize = si.dwAllocationGranularity; |
| 142 | } |
| 143 | |
| 144 | DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE; |
| 145 | |
| 146 | mFileHandle = (HANDLE) _get_osfhandle(fd); |
| 147 | mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL); |
| 148 | if (mFileMapping == NULL) { |
| 149 | ALOGE("CreateFileMapping(%p, %lx) failed with error %lu\n", |
| 150 | mFileHandle, protect, GetLastError() ); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | adjust = offset % mPageSize; |
| 155 | adjOffset = offset - adjust; |
| 156 | adjLength = length + adjust; |
| 157 | |
| 158 | mBasePtr = MapViewOfFile( mFileMapping, |
| 159 | readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, |
| 160 | 0, |
| 161 | (DWORD)(adjOffset), |
| 162 | adjLength ); |
| 163 | if (mBasePtr == NULL) { |
| 164 | ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %lu\n", |
| 165 | adjOffset, adjLength, GetLastError() ); |
| 166 | CloseHandle(mFileMapping); |
| 167 | mFileMapping = NULL; |
| 168 | return false; |
| 169 | } |
| 170 | #else // !defined(__MINGW32__) |
| 171 | int prot, flags, adjust; |
| 172 | off64_t adjOffset; |
| 173 | size_t adjLength; |
| 174 | |
| 175 | void* ptr; |
| 176 | |
| 177 | assert(fd >= 0); |
| 178 | assert(offset >= 0); |
| 179 | assert(length > 0); |
| 180 | |
| 181 | // init on first use |
| 182 | if (mPageSize == -1) { |
| 183 | mPageSize = sysconf(_SC_PAGESIZE); |
| 184 | if (mPageSize == -1) { |
| 185 | //chensenhua ALOGE("could not get _SC_PAGESIZE\n"); |
| 186 | return false; |
no outgoing calls
no test coverage detected