summaryrefslogtreecommitdiffstats
path: root/base/file_util_posix.cc
diff options
context:
space:
mode:
authorestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-12 22:05:23 +0000
committerestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-12 22:05:23 +0000
commitcf0d278d5ad746f025a7cf687dcc019b4c09421e (patch)
tree0b0b4627d132e56a7230ab07a4d98c5064359d5e /base/file_util_posix.cc
parent620b79cee22f6376851f63989e319157f23d5556 (diff)
downloadchromium_src-cf0d278d5ad746f025a7cf687dcc019b4c09421e.zip
chromium_src-cf0d278d5ad746f025a7cf687dcc019b4c09421e.tar.gz
chromium_src-cf0d278d5ad746f025a7cf687dcc019b4c09421e.tar.bz2
Implement the memory mapped file class for posix.
Review URL: http://codereview.chromium.org/14073 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6935 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util_posix.cc')
-rw-r--r--base/file_util_posix.cc40
1 files changed, 40 insertions, 0 deletions
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index 153f70a..7448ed8 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -12,6 +12,7 @@
#include <stdio.h>
#include <string.h>
#include <sys/errno.h>
+#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
@@ -378,6 +379,9 @@ bool SetCurrentDirectory(const FilePath& path) {
return !ret;
}
+///////////////////////////////////////////////
+// FileEnumerator
+
FileEnumerator::FileEnumerator(const FilePath& root_path,
bool recursive,
FileEnumerator::FILE_TYPE file_type)
@@ -479,5 +483,41 @@ FilePath FileEnumerator::Next() {
return Next();
}
+///////////////////////////////////////////////
+// MemoryMappedFile
+
+MemoryMappedFile::MemoryMappedFile()
+ : file_(-1),
+ data_(NULL),
+ length_(0) {
+}
+
+bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) {
+ file_ = open(file_name.value().c_str(), O_RDONLY);
+ if (file_ == -1)
+ return false;
+
+ struct stat file_stat;
+ if (fstat(file_, &file_stat) == -1)
+ return false;
+ length_ = file_stat.st_size;
+
+ data_ = static_cast<uint8*>(
+ mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0));
+ if (data_ == MAP_FAILED)
+ data_ = NULL;
+ return data_ != NULL;
+}
+
+void MemoryMappedFile::CloseHandles() {
+ if (data_ != NULL)
+ munmap(data_, length_);
+ if (file_ != -1)
+ close(file_);
+
+ data_ = NULL;
+ length_ = 0;
+ file_ = -1;
+}
} // namespace file_util