blob: ba7f4adae65f8b4967aa74abad3c02cd20ff0f87 [file] [log] [blame]
[email protected]9e079cb2012-06-26 13:38:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5// See net/disk_cache/disk_cache.h for the public interface of the cache.
6
[email protected]1eb89e82008-08-15 12:27:037#ifndef NET_DISK_CACHE_MAPPED_FILE_H_
8#define NET_DISK_CACHE_MAPPED_FILE_H_
9
[email protected]172da1b2011-08-12 15:52:2610#include "net/base/net_export.h"
initial.commit586acc5fe2008-07-26 22:42:5211#include "net/disk_cache/file.h"
12#include "net/disk_cache/file_block.h"
13
[email protected]a3ef4832013-02-02 05:12:3314namespace base {
[email protected]4317c712009-10-13 23:02:3715class FilePath;
[email protected]a3ef4832013-02-02 05:12:3316}
[email protected]4317c712009-10-13 23:02:3717
initial.commit586acc5fe2008-07-26 22:42:5218namespace 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]172da1b2011-08-12 15:52:2624class NET_EXPORT_PRIVATE MappedFile : public File {
initial.commit586acc5fe2008-07-26 22:42:5225 public:
[email protected]f44bb382008-08-19 19:47:4226 MappedFile() : File(true), init_(false) {}
initial.commit586acc5fe2008-07-26 22:42:5227
28 // Performs object initialization. name is the file to use, and size is the
[email protected]9e079cb2012-06-26 13:38:0229 // amount of data to memory map from the file. If size is 0, the whole file
initial.commit586acc5fe2008-07-26 22:42:5230 // will be mapped in memory.
[email protected]a3ef4832013-02-02 05:12:3331 void* Init(const base::FilePath& name, size_t size);
initial.commit586acc5fe2008-07-26 22:42:5232
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]66901eb42013-09-11 22:18:5241 // 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]9e079cb2012-06-26 13:38:0246 // Flush the memory-mapped section to disk (synchronously).
47 void Flush();
48
[email protected]5389bc72009-11-05 23:34:2449 private:
initial.commit586acc5fe2008-07-26 22:42:5250 virtual ~MappedFile();
51
initial.commit586acc5fe2008-07-26 22:42:5252 bool init_;
[email protected]408d35f52008-08-13 18:30:2253#if defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:5254 HANDLE section_;
[email protected]408d35f52008-08-13 18:30:2255#endif
initial.commit586acc5fe2008-07-26 22:42:5256 void* buffer_; // Address of the memory mapped buffer.
57 size_t view_size_; // Size of the memory pointed by buffer_.
[email protected]8a25d542013-07-11 17:27:1558#if defined(POSIX_AVOID_MMAP)
59 void* snapshot_; // Copy of the buffer taken when it was last flushed.
60#endif
initial.commit586acc5fe2008-07-26 22:42:5261
[email protected]1eb89e82008-08-15 12:27:0362 DISALLOW_COPY_AND_ASSIGN(MappedFile);
initial.commit586acc5fe2008-07-26 22:42:5263};
64
[email protected]9e079cb2012-06-26 13:38:0265// Helper class for calling Flush() on exit from the current scope.
66class ScopedFlush {
67 public:
68 explicit ScopedFlush(MappedFile* file) : file_(file) {}
69 ~ScopedFlush() {
70 file_->Flush();
71 }
72 private:
73 MappedFile* file_;
74};
75
initial.commit586acc5fe2008-07-26 22:42:5276} // namespace disk_cache
77
[email protected]1eb89e82008-08-15 12:27:0378#endif // NET_DISK_CACHE_MAPPED_FILE_H_