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) |
50 class NET_EXPORT_PRIVATE Addr { | 57 class NET_EXPORT_PRIVATE Addr { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 101 } |
95 | 102 |
96 bool operator==(Addr other) const { | 103 bool operator==(Addr other) const { |
97 return value_ == other.value_; | 104 return value_ == other.value_; |
98 } | 105 } |
99 | 106 |
100 bool operator!=(Addr other) const { | 107 bool operator!=(Addr other) const { |
101 return value_ != other.value_; | 108 return value_ != other.value_; |
102 } | 109 } |
103 | 110 |
| 111 // Conversions between the address of an external file and the address of the |
| 112 // control block of that external file. |
| 113 Addr AsExternal() const; |
| 114 Addr AsBlockFile() const; |
| 115 |
| 116 static Addr FromEntryAddress(uint32 value) { |
| 117 return Addr(kInitializedMask + (BLOCK_ENTRIES << kFileTypeOffset) + value); |
| 118 } |
| 119 |
| 120 static Addr FromEvictedAddress(uint32 value) { |
| 121 return Addr(kInitializedMask + (BLOCK_EVICTED << kFileTypeOffset) + value); |
| 122 } |
| 123 |
104 static int BlockSizeForFileType(FileType file_type) { | 124 static int BlockSizeForFileType(FileType file_type) { |
105 switch (file_type) { | 125 switch (file_type) { |
106 case RANKINGS: | 126 case RANKINGS: |
107 return 36; | 127 return 36; |
108 case BLOCK_256: | 128 case BLOCK_256: |
109 return 256; | 129 return 256; |
110 case BLOCK_1K: | 130 case BLOCK_1K: |
111 return 1024; | 131 return 1024; |
112 case BLOCK_4K: | 132 case BLOCK_4K: |
113 return 4096; | 133 return 4096; |
| 134 case BLOCK_FILES: |
| 135 return 8; |
| 136 case BLOCK_ENTRIES: |
| 137 return 104; |
| 138 case BLOCK_EVICTED: |
| 139 return 48; |
114 default: | 140 default: |
115 return 0; | 141 return 0; |
116 } | 142 } |
117 } | 143 } |
118 | 144 |
119 static FileType RequiredFileType(int size) { | 145 static FileType RequiredFileType(int size) { |
120 if (size < 1024) | 146 if (size < 1024) |
121 return BLOCK_256; | 147 return BLOCK_256; |
122 else if (size < 4096) | 148 else if (size < 4096) |
123 return BLOCK_1K; | 149 return BLOCK_1K; |
124 else if (size <= 4096 * 4) | 150 else if (size <= 4096 * 4) |
125 return BLOCK_4K; | 151 return BLOCK_4K; |
126 else | 152 else |
127 return EXTERNAL; | 153 return EXTERNAL; |
128 } | 154 } |
129 | 155 |
| 156 static int RequiredBlocks(int size, FileType file_type) { |
| 157 int block_size = BlockSizeForFileType(file_type); |
| 158 return (size + block_size - 1) / block_size; |
| 159 } |
| 160 |
130 // Returns true if this address looks like a valid one. | 161 // Returns true if this address looks like a valid one. |
131 bool SanityCheck() const; | 162 bool SanityCheckV2() const; |
132 bool SanityCheckForEntry() const; | 163 bool SanityCheckV3() const; |
| 164 bool SanityCheckForEntryV2() const; |
| 165 bool SanityCheckForEntryV3() const; |
133 bool SanityCheckForRankings() const; | 166 bool SanityCheckForRankings() const; |
134 | 167 |
135 private: | 168 private: |
| 169 uint32 reserved_bits() const { |
| 170 return value_ & kReservedBitsMask; |
| 171 } |
| 172 |
| 173 void SetFileType(FileType type); |
| 174 |
136 static const uint32 kInitializedMask = 0x80000000; | 175 static const uint32 kInitializedMask = 0x80000000; |
137 static const uint32 kFileTypeMask = 0x70000000; | 176 static const uint32 kFileTypeMask = 0x70000000; |
138 static const uint32 kFileTypeOffset = 28; | 177 static const uint32 kFileTypeOffset = 28; |
| 178 static const uint32 kReservedBitsMask = 0x0c000000; |
139 static const uint32 kNumBlocksMask = 0x03000000; | 179 static const uint32 kNumBlocksMask = 0x03000000; |
140 static const uint32 kNumBlocksOffset = 24; | 180 static const uint32 kNumBlocksOffset = 24; |
141 static const uint32 kFileSelectorMask = 0x00ff0000; | 181 static const uint32 kFileSelectorMask = 0x00ff0000; |
142 static const uint32 kFileSelectorOffset = 16; | 182 static const uint32 kFileSelectorOffset = 16; |
143 static const uint32 kStartBlockMask = 0x0000FFFF; | 183 static const uint32 kStartBlockMask = 0x0000FFFF; |
144 static const uint32 kFileNameMask = 0x0FFFFFFF; | 184 static const uint32 kFileNameMask = 0x0FFFFFFF; |
145 | 185 |
146 CacheAddr value_; | 186 CacheAddr value_; |
147 }; | 187 }; |
148 | 188 |
149 } // namespace disk_cache | 189 } // namespace disk_cache |
150 | 190 |
151 #endif // NET_DISK_CACHE_ADDR_H_ | 191 #endif // NET_DISK_CACHE_ADDR_H_ |
OLD | NEW |