Fix SDK build on Windows due to use of mmap.
Change-Id: Id4bd9a6f932285c93c5853e540efc20b99876564
This commit is contained in:
parent
647925ddf0
commit
d36ec3afda
2 changed files with 20 additions and 16 deletions
|
@ -19,6 +19,7 @@
|
|||
|
||||
#include <assert.h>
|
||||
#include <utils/Errors.h>
|
||||
#include <utils/FileMap.h>
|
||||
#include <utils/String8.h>
|
||||
|
||||
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;
|
||||
|
||||
|
|
|
@ -22,7 +22,6 @@
|
|||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <utils/Log.h>
|
||||
#include <utils/Tokenizer.h>
|
||||
|
||||
|
@ -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<const char*>(buffer), length);
|
||||
*outTokenizer = new Tokenizer(filename, fileMap,
|
||||
static_cast<const char*>(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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue