[email protected] | 9e079cb | 2012-06-26 13:38:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | // See net/disk_cache/disk_cache.h for the public interface of the cache. |
| 6 | |
[email protected] | 1eb89e8 | 2008-08-15 12:27:03 | [diff] [blame] | 7 | #ifndef NET_DISK_CACHE_MAPPED_FILE_H_ |
| 8 | #define NET_DISK_CACHE_MAPPED_FILE_H_ |
| 9 | |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 10 | #include "net/base/net_export.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 11 | #include "net/disk_cache/file.h" |
| 12 | #include "net/disk_cache/file_block.h" |
| 13 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 14 | namespace base { |
[email protected] | 4317c71 | 2009-10-13 23:02:37 | [diff] [blame] | 15 | class FilePath; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 16 | } |
[email protected] | 4317c71 | 2009-10-13 23:02:37 | [diff] [blame] | 17 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 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). |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 24 | class NET_EXPORT_PRIVATE MappedFile : public File { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 25 | public: |
[email protected] | f44bb38 | 2008-08-19 19:47:42 | [diff] [blame] | 26 | MappedFile() : File(true), init_(false) {} |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | |
| 28 | // Performs object initialization. name is the file to use, and size is the |
[email protected] | 9e079cb | 2012-06-26 13:38:02 | [diff] [blame] | 29 | // amount of data to memory map from the file. If size is 0, the whole file |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 30 | // will be mapped in memory. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 31 | void* Init(const base::FilePath& name, size_t size); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 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 | |
[email protected] | 66901eb4 | 2013-09-11 22:18:52 | [diff] [blame] | 41 | // Asynchronous versions of Load/Store, following the semantics of File::Read |
| 42 | // and File::Write. |
| 43 | bool Load(const FileBlock* block, FileIOCallback* callback, bool* completed); |
| 44 | bool Store(const FileBlock* block, FileIOCallback* callback, bool* completed); |
| 45 | |
[email protected] | 9e079cb | 2012-06-26 13:38:02 | [diff] [blame] | 46 | // Flush the memory-mapped section to disk (synchronously). |
| 47 | void Flush(); |
| 48 | |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 49 | private: |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | virtual ~MappedFile(); |
| 51 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 52 | bool init_; |
[email protected] | 408d35f5 | 2008-08-13 18:30:22 | [diff] [blame] | 53 | #if defined(OS_WIN) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | HANDLE section_; |
[email protected] | 408d35f5 | 2008-08-13 18:30:22 | [diff] [blame] | 55 | #endif |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 56 | void* buffer_; // Address of the memory mapped buffer. |
| 57 | size_t view_size_; // Size of the memory pointed by buffer_. |
[email protected] | 8a25d54 | 2013-07-11 17:27:15 | [diff] [blame] | 58 | #if defined(POSIX_AVOID_MMAP) |
| 59 | void* snapshot_; // Copy of the buffer taken when it was last flushed. |
| 60 | #endif |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 61 | |
[email protected] | 1eb89e8 | 2008-08-15 12:27:03 | [diff] [blame] | 62 | DISALLOW_COPY_AND_ASSIGN(MappedFile); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | }; |
| 64 | |
[email protected] | 9e079cb | 2012-06-26 13:38:02 | [diff] [blame] | 65 | // Helper class for calling Flush() on exit from the current scope. |
| 66 | class ScopedFlush { |
| 67 | public: |
| 68 | explicit ScopedFlush(MappedFile* file) : file_(file) {} |
| 69 | ~ScopedFlush() { |
| 70 | file_->Flush(); |
| 71 | } |
| 72 | private: |
| 73 | MappedFile* file_; |
| 74 | }; |
| 75 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 76 | } // namespace disk_cache |
| 77 | |
[email protected] | 1eb89e8 | 2008-08-15 12:27:03 | [diff] [blame] | 78 | #endif // NET_DISK_CACHE_MAPPED_FILE_H_ |