am 25f07f77: Merge changes I487ebe67,Ie7a0aeb2

* commit '25f07f77eceba32a5085a6ff9b70081352d3fdb9':
  libutils: FileMap styling adjustments
  libutils: FileMap 64 bit compile issues
This commit is contained in:
Mark Salyzyn 2014-04-17 20:52:30 +00:00 committed by Android Git Automerger
commit 7fb0ff53e8

View file

@ -23,6 +23,7 @@
#include <utils/FileMap.h> #include <utils/FileMap.h>
#include <utils/Log.h> #include <utils/Log.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -39,37 +40,32 @@ using namespace android;
/*static*/ long FileMap::mPageSize = -1; /*static*/ long FileMap::mPageSize = -1;
// Constructor. Create an empty object.
/*
* Constructor. Create an empty object.
*/
FileMap::FileMap(void) FileMap::FileMap(void)
: mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0), : mRefCount(1), mFileName(NULL), mBasePtr(NULL), mBaseLength(0),
mDataPtr(NULL), mDataLength(0) mDataPtr(NULL), mDataLength(0)
{ {
} }
/* // Destructor.
* Destructor.
*/
FileMap::~FileMap(void) FileMap::~FileMap(void)
{ {
assert(mRefCount == 0); assert(mRefCount == 0);
//printf("+++ removing FileMap %p %u\n", mDataPtr, mDataLength); //printf("+++ removing FileMap %p %zu\n", mDataPtr, mDataLength);
mRefCount = -100; // help catch double-free mRefCount = -100; // help catch double-free
if (mFileName != NULL) { if (mFileName != NULL) {
free(mFileName); free(mFileName);
} }
#ifdef HAVE_POSIX_FILEMAP #ifdef HAVE_POSIX_FILEMAP
if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) { if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) {
ALOGD("munmap(%p, %d) failed\n", mBasePtr, (int) mBaseLength); ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength);
} }
#endif #endif
#ifdef HAVE_WIN32_FILEMAP #ifdef HAVE_WIN32_FILEMAP
if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) { if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
ALOGD("UnmapViewOfFile(%p) failed, error = %ld\n", mBasePtr, ALOGD("UnmapViewOfFile(%p) failed, error = %" PRId32 "\n", mBasePtr,
GetLastError() ); GetLastError() );
} }
if (mFileMapping != INVALID_HANDLE_VALUE) { if (mFileMapping != INVALID_HANDLE_VALUE) {
@ -80,14 +76,12 @@ FileMap::~FileMap(void)
} }
/* // Create a new mapping on an open file.
* Create a new mapping on an open file. //
* // Closing the file descriptor does not unmap the pages, so we don't
* Closing the file descriptor does not unmap the pages, so we don't // claim ownership of the fd.
* claim ownership of the fd. //
* // Returns "false" on failure.
* Returns "false" on failure.
*/
bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length, bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length,
bool readOnly) bool readOnly)
{ {
@ -98,32 +92,32 @@ bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t le
if (mPageSize == -1) { if (mPageSize == -1) {
SYSTEM_INFO si; SYSTEM_INFO si;
GetSystemInfo( &si ); GetSystemInfo( &si );
mPageSize = si.dwAllocationGranularity; mPageSize = si.dwAllocationGranularity;
} }
DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE; DWORD protect = readOnly ? PAGE_READONLY : PAGE_READWRITE;
mFileHandle = (HANDLE) _get_osfhandle(fd); mFileHandle = (HANDLE) _get_osfhandle(fd);
mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL); mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
if (mFileMapping == NULL) { if (mFileMapping == NULL) {
ALOGE("CreateFileMapping(%p, %lx) failed with error %ld\n", ALOGE("CreateFileMapping(%p, %" PRIx32 ") failed with error %" PRId32 "\n",
mFileHandle, protect, GetLastError() ); mFileHandle, protect, GetLastError() );
return false; return false;
} }
adjust = offset % mPageSize; adjust = offset % mPageSize;
adjOffset = offset - adjust; adjOffset = offset - adjust;
adjLength = length + adjust; adjLength = length + adjust;
mBasePtr = MapViewOfFile( mFileMapping, mBasePtr = MapViewOfFile( mFileMapping,
readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS,
0, 0,
(DWORD)(adjOffset), (DWORD)(adjOffset),
adjLength ); adjLength );
if (mBasePtr == NULL) { if (mBasePtr == NULL) {
ALOGE("MapViewOfFile(%ld, %ld) failed with error %ld\n", ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %" PRId32 "\n",
adjOffset, adjLength, GetLastError() ); adjOffset, adjLength, GetLastError() );
CloseHandle(mFileMapping); CloseHandle(mFileMapping);
mFileMapping = INVALID_HANDLE_VALUE; mFileMapping = INVALID_HANDLE_VALUE;
@ -142,7 +136,7 @@ bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t le
assert(offset >= 0); assert(offset >= 0);
assert(length > 0); assert(length > 0);
/* init on first use */ // init on first use
if (mPageSize == -1) { if (mPageSize == -1) {
#if NOT_USING_KLIBC #if NOT_USING_KLIBC
mPageSize = sysconf(_SC_PAGESIZE); mPageSize = sysconf(_SC_PAGESIZE);
@ -151,7 +145,7 @@ bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t le
return false; return false;
} }
#else #else
/* this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM */ // this holds for Linux, Darwin, Cygwin, and doesn't pain the ARM
mPageSize = 4096; mPageSize = 4096;
#endif #endif
} }
@ -168,19 +162,19 @@ try_again:
ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset); ptr = mmap(NULL, adjLength, prot, flags, fd, adjOffset);
if (ptr == MAP_FAILED) { if (ptr == MAP_FAILED) {
// Cygwin does not seem to like file mapping files from an offset. // Cygwin does not seem to like file mapping files from an offset.
// So if we fail, try again with offset zero // So if we fail, try again with offset zero
if (adjOffset > 0) { if (adjOffset > 0) {
adjust = offset; adjust = offset;
goto try_again; goto try_again;
} }
ALOGE("mmap(%ld,%ld) failed: %s\n", ALOGE("mmap(%" PRId64 ",%zu) failed: %s\n",
(long) adjOffset, (long) adjLength, strerror(errno)); adjOffset, adjLength, strerror(errno));
return false; return false;
} }
mBasePtr = ptr; mBasePtr = ptr;
#endif /* HAVE_POSIX_FILEMAP */ #endif // HAVE_POSIX_FILEMAP
mFileName = origFileName != NULL ? strdup(origFileName) : NULL; mFileName = origFileName != NULL ? strdup(origFileName) : NULL;
mBaseLength = adjLength; mBaseLength = adjLength;
@ -190,15 +184,13 @@ try_again:
assert(mBasePtr != NULL); assert(mBasePtr != NULL);
ALOGV("MAP: base %p/%d data %p/%d\n", ALOGV("MAP: base %p/%zu data %p/%zu\n",
mBasePtr, (int) mBaseLength, mDataPtr, (int) mDataLength); mBasePtr, mBaseLength, mDataPtr, mDataLength);
return true; return true;
} }
/* // Provide guidance to the system.
* Provide guidance to the system.
*/
int FileMap::advise(MapAdvice advice) int FileMap::advise(MapAdvice advice)
{ {
#if HAVE_MADVISE #if HAVE_MADVISE
@ -220,6 +212,6 @@ int FileMap::advise(MapAdvice advice)
ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno)); ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno));
return cc; return cc;
#else #else
return -1; return -1;
#endif // HAVE_MADVISE #endif // HAVE_MADVISE
} }