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_BLOCKFILE_MAPPED_FILE_H_ 8 #define NET_DISK_CACHE_BLOCKFILE_MAPPED_FILE_H_ 9 10 #include <stddef.h> 11 12 #include "base/macros.h" 13 #include "net/base/net_export.h" 14 #include "net/disk_cache/blockfile/file.h" 15 #include "net/disk_cache/blockfile/file_block.h" 16 #include "net/net_buildflags.h" 17 18 namespace base { 19 class FilePath; 20 } 21 22 namespace disk_cache { 23 24 // This class implements a memory mapped file used to access block-files. The 25 // idea is that the header and bitmap will be memory mapped all the time, and 26 // the actual data for the blocks will be access asynchronously (most of the 27 // time). 28 class NET_EXPORT_PRIVATE MappedFile : public File { 29 public: MappedFile()30 MappedFile() : File(true), init_(false) {} 31 32 // Performs object initialization. name is the file to use, and size is the 33 // amount of data to memory map from the file. If size is 0, the whole file 34 // will be mapped in memory. 35 void* Init(const base::FilePath& name, size_t size); 36 buffer()37 void* buffer() const { 38 return buffer_; 39 } 40 41 // Loads or stores a given block from the backing file (synchronously). 42 bool Load(const FileBlock* block); 43 bool Store(const FileBlock* block); 44 45 // Flush the memory-mapped section to disk (synchronously). 46 void Flush(); 47 48 // Heats up the file system cache and make sure the file is fully 49 // readable (synchronously). 50 bool Preload(); 51 52 private: 53 ~MappedFile() override; 54 55 bool init_; 56 #if defined(OS_WIN) 57 HANDLE section_; 58 #endif 59 void* buffer_; // Address of the memory mapped buffer. 60 size_t view_size_; // Size of the memory pointed by buffer_. 61 #if BUILDFLAG(POSIX_AVOID_MMAP) 62 void* snapshot_; // Copy of the buffer taken when it was last flushed. 63 #endif 64 65 DISALLOW_COPY_AND_ASSIGN(MappedFile); 66 }; 67 68 // Helper class for calling Flush() on exit from the current scope. 69 class ScopedFlush { 70 public: ScopedFlush(MappedFile * file)71 explicit ScopedFlush(MappedFile* file) : file_(file) {} ~ScopedFlush()72 ~ScopedFlush() { 73 file_->Flush(); 74 } 75 private: 76 MappedFile* file_; 77 }; 78 79 } // namespace disk_cache 80 81 #endif // NET_DISK_CACHE_BLOCKFILE_MAPPED_FILE_H_ 82