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 // This is an internal class that handles the address of a cache record. 6 // See net/disk_cache/disk_cache.h for the public interface of the cache. 7 8 #ifndef NET_DISK_CACHE_ADDR_H_ 9 #define NET_DISK_CACHE_ADDR_H_ 10 11 #include "net/base/net_export.h" 12 #include "net/disk_cache/disk_format_base.h" 13 14 namespace disk_cache { 15 16 enum FileType { 17 EXTERNAL = 0, 18 RANKINGS = 1, 19 BLOCK_256 = 2, 20 BLOCK_1K = 3, 21 BLOCK_4K = 4, 22 BLOCK_FILES = 5, 23 BLOCK_ENTRIES = 6, 24 BLOCK_EVICTED = 7 25 }; 26 27 const int kMaxBlockSize = 4096 * 4; 28 const int kMaxBlockFile = 255; 29 const int kMaxNumBlocks = 4; 30 const int kFirstAdditionalBlockFile = 4; 31 const int kFirstAdditionalBlockFileV3 = 7; 32 33 // Defines a storage address for a cache record 34 // 35 // Header: 36 // 1000 0000 0000 0000 0000 0000 0000 0000 : initialized bit 37 // 0111 0000 0000 0000 0000 0000 0000 0000 : file type 38 // 39 // File type values: 40 // 0 = separate file on disk 41 // 1 = rankings block file 42 // 2 = 256 byte block file 43 // 3 = 1k byte block file 44 // 4 = 4k byte block file 45 // 5 = external files block file 46 // 6 = active entries block file 47 // 7 = evicted entries block file 48 // 49 // If separate file: 50 // 0000 1111 1111 1111 1111 1111 1111 1111 : file# 0 - 268,435,456 (2^28) 51 // 52 // If block file: 53 // 0000 1100 0000 0000 0000 0000 0000 0000 : reserved bits 54 // 0000 0011 0000 0000 0000 0000 0000 0000 : number of contiguous blocks 1-4 55 // 0000 0000 1111 1111 0000 0000 0000 0000 : file selector 0 - 255 56 // 0000 0000 0000 0000 1111 1111 1111 1111 : block# 0 - 65,535 (2^16) 57 class NET_EXPORT_PRIVATE Addr { 58 public: 59 Addr() : value_(0) {} 60 explicit Addr(CacheAddr address) : value_(address) {} 61 Addr(FileType file_type, int max_blocks, int block_file, int index) { 62 value_ = ((file_type << kFileTypeOffset) & kFileTypeMask) | 63 (((max_blocks - 1) << kNumBlocksOffset) & kNumBlocksMask) | 64 ((block_file << kFileSelectorOffset) & kFileSelectorMask) | 65 (index & kStartBlockMask) | kInitializedMask; 66 } 67 68 CacheAddr value() const { return value_; } 69 void set_value(CacheAddr address) { 70 value_ = address; 71 } 72 73 bool is_initialized() const { 74 return (value_ & kInitializedMask) != 0; 75 } 76 77 bool is_separate_file() const { 78 return (value_ & kFileTypeMask) == 0; 79 } 80 81 bool is_block_file() const { 82 return !is_separate_file(); 83 } 84 85 FileType file_type() const { 86 return static_cast<FileType>((value_ & kFileTypeMask) >> kFileTypeOffset); 87 } 88 89 int FileNumber() const { 90 if (is_separate_file()) 91 return value_ & kFileNameMask; 92 else 93 return ((value_ & kFileSelectorMask) >> kFileSelectorOffset); 94 } 95 96 int start_block() const; 97 int num_blocks() const; 98 bool SetFileNumber(int file_number); 99 int BlockSize() const { 100 return BlockSizeForFileType(file_type()); 101 } 102 103 bool operator==(Addr other) const { 104 return value_ == other.value_; 105 } 106 107 bool operator!=(Addr other) const { 108 return value_ != other.value_; 109 } 110 111 static Addr FromEntryAddress(uint32 value) { 112 return Addr(kInitializedMask + (BLOCK_ENTRIES << kFileTypeOffset) + value); 113 } 114 115 static Addr FromEvictedAddress(uint32 value) { 116 return Addr(kInitializedMask + (BLOCK_EVICTED << kFileTypeOffset) + value); 117 } 118 119 static int BlockSizeForFileType(FileType file_type) { 120 switch (file_type) { 121 case RANKINGS: 122 return 36; 123 case BLOCK_256: 124 return 256; 125 case BLOCK_1K: 126 return 1024; 127 case BLOCK_4K: 128 return 4096; 129 case BLOCK_FILES: 130 return 8; 131 case BLOCK_ENTRIES: 132 return 104; 133 case BLOCK_EVICTED: 134 return 48; 135 default: 136 return 0; 137 } 138 } 139 140 static FileType RequiredFileType(int size) { 141 if (size < 1024) 142 return BLOCK_256; 143 else if (size < 4096) 144 return BLOCK_1K; 145 else if (size <= 4096 * 4) 146 return BLOCK_4K; 147 else 148 return EXTERNAL; 149 } 150 151 static int RequiredBlocks(int size, FileType file_type) { 152 int block_size = BlockSizeForFileType(file_type); 153 return (size + block_size - 1) / block_size; 154 } 155 156 // Returns true if this address looks like a valid one. 157 bool SanityCheckV2() const; 158 bool SanityCheckV3() const; 159 bool SanityCheckForEntryV2() const; 160 bool SanityCheckForEntryV3() const; 161 bool SanityCheckForRankings() const; 162 163 private: 164 uint32 reserved_bits() const { 165 return value_ & kReservedBitsMask; 166 } 167 168 static const uint32 kInitializedMask = 0x80000000; 169 static const uint32 kFileTypeMask = 0x70000000; 170 static const uint32 kFileTypeOffset = 28; 171 static const uint32 kReservedBitsMask = 0x0c000000; 172 static const uint32 kNumBlocksMask = 0x03000000; 173 static const uint32 kNumBlocksOffset = 24; 174 static const uint32 kFileSelectorMask = 0x00ff0000; 175 static const uint32 kFileSelectorOffset = 16; 176 static const uint32 kStartBlockMask = 0x0000FFFF; 177 static const uint32 kFileNameMask = 0x0FFFFFFF; 178 179 CacheAddr value_; 180 }; 181 182 } // namespace disk_cache 183 184 #endif // NET_DISK_CACHE_ADDR_H_ 185