| 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/blockfile/block_files.h" | 5 #include "net/disk_cache/blockfile/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 10 matching lines...) Expand all Loading... |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const char* kBlockName = "data_"; | 23 const char* kBlockName = "data_"; |
| 24 | 24 |
| 25 // This array is used to perform a fast lookup of the nibble bit pattern to the | 25 // This array is used to perform a fast lookup of the nibble bit pattern to the |
| 26 // type of entry that can be stored there (number of consecutive blocks). | 26 // type of entry that can be stored there (number of consecutive blocks). |
| 27 const char s_types[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}; | 27 const char s_types[16] = {4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 28 | 28 |
| 29 // Returns the type of block (number of consecutive blocks that can be stored) | 29 // Returns the type of block (number of consecutive blocks that can be stored) |
| 30 // for a given nibble of the bitmap. | 30 // for a given nibble of the bitmap. |
| 31 inline int GetMapBlockType(uint8 value) { | 31 inline int GetMapBlockType(uint32 value) { |
| 32 value &= 0xf; | 32 value &= 0xf; |
| 33 return s_types[value]; | 33 return s_types[value]; |
| 34 } | 34 } |
| 35 | 35 |
| 36 } // namespace | 36 } // namespace |
| 37 | 37 |
| 38 namespace disk_cache { | 38 namespace disk_cache { |
| 39 | 39 |
| 40 BlockHeader::BlockHeader() : header_(NULL) { | 40 BlockHeader::BlockHeader() : header_(NULL) { |
| 41 } | 41 } |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 } | 272 } |
| 273 | 273 |
| 274 bool BlockFiles::Init(bool create_files) { | 274 bool BlockFiles::Init(bool create_files) { |
| 275 DCHECK(!init_); | 275 DCHECK(!init_); |
| 276 if (init_) | 276 if (init_) |
| 277 return false; | 277 return false; |
| 278 | 278 |
| 279 thread_checker_.reset(new base::ThreadChecker); | 279 thread_checker_.reset(new base::ThreadChecker); |
| 280 | 280 |
| 281 block_files_.resize(kFirstAdditionalBlockFile); | 281 block_files_.resize(kFirstAdditionalBlockFile); |
| 282 for (int i = 0; i < kFirstAdditionalBlockFile; i++) { | 282 for (int16 i = 0; i < kFirstAdditionalBlockFile; i++) { |
| 283 if (create_files) | 283 if (create_files) |
| 284 if (!CreateBlockFile(i, static_cast<FileType>(i + 1), true)) | 284 if (!CreateBlockFile(i, static_cast<FileType>(i + 1), true)) |
| 285 return false; | 285 return false; |
| 286 | 286 |
| 287 if (!OpenBlockFile(i)) | 287 if (!OpenBlockFile(i)) |
| 288 return false; | 288 return false; |
| 289 | 289 |
| 290 // Walk this chain of files removing empty ones. | 290 // Walk this chain of files removing empty ones. |
| 291 if (!RemoveEmptyFile(static_cast<FileType>(i + 1))) | 291 if (!RemoveEmptyFile(static_cast<FileType>(i + 1))) |
| 292 return false; | 292 return false; |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 break; | 567 break; |
| 568 } | 568 } |
| 569 LOCAL_HISTOGRAM_TIMES("DiskCache.GetFileForNewBlock", | 569 LOCAL_HISTOGRAM_TIMES("DiskCache.GetFileForNewBlock", |
| 570 TimeTicks::Now() - start); | 570 TimeTicks::Now() - start); |
| 571 return file; | 571 return file; |
| 572 } | 572 } |
| 573 | 573 |
| 574 MappedFile* BlockFiles::NextFile(MappedFile* file) { | 574 MappedFile* BlockFiles::NextFile(MappedFile* file) { |
| 575 ScopedFlush flush(file); | 575 ScopedFlush flush(file); |
| 576 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); | 576 BlockFileHeader* header = reinterpret_cast<BlockFileHeader*>(file->buffer()); |
| 577 int new_file = header->next_file; | 577 int16 new_file = header->next_file; |
| 578 if (!new_file) { | 578 if (!new_file) { |
| 579 // RANKINGS is not reported as a type for small entries, but we may be | 579 // RANKINGS is not reported as a type for small entries, but we may be |
| 580 // extending the rankings block file. | 580 // extending the rankings block file. |
| 581 FileType type = Addr::RequiredFileType(header->entry_size); | 581 FileType type = Addr::RequiredFileType(header->entry_size); |
| 582 if (header->entry_size == Addr::BlockSizeForFileType(RANKINGS)) | 582 if (header->entry_size == Addr::BlockSizeForFileType(RANKINGS)) |
| 583 type = RANKINGS; | 583 type = RANKINGS; |
| 584 | 584 |
| 585 new_file = CreateNextBlockFile(type); | 585 new_file = CreateNextBlockFile(type); |
| 586 if (!new_file) | 586 if (!new_file) |
| 587 return NULL; | 587 return NULL; |
| 588 | 588 |
| 589 FileLock lock(header); | 589 FileLock lock(header); |
| 590 header->next_file = new_file; | 590 header->next_file = new_file; |
| 591 } | 591 } |
| 592 | 592 |
| 593 // Only the block_file argument is relevant for what we want. | 593 // Only the block_file argument is relevant for what we want. |
| 594 Addr address(BLOCK_256, 1, new_file, 0); | 594 Addr address(BLOCK_256, 1, new_file, 0); |
| 595 return GetFile(address); | 595 return GetFile(address); |
| 596 } | 596 } |
| 597 | 597 |
| 598 int BlockFiles::CreateNextBlockFile(FileType block_type) { | 598 int16 BlockFiles::CreateNextBlockFile(FileType block_type) { |
| 599 for (int i = kFirstAdditionalBlockFile; i <= kMaxBlockFile; i++) { | 599 for (int16 i = kFirstAdditionalBlockFile; i <= kMaxBlockFile; i++) { |
| 600 if (CreateBlockFile(i, block_type, false)) | 600 if (CreateBlockFile(i, block_type, false)) |
| 601 return i; | 601 return i; |
| 602 } | 602 } |
| 603 return 0; | 603 return 0; |
| 604 } | 604 } |
| 605 | 605 |
| 606 // We walk the list of files for this particular block type, deleting the ones | 606 // We walk the list of files for this particular block type, deleting the ones |
| 607 // that are empty. | 607 // that are empty. |
| 608 bool BlockFiles::RemoveEmptyFile(FileType block_type) { | 608 bool BlockFiles::RemoveEmptyFile(FileType block_type) { |
| 609 MappedFile* file = block_files_[block_type - 1]; | 609 MappedFile* file = block_files_[block_type - 1]; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 } | 722 } |
| 723 | 723 |
| 724 base::FilePath BlockFiles::Name(int index) { | 724 base::FilePath BlockFiles::Name(int index) { |
| 725 // The file format allows for 256 files. | 725 // The file format allows for 256 files. |
| 726 DCHECK(index < 256 && index >= 0); | 726 DCHECK(index < 256 && index >= 0); |
| 727 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); | 727 std::string tmp = base::StringPrintf("%s%d", kBlockName, index); |
| 728 return path_.AppendASCII(tmp); | 728 return path_.AppendASCII(tmp); |
| 729 } | 729 } |
| 730 | 730 |
| 731 } // namespace disk_cache | 731 } // namespace disk_cache |
| OLD | NEW |