Optionally disable mmap() in the disk cache.

The disk cache mmaps the headers of certain important files
(the main index file, plus block files). Unfortunately on
some Android devices mmap performs badly on flash storage,
and it's actually better to write the data manually.

This patch adds the macro USE_MMAP_FOR_DISK_CACHE macro and
the method disk_cache::MappedFile::FlushHeader(). By default,
the macro is defined, the new method is a no-op, and there's
no change in behavior.

TEST=DiskCacheTest

BUG=


Review URL: https://chromiumcodereview.appspot.com/10573032

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@144166 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/net/disk_cache/mapped_file.h b/net/disk_cache/mapped_file.h
index 7886ad7..1e794bd 100644
--- a/net/disk_cache/mapped_file.h
+++ b/net/disk_cache/mapped_file.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -26,7 +26,7 @@
   MappedFile() : File(true), init_(false) {}
 
   // Performs object initialization. name is the file to use, and size is the
-  // ammount of data to memory map from th efile. If size is 0, the whole file
+  // amount of data to memory map from the file. If size is 0, the whole file
   // will be mapped in memory.
   void* Init(const FilePath& name, size_t size);
 
@@ -38,6 +38,9 @@
   bool Load(const FileBlock* block);
   bool Store(const FileBlock* block);
 
+  // Flush the memory-mapped section to disk (synchronously).
+  void Flush();
+
  private:
   virtual ~MappedFile();
 
@@ -47,10 +50,24 @@
 #endif
   void* buffer_;  // Address of the memory mapped buffer.
   size_t view_size_;  // Size of the memory pointed by buffer_.
+#if defined(POSIX_AVOID_MMAP)
+  void* snapshot_;  // Copy of the buffer taken when it was last flushed.
+#endif
 
   DISALLOW_COPY_AND_ASSIGN(MappedFile);
 };
 
+// Helper class for calling Flush() on exit from the current scope.
+class ScopedFlush {
+ public:
+  explicit ScopedFlush(MappedFile* file) : file_(file) {}
+  ~ScopedFlush() {
+    file_->Flush();
+  }
+ private:
+  MappedFile* file_;
+};
+
 }  // namespace disk_cache
 
 #endif  // NET_DISK_CACHE_MAPPED_FILE_H_