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