Home | History | Annotate | Download | only in disk_cache
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // See net/disk_cache/disk_cache.h for the public interface of the cache.
      6 
      7 #ifndef NET_DISK_CACHE_MAPPED_FILE_H_
      8 #define NET_DISK_CACHE_MAPPED_FILE_H_
      9 
     10 #include "net/base/net_export.h"
     11 #include "net/disk_cache/file.h"
     12 #include "net/disk_cache/file_block.h"
     13 
     14 namespace base {
     15 class FilePath;
     16 }
     17 
     18 namespace disk_cache {
     19 
     20 // This class implements a memory mapped file used to access block-files. The
     21 // idea is that the header and bitmap will be memory mapped all the time, and
     22 // the actual data for the blocks will be access asynchronously (most of the
     23 // time).
     24 class NET_EXPORT_PRIVATE MappedFile : public File {
     25  public:
     26   MappedFile() : File(true), init_(false) {}
     27 
     28   // Performs object initialization. name is the file to use, and size is the
     29   // amount of data to memory map from the file. If size is 0, the whole file
     30   // will be mapped in memory.
     31   void* Init(const base::FilePath& name, size_t size);
     32 
     33   void* buffer() const {
     34     return buffer_;
     35   }
     36 
     37   // Loads or stores a given block from the backing file (synchronously).
     38   bool Load(const FileBlock* block);
     39   bool Store(const FileBlock* block);
     40 
     41   // Flush the memory-mapped section to disk (synchronously).
     42   void Flush();
     43 
     44  private:
     45   virtual ~MappedFile();
     46 
     47   bool init_;
     48 #if defined(OS_WIN)
     49   HANDLE section_;
     50 #endif
     51   void* buffer_;  // Address of the memory mapped buffer.
     52   size_t view_size_;  // Size of the memory pointed by buffer_.
     53 #if defined(POSIX_AVOID_MMAP)
     54   void* snapshot_;  // Copy of the buffer taken when it was last flushed.
     55 #endif
     56 
     57   DISALLOW_COPY_AND_ASSIGN(MappedFile);
     58 };
     59 
     60 // Helper class for calling Flush() on exit from the current scope.
     61 class ScopedFlush {
     62  public:
     63   explicit ScopedFlush(MappedFile* file) : file_(file) {}
     64   ~ScopedFlush() {
     65     file_->Flush();
     66   }
     67  private:
     68   MappedFile* file_;
     69 };
     70 
     71 }  // namespace disk_cache
     72 
     73 #endif  // NET_DISK_CACHE_MAPPED_FILE_H_
     74