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