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 // This is an internal class that handles the address of a cache record. | 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. | 6 // See net/disk_cache/disk_cache.h for the public interface of the cache. |
7 | 7 |
8 #ifndef NET_DISK_CACHE_ADDR_H_ | 8 #ifndef NET_DISK_CACHE_ADDR_H_ |
9 #define NET_DISK_CACHE_ADDR_H_ | 9 #define NET_DISK_CACHE_ADDR_H_ |
10 | 10 |
11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
12 #include "net/disk_cache/disk_format.h" | 12 #include "net/disk_cache/disk_format_base.h" |
13 | 13 |
14 namespace disk_cache { | 14 namespace disk_cache { |
15 | 15 |
16 enum FileType { | 16 enum FileType { |
17 EXTERNAL = 0, | 17 EXTERNAL = 0, |
18 RANKINGS = 1, | 18 RANKINGS = 1, |
19 BLOCK_256, | 19 BLOCK_256 = 2, |
20 BLOCK_1K, | 20 BLOCK_1K = 3, |
21 BLOCK_4K, | 21 BLOCK_4K = 4, |
| 22 BLOCK_FILES = 5, |
| 23 BLOCK_ENTRIES = 6, |
| 24 BLOCK_EVICTED = 7 |
22 }; | 25 }; |
23 | 26 |
24 const int kMaxBlockSize = 4096 * 4; | 27 const int kMaxBlockSize = 4096 * 4; |
25 const int kMaxBlockFile = 255; | 28 const int kMaxBlockFile = 255; |
26 const int kMaxNumBlocks = 4; | 29 const int kMaxNumBlocks = 4; |
27 const int kFirstAdditionalBlockFile = 4; | 30 const int kFirstAdditionalBlockFile = 4; |
| 31 const int kFirstAdditionalBlockFileV3 = 7; |
28 | 32 |
29 // Defines a storage address for a cache record | 33 // Defines a storage address for a cache record |
30 // | 34 // |
31 // Header: | 35 // Header: |
32 // 1000 0000 0000 0000 0000 0000 0000 0000 : initialized bit | 36 // 1000 0000 0000 0000 0000 0000 0000 0000 : initialized bit |
33 // 0111 0000 0000 0000 0000 0000 0000 0000 : file type | 37 // 0111 0000 0000 0000 0000 0000 0000 0000 : file type |
34 // | 38 // |
35 // File type values: | 39 // File type values: |
36 // 0 = separate file on disk | 40 // 0 = separate file on disk |
37 // 1 = rankings block file | 41 // 1 = rankings block file |
38 // 2 = 256 byte block file | 42 // 2 = 256 byte block file |
39 // 3 = 1k byte block file | 43 // 3 = 1k byte block file |
40 // 4 = 4k 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 |
41 // | 48 // |
42 // If separate file: | 49 // If separate file: |
43 // 0000 1111 1111 1111 1111 1111 1111 1111 : file# 0 - 268,435,456 (2^28) | 50 // 0000 1111 1111 1111 1111 1111 1111 1111 : file# 0 - 268,435,456 (2^28) |
44 // | 51 // |
45 // If block file: | 52 // If block file: |
46 // 0000 1100 0000 0000 0000 0000 0000 0000 : reserved bits | 53 // 0000 1100 0000 0000 0000 0000 0000 0000 : reserved bits |
47 // 0000 0011 0000 0000 0000 0000 0000 0000 : number of contiguous blocks 1-4 | 54 // 0000 0011 0000 0000 0000 0000 0000 0000 : number of contiguous blocks 1-4 |
48 // 0000 0000 1111 1111 0000 0000 0000 0000 : file selector 0 - 255 | 55 // 0000 0000 1111 1111 0000 0000 0000 0000 : file selector 0 - 255 |
49 // 0000 0000 0000 0000 1111 1111 1111 1111 : block# 0 - 65,535 (2^16) | 56 // 0000 0000 0000 0000 1111 1111 1111 1111 : block# 0 - 65,535 (2^16) |
| 57 // |
| 58 // Note that an Addr can be used to "point" to a variety of different objects, |
| 59 // from a given type of entry to random blobs of data. Conceptually, an Addr is |
| 60 // just a number that someone can inspect to find out how to locate the desired |
| 61 // record. Most users will not care about the specific bits inside Addr, for |
| 62 // example, what parts of it point to a file number; only the code that has to |
| 63 // select a specific file would care about those specific bits. |
| 64 // |
| 65 // From a general point of view, an Addr has a total capacity of 2^24 entities, |
| 66 // in that it has 24 bits that can identify individual records. Note that the |
| 67 // address space is bigger for independent files (2^28), but that would not be |
| 68 // the general case. |
50 class NET_EXPORT_PRIVATE Addr { | 69 class NET_EXPORT_PRIVATE Addr { |
51 public: | 70 public: |
52 Addr() : value_(0) {} | 71 Addr() : value_(0) {} |
53 explicit Addr(CacheAddr address) : value_(address) {} | 72 explicit Addr(CacheAddr address) : value_(address) {} |
54 Addr(FileType file_type, int max_blocks, int block_file, int index) { | 73 Addr(FileType file_type, int max_blocks, int block_file, int index) { |
55 value_ = ((file_type << kFileTypeOffset) & kFileTypeMask) | | 74 value_ = ((file_type << kFileTypeOffset) & kFileTypeMask) | |
56 (((max_blocks - 1) << kNumBlocksOffset) & kNumBlocksMask) | | 75 (((max_blocks - 1) << kNumBlocksOffset) & kNumBlocksMask) | |
57 ((block_file << kFileSelectorOffset) & kFileSelectorMask) | | 76 ((block_file << kFileSelectorOffset) & kFileSelectorMask) | |
58 (index & kStartBlockMask) | kInitializedMask; | 77 (index & kStartBlockMask) | kInitializedMask; |
59 } | 78 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 113 } |
95 | 114 |
96 bool operator==(Addr other) const { | 115 bool operator==(Addr other) const { |
97 return value_ == other.value_; | 116 return value_ == other.value_; |
98 } | 117 } |
99 | 118 |
100 bool operator!=(Addr other) const { | 119 bool operator!=(Addr other) const { |
101 return value_ != other.value_; | 120 return value_ != other.value_; |
102 } | 121 } |
103 | 122 |
| 123 // Conversions between the address of an external file and the address of the |
| 124 // control block of that external file. |
| 125 Addr AsExternal() const; |
| 126 Addr AsBlockFile() const; |
| 127 |
104 static int BlockSizeForFileType(FileType file_type) { | 128 static int BlockSizeForFileType(FileType file_type) { |
105 switch (file_type) { | 129 switch (file_type) { |
106 case RANKINGS: | 130 case RANKINGS: |
107 return 36; | 131 return 36; |
108 case BLOCK_256: | 132 case BLOCK_256: |
109 return 256; | 133 return 256; |
110 case BLOCK_1K: | 134 case BLOCK_1K: |
111 return 1024; | 135 return 1024; |
112 case BLOCK_4K: | 136 case BLOCK_4K: |
113 return 4096; | 137 return 4096; |
| 138 case BLOCK_FILES: |
| 139 return 8; |
| 140 case BLOCK_ENTRIES: |
| 141 return 104; |
| 142 case BLOCK_EVICTED: |
| 143 return 48; |
114 default: | 144 default: |
115 return 0; | 145 return 0; |
116 } | 146 } |
117 } | 147 } |
118 | 148 |
119 static FileType RequiredFileType(int size) { | 149 static FileType RequiredFileType(int size) { |
120 if (size < 1024) | 150 if (size < 1024) |
121 return BLOCK_256; | 151 return BLOCK_256; |
122 else if (size < 4096) | 152 else if (size < 4096) |
123 return BLOCK_1K; | 153 return BLOCK_1K; |
124 else if (size <= 4096 * 4) | 154 else if (size <= 4096 * 4) |
125 return BLOCK_4K; | 155 return BLOCK_4K; |
126 else | 156 else |
127 return EXTERNAL; | 157 return EXTERNAL; |
128 } | 158 } |
129 | 159 |
| 160 static int RequiredBlocks(int size, FileType file_type) { |
| 161 int block_size = BlockSizeForFileType(file_type); |
| 162 return (size + block_size - 1) / block_size; |
| 163 } |
| 164 |
130 // Returns true if this address looks like a valid one. | 165 // Returns true if this address looks like a valid one. |
131 bool SanityCheck() const; | 166 bool SanityCheckV2() const; |
132 bool SanityCheckForEntry() const; | 167 bool SanityCheckV3() const; |
| 168 bool SanityCheckForEntryV2() const; |
| 169 bool SanityCheckForEntryV3() const; |
133 bool SanityCheckForRankings() const; | 170 bool SanityCheckForRankings() const; |
134 | 171 |
135 private: | 172 private: |
| 173 uint32 reserved_bits() const { |
| 174 return value_ & kReservedBitsMask; |
| 175 } |
| 176 |
| 177 void SetFileType(FileType type); |
| 178 |
136 static const uint32 kInitializedMask = 0x80000000; | 179 static const uint32 kInitializedMask = 0x80000000; |
137 static const uint32 kFileTypeMask = 0x70000000; | 180 static const uint32 kFileTypeMask = 0x70000000; |
138 static const uint32 kFileTypeOffset = 28; | 181 static const uint32 kFileTypeOffset = 28; |
| 182 static const uint32 kReservedBitsMask = 0x0c000000; |
139 static const uint32 kNumBlocksMask = 0x03000000; | 183 static const uint32 kNumBlocksMask = 0x03000000; |
140 static const uint32 kNumBlocksOffset = 24; | 184 static const uint32 kNumBlocksOffset = 24; |
141 static const uint32 kFileSelectorMask = 0x00ff0000; | 185 static const uint32 kFileSelectorMask = 0x00ff0000; |
142 static const uint32 kFileSelectorOffset = 16; | 186 static const uint32 kFileSelectorOffset = 16; |
143 static const uint32 kStartBlockMask = 0x0000FFFF; | 187 static const uint32 kStartBlockMask = 0x0000FFFF; |
144 static const uint32 kFileNameMask = 0x0FFFFFFF; | 188 static const uint32 kFileNameMask = 0x0FFFFFFF; |
145 | 189 |
146 CacheAddr value_; | 190 CacheAddr value_; |
147 }; | 191 }; |
148 | 192 |
149 } // namespace disk_cache | 193 } // namespace disk_cache |
150 | 194 |
151 #endif // NET_DISK_CACHE_ADDR_H_ | 195 #endif // NET_DISK_CACHE_ADDR_H_ |
OLD | NEW |