Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / disk_cache / blockfile / mapped_file.h
blob7634cb70f46dfe7aa0cc0c7c754f47ee3f15c93b
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.
5 // See net/disk_cache/disk_cache.h for the public interface of the cache.
7 #ifndef NET_DISK_CACHE_BLOCKFILE_MAPPED_FILE_H_
8 #define NET_DISK_CACHE_BLOCKFILE_MAPPED_FILE_H_
10 #include "net/base/net_export.h"
11 #include "net/disk_cache/blockfile/file.h"
12 #include "net/disk_cache/blockfile/file_block.h"
14 namespace base {
15 class FilePath;
18 namespace disk_cache {
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) {}
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);
33 void* buffer() const {
34 return buffer_;
37 // Loads or stores a given block from the backing file (synchronously).
38 bool Load(const FileBlock* block);
39 bool Store(const FileBlock* block);
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);
46 // Flush the memory-mapped section to disk (synchronously).
47 void Flush();
49 // Heats up the file system cache and make sure the file is fully
50 // readable (synchronously).
51 bool Preload();
53 private:
54 ~MappedFile() override;
56 bool init_;
57 #if defined(OS_WIN)
58 HANDLE section_;
59 #endif
60 void* buffer_; // Address of the memory mapped buffer.
61 size_t view_size_; // Size of the memory pointed by buffer_.
62 #if defined(POSIX_AVOID_MMAP)
63 void* snapshot_; // Copy of the buffer taken when it was last flushed.
64 #endif
66 DISALLOW_COPY_AND_ASSIGN(MappedFile);
69 // Helper class for calling Flush() on exit from the current scope.
70 class ScopedFlush {
71 public:
72 explicit ScopedFlush(MappedFile* file) : file_(file) {}
73 ~ScopedFlush() {
74 file_->Flush();
76 private:
77 MappedFile* file_;
80 } // namespace disk_cache
82 #endif // NET_DISK_CACHE_BLOCKFILE_MAPPED_FILE_H_