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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 uint32 map_block = header_->allocation_map[i]; | 170 uint32 map_block = header_->allocation_map[i]; |
171 | 171 |
172 for (int j = 0; j < 8; j++, map_block >>= 4) { | 172 for (int j = 0; j < 8; j++, map_block >>= 4) { |
173 int type = GetMapBlockType(map_block); | 173 int type = GetMapBlockType(map_block); |
174 if (type) | 174 if (type) |
175 header_->empty[type -1]++; | 175 header_->empty[type -1]++; |
176 } | 176 } |
177 } | 177 } |
178 } | 178 } |
179 | 179 |
180 bool BlockHeader::NeedToGrowBlockFile(int block_count) { | 180 bool BlockHeader::NeedToGrowBlockFile(int block_count) const { |
181 bool have_space = false; | 181 bool have_space = false; |
182 int empty_blocks = 0; | 182 int empty_blocks = 0; |
183 for (int i = 0; i < kMaxNumBlocks; i++) { | 183 for (int i = 0; i < kMaxNumBlocks; i++) { |
184 empty_blocks += header_->empty[i] * (i + 1); | 184 empty_blocks += header_->empty[i] * (i + 1); |
185 if (i >= block_count - 1 && header_->empty[i]) | 185 if (i >= block_count - 1 && header_->empty[i]) |
186 have_space = true; | 186 have_space = true; |
187 } | 187 } |
188 | 188 |
189 if (header_->next_file && (empty_blocks < kMaxBlocks / 10)) { | 189 if (header_->next_file && (empty_blocks < kMaxBlocks / 10)) { |
190 // 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 |
191 // 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 |
192 // using this file again. | 192 // using this file again. |
193 return true; | 193 return true; |
194 } | 194 } |
195 return !have_space; | 195 return !have_space; |
196 } | 196 } |
197 | 197 |
198 bool BlockHeader::CanAllocate(int block_count) const { | |
199 bool have_space = false; | |
200 int empty_blocks = 0; | |
201 for (int i = 0; i < kMaxNumBlocks; i++) { | |
202 empty_blocks += header_->empty[i] * (i + 1); | |
203 if (i >= (block_count - 1) && header_->empty[i]) | |
204 have_space = true; | |
205 } | |
206 | |
207 return have_space; | |
208 } | |
209 | |
198 int BlockHeader::EmptyBlocks() const { | 210 int BlockHeader::EmptyBlocks() const { |
199 int empty_blocks = 0; | 211 int empty_blocks = 0; |
200 for (int i = 0; i < disk_cache::kMaxNumBlocks; i++) { | 212 for (int i = 0; i < disk_cache::kMaxNumBlocks; i++) { |
201 empty_blocks += header_->empty[i] * (i + 1); | 213 empty_blocks += header_->empty[i] * (i + 1); |
202 if (header_->empty[i] < 0) | 214 if (header_->empty[i] < 0) |
203 return 0; | 215 return 0; |
204 } | 216 } |
205 return empty_blocks; | 217 return empty_blocks; |
206 } | 218 } |
207 | 219 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
253 if (!RemoveEmptyFile(static_cast<FileType>(i + 1))) | 265 if (!RemoveEmptyFile(static_cast<FileType>(i + 1))) |
254 return false; | 266 return false; |
255 } | 267 } |
256 | 268 |
257 init_ = true; | 269 init_ = true; |
258 return true; | 270 return true; |
259 } | 271 } |
260 | 272 |
261 MappedFile* BlockFiles::GetFile(Addr address) { | 273 MappedFile* BlockFiles::GetFile(Addr address) { |
262 DCHECK(thread_checker_->CalledOnValidThread()); | 274 DCHECK(thread_checker_->CalledOnValidThread()); |
263 DCHECK(block_files_.size() >= 4); | 275 DCHECK(block_files_.size() >= kFirstAdditionalBlockFile); |
gavinp
2013/06/28 21:47:45
Nit: Consider using the DCHECK_LE macro here, and
rvargas (doing something else)
2013/07/11 19:54:55
Already changed
| |
264 DCHECK(address.is_block_file() || !address.is_initialized()); | 276 DCHECK(address.is_block_file() || !address.is_initialized()); |
265 if (!address.is_initialized()) | 277 if (!address.is_initialized()) |
266 return NULL; | 278 return NULL; |
267 | 279 |
268 int file_index = address.FileNumber(); | 280 int file_index = address.FileNumber(); |
269 if (static_cast<unsigned int>(file_index) >= block_files_.size() || | 281 if (static_cast<unsigned int>(file_index) >= block_files_.size() || |
270 !block_files_[file_index]) { | 282 !block_files_[file_index]) { |
271 // We need to open the file | 283 // We need to open the file |
272 if (!OpenBlockFile(file_index)) | 284 if (!OpenBlockFile(file_index)) |
273 return NULL; | 285 return NULL; |
274 } | 286 } |
275 DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index)); | 287 DCHECK(block_files_.size() >= static_cast<unsigned int>(file_index)); |
276 return block_files_[file_index]; | 288 return block_files_[file_index]; |
277 } | 289 } |
278 | 290 |
279 bool BlockFiles::CreateBlock(FileType block_type, int block_count, | 291 bool BlockFiles::CreateBlock(FileType block_type, int block_count, |
280 Addr* block_address) { | 292 Addr* block_address) { |
281 DCHECK(thread_checker_->CalledOnValidThread()); | 293 DCHECK(thread_checker_->CalledOnValidThread()); |
282 if (block_type < RANKINGS || block_type > BLOCK_4K || | 294 if (block_type < RANKINGS || block_type > BLOCK_4K || |
283 block_count < 1 || block_count > 4) | 295 block_count < 1 || block_count > kMaxNumBlocks) { |
284 return false; | 296 return false; |
297 } | |
285 if (!init_) | 298 if (!init_) |
286 return false; | 299 return false; |
287 | 300 |
288 MappedFile* file = FileForNewBlock(block_type, block_count); | 301 MappedFile* file = FileForNewBlock(block_type, block_count); |
289 if (!file) | 302 if (!file) |
290 return false; | 303 return false; |
291 | 304 |
292 ScopedFlush flush(file); | 305 ScopedFlush flush(file); |
293 BlockHeader header(file); | 306 BlockHeader header(file); |
294 | 307 |
295 int target_size = 0; | 308 int target_size = 0; |
296 for (int i = block_count; i <= 4; i++) { | 309 for (int i = block_count; i <= kMaxNumBlocks; i++) { |
297 if (header->empty[i - 1]) { | 310 if (header->empty[i - 1]) { |
298 target_size = i; | 311 target_size = i; |
299 break; | 312 break; |
300 } | 313 } |
301 } | 314 } |
302 | 315 |
303 DCHECK(target_size); | 316 DCHECK(target_size); |
304 int index; | 317 int index; |
305 if (!header.CreateMapBlock(target_size, block_count, &index)) | 318 if (!header.CreateMapBlock(target_size, block_count, &index)) |
306 return false; | 319 return false; |
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
664 *load = 0; | 677 *load = 0; |
665 for (;;) { | 678 for (;;) { |
666 if (!block_files_[index] && !OpenBlockFile(index)) | 679 if (!block_files_[index] && !OpenBlockFile(index)) |
667 return; | 680 return; |
668 | 681 |
669 BlockFileHeader* header = | 682 BlockFileHeader* header = |
670 reinterpret_cast<BlockFileHeader*>(block_files_[index]->buffer()); | 683 reinterpret_cast<BlockFileHeader*>(block_files_[index]->buffer()); |
671 | 684 |
672 max_blocks += header->max_entries; | 685 max_blocks += header->max_entries; |
673 int used = header->max_entries; | 686 int used = header->max_entries; |
674 for (int i = 0; i < 4; i++) { | 687 for (int i = 0; i < kMaxNumBlocks; i++) { |
675 used -= header->empty[i] * (i + 1); | 688 used -= header->empty[i] * (i + 1); |
676 DCHECK_GE(used, 0); | 689 DCHECK_GE(used, 0); |
677 } | 690 } |
678 *used_count += used; | 691 *used_count += used; |
679 | 692 |
680 if (!header->next_file) | 693 if (!header->next_file) |
681 break; | 694 break; |
682 index = header->next_file; | 695 index = header->next_file; |
683 } | 696 } |
684 if (max_blocks) | 697 if (max_blocks) |
685 *load = *used_count * 100 / max_blocks; | 698 *load = *used_count * 100 / max_blocks; |
686 } | 699 } |
687 | 700 |
688 base::FilePath BlockFiles::Name(int index) { | 701 base::FilePath BlockFiles::Name(int index) { |
689 // The file format allows for 256 files. | 702 // The file format allows for 256 files. |
690 DCHECK(index < 256 || index >= 0); | 703 DCHECK(index < 256 || index >= 0); |
691 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); | 704 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); |
692 return path_.AppendASCII(tmp); | 705 return path_.AppendASCII(tmp); |
693 } | 706 } |
694 | 707 |
695 } // namespace disk_cache | 708 } // namespace disk_cache |
OLD | NEW |