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 #include "net/disk_cache/block_files.h" | 5 #include "net/disk_cache/block_files.h" |
6 | 6 |
7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 // type of entry that can be stored there (number of consecutive blocks). | 25 // type of entry that can be stored there (number of consecutive blocks). |
26 const char s_types[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}; | 26 const char s_types[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}; |
27 | 27 |
28 // Returns the type of block (number of consecutive blocks that can be stored) | 28 // Returns the type of block (number of consecutive blocks that can be stored) |
29 // for a given nibble of the bitmap. | 29 // for a given nibble of the bitmap. |
30 inline int GetMapBlockType(uint8 value) { | 30 inline int GetMapBlockType(uint8 value) { |
31 value &= 0xf; | 31 value &= 0xf; |
32 return s_types[value]; | 32 return s_types[value]; |
33 } | 33 } |
34 | 34 |
35 void FixAllocationCounters(disk_cache::BlockFileHeader* header); | 35 } // namespace |
36 | 36 |
37 // Creates a new entry on the allocation map, updating the apropriate counters. | 37 namespace disk_cache { |
38 // target is the type of block to use (number of empty blocks), and size is the | 38 |
39 // actual number of blocks to use. | 39 BlockHeader::BlockHeader() : header_(NULL) { |
40 bool CreateMapBlock(int target, int size, disk_cache::BlockFileHeader* header, | 40 } |
41 int* index) { | 41 |
42 if (target <= 0 || target > disk_cache::kMaxNumBlocks || | 42 BlockHeader::BlockHeader(BlockFileHeader* header) : header_(header) { |
43 size <= 0 || size > disk_cache::kMaxNumBlocks) { | 43 } |
44 | |
45 BlockHeader::BlockHeader(MappedFile* file) | |
46 : header_(reinterpret_cast<BlockFileHeader*>(file->buffer())) { | |
gavinp
2013/06/18 14:17:45
Shouldn't this be a static_cast?
rvargas (doing something else)
2013/06/18 18:12:23
This is a cast between "unrelated" pointer types,
| |
47 } | |
48 | |
49 BlockHeader::BlockHeader(const BlockHeader& other) : header_(other.header_) { | |
50 } | |
51 | |
52 BlockHeader::~BlockHeader() { | |
53 } | |
54 | |
55 bool BlockHeader::CreateMapBlock(int target, int size, int* index) { | |
56 if (target <= 0 || target > kMaxNumBlocks || | |
57 size <= 0 || size > kMaxNumBlocks) { | |
44 NOTREACHED(); | 58 NOTREACHED(); |
45 return false; | 59 return false; |
46 } | 60 } |
47 | 61 |
48 TimeTicks start = TimeTicks::Now(); | 62 TimeTicks start = TimeTicks::Now(); |
49 // We are going to process the map on 32-block chunks (32 bits), and on every | 63 // We are going to process the map on 32-block chunks (32 bits), and on every |
50 // chunk, iterate through the 8 nibbles where the new block can be located. | 64 // chunk, iterate through the 8 nibbles where the new block can be located. |
51 int current = header->hints[target - 1]; | 65 int current = header_->hints[target - 1]; |
52 for (int i = 0; i < header->max_entries / 32; i++, current++) { | 66 for (int i = 0; i < header_->max_entries / 32; i++, current++) { |
53 if (current == header->max_entries / 32) | 67 if (current == header_->max_entries / 32) |
54 current = 0; | 68 current = 0; |
55 uint32 map_block = header->allocation_map[current]; | 69 uint32 map_block = header_->allocation_map[current]; |
56 | 70 |
57 for (int j = 0; j < 8; j++, map_block >>= 4) { | 71 for (int j = 0; j < 8; j++, map_block >>= 4) { |
58 if (GetMapBlockType(map_block) != target) | 72 if (GetMapBlockType(map_block) != target) |
59 continue; | 73 continue; |
60 | 74 |
61 disk_cache::FileLock lock(header); | 75 disk_cache::FileLock lock(header_); |
62 int index_offset = j * 4 + 4 - target; | 76 int index_offset = j * 4 + 4 - target; |
63 *index = current * 32 + index_offset; | 77 *index = current * 32 + index_offset; |
64 DCHECK_EQ(*index / 4, (*index + size - 1) / 4); | 78 DCHECK_EQ(*index / 4, (*index + size - 1) / 4); |
65 uint32 to_add = ((1 << size) - 1) << index_offset; | 79 uint32 to_add = ((1 << size) - 1) << index_offset; |
66 header->num_entries++; | 80 header_->num_entries++; |
67 | 81 |
68 // Note that there is no race in the normal sense here, but if we enforce | 82 // Note that there is no race in the normal sense here, but if we enforce |
69 // the order of memory accesses between num_entries and allocation_map, we | 83 // the order of memory accesses between num_entries and allocation_map, we |
70 // can assert that even if we crash here, num_entries will never be less | 84 // can assert that even if we crash here, num_entries will never be less |
71 // than the actual number of used blocks. | 85 // than the actual number of used blocks. |
72 base::subtle::MemoryBarrier(); | 86 base::subtle::MemoryBarrier(); |
73 header->allocation_map[current] |= to_add; | 87 header_->allocation_map[current] |= to_add; |
74 | 88 |
75 header->hints[target - 1] = current; | 89 header_->hints[target - 1] = current; |
76 header->empty[target - 1]--; | 90 header_->empty[target - 1]--; |
77 DCHECK_GE(header->empty[target - 1], 0); | 91 DCHECK_GE(header_->empty[target - 1], 0); |
78 if (target != size) { | 92 if (target != size) { |
79 header->empty[target - size - 1]++; | 93 header_->empty[target - size - 1]++; |
80 } | 94 } |
81 HISTOGRAM_TIMES("DiskCache.CreateBlock", TimeTicks::Now() - start); | 95 HISTOGRAM_TIMES("DiskCache.CreateBlock", TimeTicks::Now() - start); |
82 return true; | 96 return true; |
83 } | 97 } |
84 } | 98 } |
85 | 99 |
86 // It is possible to have an undetected corruption (for example when the OS | 100 // It is possible to have an undetected corruption (for example when the OS |
87 // crashes), fix it here. | 101 // crashes), fix it here. |
88 LOG(ERROR) << "Failing CreateMapBlock"; | 102 LOG(ERROR) << "Failing CreateMapBlock"; |
89 FixAllocationCounters(header); | 103 FixAllocationCounters(); |
90 return false; | 104 return false; |
91 } | 105 } |
92 | 106 |
93 // Deletes the block pointed by index from allocation_map, and updates the | 107 void BlockHeader::DeleteMapBlock(int index, int size) { |
94 // relevant counters on the header. | 108 if (size < 0 || size > kMaxNumBlocks) { |
95 void DeleteMapBlock(int index, int size, disk_cache::BlockFileHeader* header) { | |
96 if (size < 0 || size > disk_cache::kMaxNumBlocks) { | |
97 NOTREACHED(); | 109 NOTREACHED(); |
98 return; | 110 return; |
99 } | 111 } |
100 TimeTicks start = TimeTicks::Now(); | 112 TimeTicks start = TimeTicks::Now(); |
101 int byte_index = index / 8; | 113 int byte_index = index / 8; |
102 uint8* byte_map = reinterpret_cast<uint8*>(header->allocation_map); | 114 uint8* byte_map = reinterpret_cast<uint8*>(header_->allocation_map); |
103 uint8 map_block = byte_map[byte_index]; | 115 uint8 map_block = byte_map[byte_index]; |
104 | 116 |
105 if (index % 8 >= 4) | 117 if (index % 8 >= 4) |
106 map_block >>= 4; | 118 map_block >>= 4; |
107 | 119 |
108 // See what type of block will be availabe after we delete this one. | 120 // See what type of block will be available after we delete this one. |
109 int bits_at_end = 4 - size - index % 4; | 121 int bits_at_end = 4 - size - index % 4; |
110 uint8 end_mask = (0xf << (4 - bits_at_end)) & 0xf; | 122 uint8 end_mask = (0xf << (4 - bits_at_end)) & 0xf; |
111 bool update_counters = (map_block & end_mask) == 0; | 123 bool update_counters = (map_block & end_mask) == 0; |
112 uint8 new_value = map_block & ~(((1 << size) - 1) << (index % 4)); | 124 uint8 new_value = map_block & ~(((1 << size) - 1) << (index % 4)); |
113 int new_type = GetMapBlockType(new_value); | 125 int new_type = GetMapBlockType(new_value); |
114 | 126 |
115 disk_cache::FileLock lock(header); | 127 disk_cache::FileLock lock(header_); |
116 DCHECK((((1 << size) - 1) << (index % 8)) < 0x100); | 128 DCHECK((((1 << size) - 1) << (index % 8)) < 0x100); |
117 uint8 to_clear = ((1 << size) - 1) << (index % 8); | 129 uint8 to_clear = ((1 << size) - 1) << (index % 8); |
118 DCHECK((byte_map[byte_index] & to_clear) == to_clear); | 130 DCHECK((byte_map[byte_index] & to_clear) == to_clear); |
119 byte_map[byte_index] &= ~to_clear; | 131 byte_map[byte_index] &= ~to_clear; |
120 | 132 |
121 if (update_counters) { | 133 if (update_counters) { |
122 if (bits_at_end) | 134 if (bits_at_end) |
123 header->empty[bits_at_end - 1]--; | 135 header_->empty[bits_at_end - 1]--; |
124 header->empty[new_type - 1]++; | 136 header_->empty[new_type - 1]++; |
125 DCHECK_GE(header->empty[bits_at_end - 1], 0); | 137 DCHECK_GE(header_->empty[bits_at_end - 1], 0); |
126 } | 138 } |
127 base::subtle::MemoryBarrier(); | 139 base::subtle::MemoryBarrier(); |
128 header->num_entries--; | 140 header_->num_entries--; |
129 DCHECK_GE(header->num_entries, 0); | 141 DCHECK_GE(header_->num_entries, 0); |
130 HISTOGRAM_TIMES("DiskCache.DeleteBlock", TimeTicks::Now() - start); | 142 HISTOGRAM_TIMES("DiskCache.DeleteBlock", TimeTicks::Now() - start); |
131 } | 143 } |
132 | 144 |
133 #ifndef NDEBUG | 145 // Note that this is a simplified version of DeleteMapBlock(). |
134 // Returns true if the specified block is used. Note that this is a simplified | 146 bool BlockHeader::UsedMapBlock(int index, int size) { |
135 // version of DeleteMapBlock(). | 147 if (size < 0 || size > kMaxNumBlocks) { |
136 bool UsedMapBlock(int index, int size, disk_cache::BlockFileHeader* header) { | |
137 if (size < 0 || size > disk_cache::kMaxNumBlocks) { | |
138 NOTREACHED(); | 148 NOTREACHED(); |
139 return false; | 149 return false; |
140 } | 150 } |
141 int byte_index = index / 8; | 151 int byte_index = index / 8; |
142 uint8* byte_map = reinterpret_cast<uint8*>(header->allocation_map); | 152 uint8* byte_map = reinterpret_cast<uint8*>(header_->allocation_map); |
143 uint8 map_block = byte_map[byte_index]; | 153 uint8 map_block = byte_map[byte_index]; |
144 | 154 |
145 if (index % 8 >= 4) | 155 if (index % 8 >= 4) |
146 map_block >>= 4; | 156 map_block >>= 4; |
147 | 157 |
148 DCHECK((((1 << size) - 1) << (index % 8)) < 0x100); | 158 DCHECK((((1 << size) - 1) << (index % 8)) < 0x100); |
149 uint8 to_clear = ((1 << size) - 1) << (index % 8); | 159 uint8 to_clear = ((1 << size) - 1) << (index % 8); |
150 return ((byte_map[byte_index] & to_clear) == to_clear); | 160 return ((byte_map[byte_index] & to_clear) == to_clear); |
151 } | 161 } |
152 #endif // NDEBUG | |
153 | 162 |
154 // Restores the "empty counters" and allocation hints. | 163 void BlockHeader::FixAllocationCounters() { |
155 void FixAllocationCounters(disk_cache::BlockFileHeader* header) { | 164 for (int i = 0; i < kMaxNumBlocks; i++) { |
156 for (int i = 0; i < disk_cache::kMaxNumBlocks; i++) { | 165 header_->hints[i] = 0; |
157 header->hints[i] = 0; | 166 header_->empty[i] = 0; |
158 header->empty[i] = 0; | |
159 } | 167 } |
160 | 168 |
161 for (int i = 0; i < header->max_entries / 32; i++) { | 169 for (int i = 0; i < header_->max_entries / 32; i++) { |
162 uint32 map_block = header->allocation_map[i]; | 170 uint32 map_block = header_->allocation_map[i]; |
163 | 171 |
164 for (int j = 0; j < 8; j++, map_block >>= 4) { | 172 for (int j = 0; j < 8; j++, map_block >>= 4) { |
165 int type = GetMapBlockType(map_block); | 173 int type = GetMapBlockType(map_block); |
166 if (type) | 174 if (type) |
167 header->empty[type -1]++; | 175 header_->empty[type -1]++; |
168 } | 176 } |
169 } | 177 } |
170 } | 178 } |
171 | 179 |
172 // Returns true if the current block file should not be used as-is to store more | 180 bool BlockHeader::NeedToGrowBlockFile(int block_count) { |
173 // records. |block_count| is the number of blocks to allocate. | |
174 bool NeedToGrowBlockFile(const disk_cache::BlockFileHeader* header, | |
175 int block_count) { | |
176 bool have_space = false; | 181 bool have_space = false; |
177 int empty_blocks = 0; | 182 int empty_blocks = 0; |
178 for (int i = 0; i < disk_cache::kMaxNumBlocks; i++) { | 183 for (int i = 0; i < kMaxNumBlocks; i++) { |
179 empty_blocks += header->empty[i] * (i + 1); | 184 empty_blocks += header_->empty[i] * (i + 1); |
180 if (i >= block_count - 1 && header->empty[i]) | 185 if (i >= block_count - 1 && header_->empty[i]) |
181 have_space = true; | 186 have_space = true; |
182 } | 187 } |
183 | 188 |
184 if (header->next_file && (empty_blocks < disk_cache::kMaxBlocks / 10)) { | 189 if (header_->next_file && (empty_blocks < kMaxBlocks / 10)) { |
185 // This file is almost full but we already created another one, don't use | 190 // This file is almost full but we already created another one, don't use |
186 // this file yet so that it is easier to find empty blocks when we start | 191 // this file yet so that it is easier to find empty blocks when we start |
187 // using this file again. | 192 // using this file again. |
188 return true; | 193 return true; |
189 } | 194 } |
190 return !have_space; | 195 return !have_space; |
191 } | 196 } |
192 | 197 |
193 // Returns the number of empty blocks for this file. | 198 int BlockHeader::EmptyBlocks() const { |
194 int EmptyBlocks(const disk_cache::BlockFileHeader* header) { | |
195 int empty_blocks = 0; | 199 int empty_blocks = 0; |
196 for (int i = 0; i < disk_cache::kMaxNumBlocks; i++) { | 200 for (int i = 0; i < disk_cache::kMaxNumBlocks; i++) { |
197 empty_blocks += header->empty[i] * (i + 1); | 201 empty_blocks += header_->empty[i] * (i + 1); |
198 if (header->empty[i] < 0) | 202 if (header_->empty[i] < 0) |
199 return false; | 203 return 0; |
200 } | 204 } |
201 return empty_blocks; | 205 return empty_blocks; |
202 } | 206 } |
203 | 207 |
204 // Returns true if the counters look OK. | 208 bool BlockHeader::ValidateCounters() const { |
205 bool ValidateCounters(const disk_cache::BlockFileHeader* header) { | 209 if (header_->max_entries < 0 || header_->max_entries > kMaxBlocks || |
206 if (header->max_entries < 0 || header->max_entries > disk_cache::kMaxBlocks || | 210 header_->num_entries < 0) |
207 header->num_entries < 0) | |
208 return false; | 211 return false; |
209 | 212 |
210 int empty_blocks = EmptyBlocks(header); | 213 int empty_blocks = EmptyBlocks(); |
211 if (empty_blocks + header->num_entries > header->max_entries) | 214 if (empty_blocks + header_->num_entries > header_->max_entries) |
212 return false; | 215 return false; |
213 | 216 |
214 return true; | 217 return true; |
215 } | 218 } |
216 | 219 |
217 } // namespace | 220 int BlockHeader::Size() const { |
gavinp
2013/06/18 18:14:03
This is a good candidate for an inline function, e
rvargas (doing something else)
2013/06/19 01:32:32
This method will be removed when the code to open
| |
221 return static_cast<int>(sizeof(*header_)); | |
222 } | |
218 | 223 |
219 namespace disk_cache { | 224 // ------------------------------------------------------------------------ |
220 | 225 |
221 BlockFiles::BlockFiles(const base::FilePath& path) | 226 BlockFiles::BlockFiles(const base::FilePath& path) |
222 : init_(false), zero_buffer_(NULL), path_(path) { | 227 : init_(false), zero_buffer_(NULL), path_(path) { |
223 } | 228 } |
224 | 229 |
225 BlockFiles::~BlockFiles() { | 230 BlockFiles::~BlockFiles() { |
226 if (zero_buffer_) | 231 if (zero_buffer_) |
227 delete[] zero_buffer_; | 232 delete[] zero_buffer_; |
228 CloseFiles(); | 233 CloseFiles(); |
229 } | 234 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 block_count < 1 || block_count > 4) | 283 block_count < 1 || block_count > 4) |
279 return false; | 284 return false; |
280 if (!init_) | 285 if (!init_) |
281 return false; | 286 return false; |
282 | 287 |
283 MappedFile* file = FileForNewBlock(block_type, block_count); | 288 MappedFile* file = FileForNewBlock(block_type, block_count); |
284 if (!file) | 289 if (!file) |
285 return false; | 290 return false; |
286 | 291 |
287 ScopedFlush flush(file); | 292 ScopedFlush flush(file); |
288 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 293 BlockHeader header(file); |
289 | 294 |
290 int target_size = 0; | 295 int target_size = 0; |
291 for (int i = block_count; i <= 4; i++) { | 296 for (int i = block_count; i <= 4; i++) { |
292 if (header->empty[i - 1]) { | 297 if (header->empty[i - 1]) { |
293 target_size = i; | 298 target_size = i; |
294 break; | 299 break; |
295 } | 300 } |
296 } | 301 } |
297 | 302 |
298 DCHECK(target_size); | 303 DCHECK(target_size); |
299 int index; | 304 int index; |
300 if (!CreateMapBlock(target_size, block_count, header, &index)) | 305 if (!header.CreateMapBlock(target_size, block_count, &index)) |
301 return false; | 306 return false; |
302 | 307 |
303 Addr address(block_type, block_count, header->this_file, index); | 308 Addr address(block_type, block_count, header->this_file, index); |
304 block_address->set_value(address.value()); | 309 block_address->set_value(address.value()); |
305 Trace("CreateBlock 0x%x", address.value()); | 310 Trace("CreateBlock 0x%x", address.value()); |
306 return true; | 311 return true; |
307 } | 312 } |
308 | 313 |
309 void BlockFiles::DeleteBlock(Addr address, bool deep) { | 314 void BlockFiles::DeleteBlock(Addr address, bool deep) { |
310 DCHECK(thread_checker_->CalledOnValidThread()); | 315 DCHECK(thread_checker_->CalledOnValidThread()); |
311 if (!address.is_initialized() || address.is_separate_file()) | 316 if (!address.is_initialized() || address.is_separate_file()) |
312 return; | 317 return; |
313 | 318 |
314 if (!zero_buffer_) { | 319 if (!zero_buffer_) { |
315 zero_buffer_ = new char[Addr::BlockSizeForFileType(BLOCK_4K) * 4]; | 320 zero_buffer_ = new char[Addr::BlockSizeForFileType(BLOCK_4K) * 4]; |
316 memset(zero_buffer_, 0, Addr::BlockSizeForFileType(BLOCK_4K) * 4); | 321 memset(zero_buffer_, 0, Addr::BlockSizeForFileType(BLOCK_4K) * 4); |
317 } | 322 } |
318 MappedFile* file = GetFile(address); | 323 MappedFile* file = GetFile(address); |
319 if (!file) | 324 if (!file) |
320 return; | 325 return; |
321 | 326 |
322 Trace("DeleteBlock 0x%x", address.value()); | 327 Trace("DeleteBlock 0x%x", address.value()); |
323 | 328 |
324 size_t size = address.BlockSize() * address.num_blocks(); | 329 size_t size = address.BlockSize() * address.num_blocks(); |
325 size_t offset = address.start_block() * address.BlockSize() + | 330 size_t offset = address.start_block() * address.BlockSize() + |
326 kBlockHeaderSize; | 331 kBlockHeaderSize; |
327 if (deep) | 332 if (deep) |
328 file->Write(zero_buffer_, size, offset); | 333 file->Write(zero_buffer_, size, offset); |
329 | 334 |
330 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 335 BlockHeader header(file); |
331 DeleteMapBlock(address.start_block(), address.num_blocks(), header); | 336 header.DeleteMapBlock(address.start_block(), address.num_blocks()); |
332 file->Flush(); | 337 file->Flush(); |
333 | 338 |
334 if (!header->num_entries) { | 339 if (!header->num_entries) { |
335 // This file is now empty. Let's try to delete it. | 340 // This file is now empty. Let's try to delete it. |
336 FileType type = Addr::RequiredFileType(header->entry_size); | 341 FileType type = Addr::RequiredFileType(header->entry_size); |
337 if (Addr::BlockSizeForFileType(RANKINGS) == header->entry_size) | 342 if (Addr::BlockSizeForFileType(RANKINGS) == header->entry_size) |
338 type = RANKINGS; | 343 type = RANKINGS; |
339 RemoveEmptyFile(type); // Ignore failures. | 344 RemoveEmptyFile(type); // Ignore failures. |
340 } | 345 } |
341 } | 346 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 #ifdef NDEBUG | 381 #ifdef NDEBUG |
377 return true; | 382 return true; |
378 #else | 383 #else |
379 if (!address.is_initialized() || address.is_separate_file()) | 384 if (!address.is_initialized() || address.is_separate_file()) |
380 return false; | 385 return false; |
381 | 386 |
382 MappedFile* file = GetFile(address); | 387 MappedFile* file = GetFile(address); |
383 if (!file) | 388 if (!file) |
384 return false; | 389 return false; |
385 | 390 |
386 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 391 BlockHeader header(file); |
387 bool rv = UsedMapBlock(address.start_block(), address.num_blocks(), header); | 392 bool rv = header.UsedMapBlock(address.start_block(), address.num_blocks()); |
388 DCHECK(rv); | 393 DCHECK(rv); |
389 | 394 |
390 static bool read_contents = false; | 395 static bool read_contents = false; |
391 if (read_contents) { | 396 if (read_contents) { |
392 scoped_ptr<char[]> buffer; | 397 scoped_ptr<char[]> buffer; |
393 buffer.reset(new char[Addr::BlockSizeForFileType(BLOCK_4K) * 4]); | 398 buffer.reset(new char[Addr::BlockSizeForFileType(BLOCK_4K) * 4]); |
394 size_t size = address.BlockSize() * address.num_blocks(); | 399 size_t size = address.BlockSize() * address.num_blocks(); |
395 size_t offset = address.start_block() * address.BlockSize() + | 400 size_t offset = address.start_block() * address.BlockSize() + |
396 kBlockHeaderSize; | 401 kBlockHeaderSize; |
397 bool ok = file->Read(buffer.get(), size, offset); | 402 bool ok = file->Read(buffer.get(), size, offset); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
438 LOG(ERROR) << "Failed to open " << name.value(); | 443 LOG(ERROR) << "Failed to open " << name.value(); |
439 return false; | 444 return false; |
440 } | 445 } |
441 | 446 |
442 size_t file_len = file->GetLength(); | 447 size_t file_len = file->GetLength(); |
443 if (file_len < static_cast<size_t>(kBlockHeaderSize)) { | 448 if (file_len < static_cast<size_t>(kBlockHeaderSize)) { |
444 LOG(ERROR) << "File too small " << name.value(); | 449 LOG(ERROR) << "File too small " << name.value(); |
445 return false; | 450 return false; |
446 } | 451 } |
447 | 452 |
448 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 453 BlockHeader header(file); |
449 if (kBlockMagic != header->magic || kBlockVersion2 != header->version) { | 454 if (kBlockMagic != header->magic || kBlockVersion2 != header->version) { |
450 LOG(ERROR) << "Invalid file version or magic " << name.value(); | 455 LOG(ERROR) << "Invalid file version or magic " << name.value(); |
451 return false; | 456 return false; |
452 } | 457 } |
453 | 458 |
454 if (header->updating || !ValidateCounters(header)) { | 459 if (header->updating || !header.ValidateCounters()) { |
455 // Last instance was not properly shutdown, or counters are out of sync. | 460 // Last instance was not properly shutdown, or counters are out of sync. |
456 if (!FixBlockFileHeader(file.get())) { | 461 if (!FixBlockFileHeader(file.get())) { |
457 LOG(ERROR) << "Unable to fix block file " << name.value(); | 462 LOG(ERROR) << "Unable to fix block file " << name.value(); |
458 return false; | 463 return false; |
459 } | 464 } |
460 } | 465 } |
461 | 466 |
462 if (static_cast<int>(file_len) < | 467 if (static_cast<int>(file_len) < |
463 header->max_entries * header->entry_size + kBlockHeaderSize) { | 468 header->max_entries * header->entry_size + kBlockHeaderSize) { |
464 LOG(ERROR) << "File too small " << name.value(); | 469 LOG(ERROR) << "File too small " << name.value(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
504 FileLock lock(header); | 509 FileLock lock(header); |
505 header->empty[3] = (new_size - header->max_entries) / 4; // 4 blocks entries | 510 header->empty[3] = (new_size - header->max_entries) / 4; // 4 blocks entries |
506 header->max_entries = new_size; | 511 header->max_entries = new_size; |
507 | 512 |
508 return true; | 513 return true; |
509 } | 514 } |
510 | 515 |
511 MappedFile* BlockFiles::FileForNewBlock(FileType block_type, int block_count) { | 516 MappedFile* BlockFiles::FileForNewBlock(FileType block_type, int block_count) { |
512 COMPILE_ASSERT(RANKINGS == 1, invalid_file_type); | 517 COMPILE_ASSERT(RANKINGS == 1, invalid_file_type); |
513 MappedFile* file = block_files_[block_type - 1]; | 518 MappedFile* file = block_files_[block_type - 1]; |
514 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 519 BlockHeader header(file); |
515 | 520 |
516 TimeTicks start = TimeTicks::Now(); | 521 TimeTicks start = TimeTicks::Now(); |
517 while (NeedToGrowBlockFile(header, block_count)) { | 522 while (header.NeedToGrowBlockFile(block_count)) { |
518 if (kMaxBlocks == header->max_entries) { | 523 if (kMaxBlocks == header->max_entries) { |
519 file = NextFile(file); | 524 file = NextFile(file); |
520 if (!file) | 525 if (!file) |
521 return NULL; | 526 return NULL; |
522 header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 527 header = BlockHeader(file); |
523 continue; | 528 continue; |
524 } | 529 } |
525 | 530 |
526 if (!GrowBlockFile(file, header)) | 531 if (!GrowBlockFile(file, header.Get())) |
527 return NULL; | 532 return NULL; |
528 break; | 533 break; |
529 } | 534 } |
530 HISTOGRAM_TIMES("DiskCache.GetFileForNewBlock", TimeTicks::Now() - start); | 535 HISTOGRAM_TIMES("DiskCache.GetFileForNewBlock", TimeTicks::Now() - start); |
531 return file; | 536 return file; |
532 } | 537 } |
533 | 538 |
534 MappedFile* BlockFiles::NextFile(MappedFile* file) { | 539 MappedFile* BlockFiles::NextFile(MappedFile* file) { |
535 ScopedFlush flush(file); | 540 ScopedFlush flush(file); |
536 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 541 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
604 header = next_header; | 609 header = next_header; |
605 file = next_file; | 610 file = next_file; |
606 } | 611 } |
607 return true; | 612 return true; |
608 } | 613 } |
609 | 614 |
610 // Note that we expect to be called outside of a FileLock... however, we cannot | 615 // Note that we expect to be called outside of a FileLock... however, we cannot |
611 // DCHECK on header->updating because we may be fixing a crash. | 616 // DCHECK on header->updating because we may be fixing a crash. |
612 bool BlockFiles::FixBlockFileHeader(MappedFile* file) { | 617 bool BlockFiles::FixBlockFileHeader(MappedFile* file) { |
613 ScopedFlush flush(file); | 618 ScopedFlush flush(file); |
614 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 619 BlockHeader header(file); |
615 int file_size = static_cast<int>(file->GetLength()); | 620 int file_size = static_cast<int>(file->GetLength()); |
616 if (file_size < static_cast<int>(sizeof(*header))) | 621 if (file_size < header.Size()) |
617 return false; // file_size > 2GB is also an error. | 622 return false; // file_size > 2GB is also an error. |
618 | 623 |
619 const int kMinBlockSize = 36; | 624 const int kMinBlockSize = 36; |
620 const int kMaxBlockSize = 4096; | 625 const int kMaxBlockSize = 4096; |
621 if (header->entry_size < kMinBlockSize || | 626 if (header->entry_size < kMinBlockSize || |
622 header->entry_size > kMaxBlockSize || header->num_entries < 0) | 627 header->entry_size > kMaxBlockSize || header->num_entries < 0) |
623 return false; | 628 return false; |
624 | 629 |
625 // Make sure that we survive crashes. | 630 // Make sure that we survive crashes. |
626 header->updating = 1; | 631 header->updating = 1; |
627 int expected = header->entry_size * header->max_entries + sizeof(*header); | 632 int expected = header->entry_size * header->max_entries + header.Size(); |
628 if (file_size != expected) { | 633 if (file_size != expected) { |
629 int max_expected = header->entry_size * kMaxBlocks + sizeof(*header); | 634 int max_expected = header->entry_size * kMaxBlocks + header.Size(); |
630 if (file_size < expected || header->empty[3] || file_size > max_expected) { | 635 if (file_size < expected || header->empty[3] || file_size > max_expected) { |
631 NOTREACHED(); | 636 NOTREACHED(); |
632 LOG(ERROR) << "Unexpected file size"; | 637 LOG(ERROR) << "Unexpected file size"; |
633 return false; | 638 return false; |
634 } | 639 } |
635 // We were in the middle of growing the file. | 640 // We were in the middle of growing the file. |
636 int num_entries = (file_size - sizeof(*header)) / header->entry_size; | 641 int num_entries = (file_size - header.Size()) / header->entry_size; |
637 header->max_entries = num_entries; | 642 header->max_entries = num_entries; |
638 } | 643 } |
639 | 644 |
640 FixAllocationCounters(header); | 645 header.FixAllocationCounters(); |
641 int empty_blocks = EmptyBlocks(header); | 646 int empty_blocks = header.EmptyBlocks(); |
642 if (empty_blocks + header->num_entries > header->max_entries) | 647 if (empty_blocks + header->num_entries > header->max_entries) |
643 header->num_entries = header->max_entries - empty_blocks; | 648 header->num_entries = header->max_entries - empty_blocks; |
644 | 649 |
645 if (!ValidateCounters(header)) | 650 if (!header.ValidateCounters()) |
646 return false; | 651 return false; |
647 | 652 |
648 header->updating = 0; | 653 header->updating = 0; |
649 return true; | 654 return true; |
650 } | 655 } |
651 | 656 |
652 // We are interested in the total number of blocks used by this file type, and | 657 // We are interested in the total number of blocks used by this file type, and |
653 // the max number of blocks that we can store (reported as the percentage of | 658 // the max number of blocks that we can store (reported as the percentage of |
654 // used blocks). In order to find out the number of used blocks, we have to | 659 // used blocks). In order to find out the number of used blocks, we have to |
655 // substract the empty blocks from the total blocks for each file in the chain. | 660 // substract the empty blocks from the total blocks for each file in the chain. |
(...skipping 25 matching lines...) Expand all Loading... | |
681 } | 686 } |
682 | 687 |
683 base::FilePath BlockFiles::Name(int index) { | 688 base::FilePath BlockFiles::Name(int index) { |
684 // The file format allows for 256 files. | 689 // The file format allows for 256 files. |
685 DCHECK(index < 256 || index >= 0); | 690 DCHECK(index < 256 || index >= 0); |
686 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); | 691 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); |
687 return path_.AppendASCII(tmp); | 692 return path_.AppendASCII(tmp); |
688 } | 693 } |
689 | 694 |
690 } // namespace disk_cache | 695 } // namespace disk_cache |
OLD | NEW |