OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // See net/disk_cache/disk_cache.h for the public interface. | 5 // See net/disk_cache/disk_cache.h for the public interface. |
6 | 6 |
7 #ifndef NET_DISK_CACHE_BLOCK_FILES_H_ | 7 #ifndef NET_DISK_CACHE_BLOCK_FILES_H_ |
8 #define NET_DISK_CACHE_BLOCK_FILES_H_ | 8 #define NET_DISK_CACHE_BLOCK_FILES_H_ |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
16 #include "net/disk_cache/addr.h" | 16 #include "net/disk_cache/addr.h" |
| 17 #include "net/disk_cache/disk_format_base.h" |
17 #include "net/disk_cache/mapped_file.h" | 18 #include "net/disk_cache/mapped_file.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 class ThreadChecker; | 21 class ThreadChecker; |
21 } | 22 } |
22 | 23 |
23 namespace disk_cache { | 24 namespace disk_cache { |
24 | 25 |
| 26 // An instance of this class represents the header of a block file in memory. |
| 27 // Note that this class doesn't perform any file operation. |
| 28 class NET_EXPORT_PRIVATE BlockHeader { |
| 29 public: |
| 30 BlockHeader(); |
| 31 explicit BlockHeader(BlockFileHeader* header); |
| 32 explicit BlockHeader(MappedFile* file); |
| 33 BlockHeader(const BlockHeader& other); |
| 34 ~BlockHeader(); |
| 35 |
| 36 // Creates a new entry on the allocation map, updating the apropriate |
| 37 // counters. |target| is the type of block to use (number of empty blocks), |
| 38 // and |size| is the actual number of blocks to use. |
| 39 bool CreateMapBlock(int target, int size, int* index); |
| 40 |
| 41 // Deletes the block pointed by |index|. |
| 42 void DeleteMapBlock(int index, int block_size); |
| 43 |
| 44 // Returns true if the specified block is used. |
| 45 bool UsedMapBlock(int index, int size); |
| 46 |
| 47 // Restores the "empty counters" and allocation hints. |
| 48 void FixAllocationCounters(); |
| 49 |
| 50 // Returns true if the current block file should not be used as-is to store |
| 51 // more records. |block_count| is the number of blocks to allocate. |
| 52 bool NeedToGrowBlockFile(int block_count); |
| 53 |
| 54 // Returns true if this block file can be used to store an extra record of |
| 55 // size |block_count|. |
| 56 bool CanAllocate(int block_count); |
| 57 |
| 58 // Returns the number of empty blocks for this file. |
| 59 int EmptyBlocks() const; |
| 60 |
| 61 // Returns true if the counters look OK. |
| 62 bool ValidateCounters() const; |
| 63 |
| 64 // Returns the size of the wrapped structure (BlockFileHeader). |
| 65 int Size() const; |
| 66 |
| 67 BlockFileHeader* operator->() { return header_; } |
| 68 void operator=(const BlockHeader& other) { header_ = other.header_; } |
| 69 BlockFileHeader* Get() { return header_; } |
| 70 |
| 71 private: |
| 72 // Set force to true to overwrite the file if it exists. |
| 73 //bool CreateBlockFile(int index, FileType file_type, bool force); |
| 74 |
| 75 BlockFileHeader* header_; |
| 76 }; |
| 77 |
| 78 typedef std::vector<BlockHeader> BlockFilesBitmaps; |
| 79 |
25 // This class handles the set of block-files open by the disk cache. | 80 // This class handles the set of block-files open by the disk cache. |
26 class NET_EXPORT_PRIVATE BlockFiles { | 81 class NET_EXPORT_PRIVATE BlockFiles { |
27 public: | 82 public: |
28 explicit BlockFiles(const base::FilePath& path); | 83 explicit BlockFiles(const base::FilePath& path); |
29 ~BlockFiles(); | 84 ~BlockFiles(); |
30 | 85 |
31 // Performs the object initialization. create_files indicates if the backing | 86 // Performs the object initialization. create_files indicates if the backing |
32 // files should be created or just open. | 87 // files should be created or just open. |
33 bool Init(bool create_files); | 88 bool Init(bool create_files, int num_files); |
| 89 |
| 90 // Returns the allocation bitmaps for all the files managed by this object. |
| 91 void GetBitmaps(int num_files, BlockFilesBitmaps* bitmaps); |
34 | 92 |
35 // Returns the file that stores a given address. | 93 // Returns the file that stores a given address. |
36 MappedFile* GetFile(Addr address); | 94 MappedFile* GetFile(Addr address); |
37 | 95 |
38 // Creates a new entry on a block file. block_type indicates the size of block | 96 // Creates a new entry on a block file. block_type indicates the size of block |
39 // to be used (as defined on cache_addr.h), block_count is the number of | 97 // to be used (as defined on cache_addr.h), block_count is the number of |
40 // blocks to allocate, and block_address is the address of the new entry. | 98 // blocks to allocate, and block_address is the address of the new entry. |
41 bool CreateBlock(FileType block_type, int block_count, Addr* block_address); | 99 bool CreateBlock(FileType block_type, int block_count, Addr* block_address); |
42 | 100 |
43 // Removes an entry from the block files. If deep is true, the storage is zero | 101 // Removes an entry from the block files. If deep is true, the storage is zero |
44 // filled; otherwise the entry is removed but the data is not altered (must be | 102 // filled; otherwise the entry is removed but the data is not altered (must be |
45 // already zeroed). | 103 // already zeroed). |
46 void DeleteBlock(Addr address, bool deep); | 104 void DeleteBlock(Addr address, bool deep); |
47 | 105 |
48 // Close all the files and set the internal state to be initializad again. The | 106 // Close all the files and set the internal state to be initializad again. The |
49 // cache is being purged. | 107 // cache is being purged. |
50 void CloseFiles(); | 108 void CloseFiles(); |
51 | 109 |
52 // Sends UMA stats. | 110 // Sends UMA stats. |
53 void ReportStats(); | 111 void ReportStats(); |
54 | 112 |
55 // Returns true if the blocks pointed by a given address are currently used. | 113 // Returns true if the blocks pointed by a given address are currently used. |
56 // This method is only intended for debugging. | 114 // This method is only intended for debugging. |
57 bool IsValid(Addr address); | 115 bool IsValid(Addr address); |
58 | 116 |
| 117 // Increments the size of files very slowly. |
| 118 void UseSmallSizeIncrementsForTest() { small_steps_ = true; } |
| 119 |
59 private: | 120 private: |
60 // Set force to true to overwrite the file if it exists. | 121 // Set force to true to overwrite the file if it exists. |
61 bool CreateBlockFile(int index, FileType file_type, bool force); | 122 bool CreateBlockFile(int index, FileType file_type, bool force); |
62 bool OpenBlockFile(int index); | 123 bool OpenBlockFile(int index); |
63 | 124 |
64 // Attemp to grow this file. Fails if the file cannot be extended anymore. | 125 // Attemp to grow this file. Fails if the file cannot be extended anymore. |
65 bool GrowBlockFile(MappedFile* file, BlockFileHeader* header); | 126 bool GrowBlockFile(BlockFileHeader* header); |
66 | 127 |
67 // Returns the appropriate file to use for a new block. | 128 // Returns the appropriate file to use for a new block. |
68 MappedFile* FileForNewBlock(FileType block_type, int block_count); | 129 MappedFile* FileForNewBlock(FileType block_type, int block_count); |
69 | 130 |
70 // Returns the next block file on this chain, creating new files if needed. | 131 // Returns the next block file on this chain, creating new files if needed. |
71 MappedFile* NextFile(MappedFile* file); | 132 MappedFile* NextFile(MappedFile* file); |
72 | 133 |
| 134 // Returns the file index that stores a given address, or -1 on failure. |
| 135 int GetFileIndex(Addr address); |
| 136 |
| 137 // Returns the header file that stores a given address. |
| 138 MappedFile* GetFileHeader(Addr address); |
| 139 |
73 // Creates an empty block file and returns its index. | 140 // Creates an empty block file and returns its index. |
74 int CreateNextBlockFile(FileType block_type); | 141 int CreateNextBlockFile(FileType block_type); |
75 | 142 |
76 // Removes a chained block file that is now empty. | 143 // Removes a chained block file that is now empty. |
77 bool RemoveEmptyFile(FileType block_type); | 144 bool RemoveEmptyFile(FileType block_type); |
78 | 145 |
| 146 bool PreallocateSpace(FileType block_type); |
| 147 |
79 // Restores the header of a potentially inconsistent file. | 148 // Restores the header of a potentially inconsistent file. |
80 bool FixBlockFileHeader(MappedFile* file); | 149 bool FixBlockFileHeader(int index); |
81 | 150 |
82 // Retrieves stats for the given file index. | 151 // Retrieves stats for the given file index. |
83 void GetFileStats(int index, int* used_count, int* load); | 152 void GetFileStats(int index, int* used_count, int* load); |
84 | 153 |
85 // Returns the filename for a given file index. | 154 // Returns the filename for the header section, given a file index. |
86 base::FilePath Name(int index); | 155 base::FilePath HeaderName(int index); |
| 156 |
| 157 // Returns the filename for the data portion, given a file index. |
| 158 base::FilePath DataName(int index); |
87 | 159 |
88 bool init_; | 160 bool init_; |
| 161 bool small_steps_; |
| 162 int data_offset_; // Zero for V3. |
89 char* zero_buffer_; // Buffer to speed-up cleaning deleted entries. | 163 char* zero_buffer_; // Buffer to speed-up cleaning deleted entries. |
90 base::FilePath path_; // Path to the backing folder. | 164 base::FilePath path_; // Path to the backing folder. |
91 std::vector<MappedFile*> block_files_; // The actual files. | 165 std::vector<MappedFile*> block_headers_; // The block file headers. |
| 166 std::vector<MappedFile*> block_data_; // The user data (if not stored with |
| 167 // the header). |
92 scoped_ptr<base::ThreadChecker> thread_checker_; | 168 scoped_ptr<base::ThreadChecker> thread_checker_; |
93 | 169 |
94 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_ZeroSizeFile); | 170 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_ZeroSizeFile); |
95 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_TruncatedFile); | 171 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_TruncatedFile); |
96 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_InvalidFile); | 172 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_InvalidFile); |
97 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_Stats); | 173 FRIEND_TEST_ALL_PREFIXES(DiskCacheTest, BlockFiles_Stats); |
98 | 174 |
99 DISALLOW_COPY_AND_ASSIGN(BlockFiles); | 175 DISALLOW_COPY_AND_ASSIGN(BlockFiles); |
100 }; | 176 }; |
101 | 177 |
102 } // namespace disk_cache | 178 } // namespace disk_cache |
103 | 179 |
104 #endif // NET_DISK_CACHE_BLOCK_FILES_H_ | 180 #endif // NET_DISK_CACHE_BLOCK_FILES_H_ |
OLD | NEW |