From d36ec3afdac0ca4158a786b96599ee8bdf64b043 Mon Sep 17 00:00:00 2001 From: Jeff Brown Date: Fri, 19 Nov 2010 13:13:07 -0800 Subject: [PATCH] Fix SDK build on Windows due to use of mmap. Change-Id: Id4bd9a6f932285c93c5853e540efc20b99876564 --- include/utils/Tokenizer.h | 4 +++- libs/utils/Tokenizer.cpp | 32 +++++++++++++++++--------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/include/utils/Tokenizer.h b/include/utils/Tokenizer.h index bfe89232d..21e58e62c 100644 --- a/include/utils/Tokenizer.h +++ b/include/utils/Tokenizer.h @@ -19,6 +19,7 @@ #include #include +#include #include namespace android { @@ -27,7 +28,7 @@ namespace android { * A simple tokenizer for loading and parsing ASCII text files line by line. */ class Tokenizer { - Tokenizer(const String8& filename, const char* buffer, size_t length); + Tokenizer(const String8& filename, FileMap* fileMap, const char* buffer, size_t length); public: ~Tokenizer(); @@ -108,6 +109,7 @@ private: Tokenizer(const Tokenizer& other); // not copyable String8 mFilename; + FileMap* mFileMap; const char* mBuffer; size_t mLength; diff --git a/libs/utils/Tokenizer.cpp b/libs/utils/Tokenizer.cpp index 19dadf08d..9251973ca 100644 --- a/libs/utils/Tokenizer.cpp +++ b/libs/utils/Tokenizer.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include @@ -37,13 +36,16 @@ static inline bool isDelimiter(char ch, const char* delimiters) { } -Tokenizer::Tokenizer(const String8& filename, const char* buffer, size_t length) : - mFilename(filename), mBuffer(buffer), mLength(length), +Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, + const char* buffer, size_t length) : + mFilename(filename), mFileMap(fileMap), mBuffer(buffer), mLength(length), mCurrent(buffer), mLineNumber(1) { } Tokenizer::~Tokenizer() { - munmap((void*)mBuffer, mLength); + if (mFileMap) { + mFileMap->release(); + } } status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) { @@ -55,29 +57,29 @@ status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) { result = -errno; LOGE("Error opening file '%s', %s.", filename.string(), strerror(errno)); } else { - struct stat64 stat; - if (fstat64(fd, &stat)) { + struct stat stat; + if (fstat(fd, &stat)) { result = -errno; LOGE("Error getting size of file '%s', %s.", filename.string(), strerror(errno)); } else { size_t length = size_t(stat.st_size); - void* buffer = mmap(NULL, length, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); - if (buffer == MAP_FAILED) { - result = -errno; + FileMap* fileMap = new FileMap(); + if (!fileMap->create(NULL, fd, 0, length, true)) { + result = NO_MEMORY; LOGE("Error mapping file '%s', %s.", filename.string(), strerror(errno)); } else { - if (madvise(buffer, length, MADV_SEQUENTIAL)) { - LOGW("Error calling madvise for mmapped file '%s', %s.", filename.string(), - strerror(errno)); - } + fileMap->advise(FileMap::SEQUENTIAL); - *outTokenizer = new Tokenizer(filename, static_cast(buffer), length); + *outTokenizer = new Tokenizer(filename, fileMap, + static_cast(fileMap->getDataPtr()), length); if (!*outTokenizer) { result = NO_MEMORY; LOGE("Error allocating tokenizer for file=%s.", filename.string()); - munmap(buffer, length); } } + if (result) { + fileMap->release(); + } } close(fd); }