OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/backend_impl.h" | 5 #include "net/disk_cache/backend_impl.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/histogram.h" | 8 #include "base/histogram.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 } | 348 } |
349 | 349 |
350 if (parent.get()) | 350 if (parent.get()) |
351 parent->SetNextAddress(entry_address); | 351 parent->SetNextAddress(entry_address); |
352 | 352 |
353 block_files_.GetFile(entry_address)->Store(cache_entry->entry()); | 353 block_files_.GetFile(entry_address)->Store(cache_entry->entry()); |
354 block_files_.GetFile(node_address)->Store(cache_entry->rankings()); | 354 block_files_.GetFile(node_address)->Store(cache_entry->rankings()); |
355 | 355 |
356 data_->header.num_entries++; | 356 data_->header.num_entries++; |
357 DCHECK(data_->header.num_entries > 0); | 357 DCHECK(data_->header.num_entries > 0); |
358 rankings_.Insert(cache_entry->rankings(), true); | 358 rankings_.Insert(cache_entry->rankings(), true, Rankings::NO_USE); |
359 if (!parent.get()) | 359 if (!parent.get()) |
360 data_->table[hash & mask_] = entry_address.value(); | 360 data_->table[hash & mask_] = entry_address.value(); |
361 | 361 |
362 DCHECK(entry); | 362 DCHECK(entry); |
363 *entry = NULL; | 363 *entry = NULL; |
364 cache_entry.swap(reinterpret_cast<EntryImpl**>(entry)); | 364 cache_entry.swap(reinterpret_cast<EntryImpl**>(entry)); |
365 | 365 |
366 UMA_HISTOGRAM_TIMES(L"DiskCache.CreateTime", Time::Now() - start); | 366 UMA_HISTOGRAM_TIMES(L"DiskCache.CreateTime", Time::Now() - start); |
367 stats_.OnEvent(Stats::CREATE_HIT); | 367 stats_.OnEvent(Stats::CREATE_HIT); |
368 Trace("create entry hit "); | 368 Trace("create entry hit "); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 } | 566 } |
567 | 567 |
568 void BackendImpl::DeleteBlock(Addr block_address, bool deep) { | 568 void BackendImpl::DeleteBlock(Addr block_address, bool deep) { |
569 block_files_.DeleteBlock(block_address, deep); | 569 block_files_.DeleteBlock(block_address, deep); |
570 } | 570 } |
571 | 571 |
572 LruData* BackendImpl::GetLruData() { | 572 LruData* BackendImpl::GetLruData() { |
573 return &data_->header.lru; | 573 return &data_->header.lru; |
574 } | 574 } |
575 | 575 |
576 void BackendImpl::UpdateRank(CacheRankingsBlock* node, bool modified) { | 576 void BackendImpl::UpdateRank(EntryImpl* entry, bool modified) { |
577 if (!read_only_) | 577 if (!read_only_) { |
578 rankings_.UpdateRank(node, modified); | 578 rankings_.UpdateRank(entry->rankings(), modified, Rankings::NO_USE); |
| 579 } |
579 } | 580 } |
580 | 581 |
581 void BackendImpl::RecoveredEntry(CacheRankingsBlock* rankings) { | 582 void BackendImpl::RecoveredEntry(CacheRankingsBlock* rankings) { |
582 Addr address(rankings->Data()->contents); | 583 Addr address(rankings->Data()->contents); |
583 EntryImpl* cache_entry = NULL; | 584 EntryImpl* cache_entry = NULL; |
584 bool dirty; | 585 bool dirty; |
585 if (NewEntry(address, &cache_entry, &dirty)) | 586 if (NewEntry(address, &cache_entry, &dirty)) |
586 return; | 587 return; |
587 | 588 |
588 uint32 hash = cache_entry->GetHash(); | 589 uint32 hash = cache_entry->GetHash(); |
589 cache_entry->Release(); | 590 cache_entry->Release(); |
590 | 591 |
591 // Anything on the table means that this entry is there. | 592 // Anything on the table means that this entry is there. |
592 if (data_->table[hash & mask_]) | 593 if (data_->table[hash & mask_]) |
593 return; | 594 return; |
594 | 595 |
595 data_->table[hash & mask_] = address.value(); | 596 data_->table[hash & mask_] = address.value(); |
596 } | 597 } |
597 | 598 |
598 void BackendImpl::InternalDoomEntry(EntryImpl* entry) { | 599 void BackendImpl::InternalDoomEntry(EntryImpl* entry) { |
599 uint32 hash = entry->GetHash(); | 600 uint32 hash = entry->GetHash(); |
600 std::string key = entry->GetKey(); | 601 std::string key = entry->GetKey(); |
601 EntryImpl* parent_entry = MatchEntry(key, hash, true); | 602 EntryImpl* parent_entry = MatchEntry(key, hash, true); |
602 CacheAddr child(entry->GetNextAddress()); | 603 CacheAddr child(entry->GetNextAddress()); |
603 | 604 |
604 Trace("Doom entry 0x%p", entry); | 605 Trace("Doom entry 0x%p", entry); |
605 | 606 |
606 rankings_.Remove(entry->rankings()); | 607 rankings_.Remove(entry->rankings(), Rankings::NO_USE); |
607 | 608 |
608 entry->InternalDoom(); | 609 entry->InternalDoom(); |
609 | 610 |
610 if (parent_entry) { | 611 if (parent_entry) { |
611 parent_entry->SetNextAddress(Addr(child)); | 612 parent_entry->SetNextAddress(Addr(child)); |
612 parent_entry->Release(); | 613 parent_entry->Release(); |
613 } else { | 614 } else { |
614 data_->table[hash & mask_] = child; | 615 data_->table[hash & mask_] = child; |
615 } | 616 } |
616 | 617 |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
964 } | 965 } |
965 | 966 |
966 // This is the actual implementation for OpenNextEntry and OpenPrevEntry. | 967 // This is the actual implementation for OpenNextEntry and OpenPrevEntry. |
967 bool BackendImpl::OpenFollowingEntry(bool forward, void** iter, | 968 bool BackendImpl::OpenFollowingEntry(bool forward, void** iter, |
968 Entry** next_entry) { | 969 Entry** next_entry) { |
969 if (disabled_) | 970 if (disabled_) |
970 return false; | 971 return false; |
971 | 972 |
972 Rankings::ScopedRankingsBlock rankings(&rankings_, | 973 Rankings::ScopedRankingsBlock rankings(&rankings_, |
973 reinterpret_cast<CacheRankingsBlock*>(*iter)); | 974 reinterpret_cast<CacheRankingsBlock*>(*iter)); |
974 CacheRankingsBlock* next_block = forward ? rankings_.GetNext(rankings.get()) : | 975 CacheRankingsBlock* next_block = forward ? |
975 rankings_.GetPrev(rankings.get()); | 976 rankings_.GetNext(rankings.get(), Rankings::NO_USE) : |
| 977 rankings_.GetPrev(rankings.get(), Rankings::NO_USE); |
976 Rankings::ScopedRankingsBlock next(&rankings_, next_block); | 978 Rankings::ScopedRankingsBlock next(&rankings_, next_block); |
977 *next_entry = NULL; | 979 *next_entry = NULL; |
978 *iter = NULL; | 980 *iter = NULL; |
979 if (!next.get()) | 981 if (!next.get()) |
980 return false; | 982 return false; |
981 | 983 |
982 scoped_refptr<EntryImpl> entry; | 984 scoped_refptr<EntryImpl> entry; |
983 if (next->Data()->pointer) { | 985 if (next->Data()->pointer) { |
984 entry = reinterpret_cast<EntryImpl*>(next->Data()->pointer); | 986 entry = reinterpret_cast<EntryImpl*>(next->Data()->pointer); |
985 } else { | 987 } else { |
(...skipping 25 matching lines...) Expand all Loading... |
1011 | 1013 |
1012 entry.swap(reinterpret_cast<EntryImpl**>(next_entry)); | 1014 entry.swap(reinterpret_cast<EntryImpl**>(next_entry)); |
1013 *iter = next.release(); | 1015 *iter = next.release(); |
1014 return true; | 1016 return true; |
1015 } | 1017 } |
1016 | 1018 |
1017 void BackendImpl::DestroyInvalidEntry(Addr address, EntryImpl* entry) { | 1019 void BackendImpl::DestroyInvalidEntry(Addr address, EntryImpl* entry) { |
1018 LOG(WARNING) << "Destroying invalid entry."; | 1020 LOG(WARNING) << "Destroying invalid entry."; |
1019 Trace("Destroying invalid entry 0x%p", entry); | 1021 Trace("Destroying invalid entry 0x%p", entry); |
1020 | 1022 |
1021 rankings_.Remove(entry->rankings()); | 1023 rankings_.Remove(entry->rankings(), Rankings::NO_USE); |
1022 entry->SetPointerForInvalidEntry(GetCurrentEntryId()); | 1024 entry->SetPointerForInvalidEntry(GetCurrentEntryId()); |
1023 | 1025 |
1024 entry->InternalDoom(); | 1026 entry->InternalDoom(); |
1025 | 1027 |
1026 data_->header.num_entries--; | 1028 data_->header.num_entries--; |
1027 DCHECK(data_->header.num_entries >= 0); | 1029 DCHECK(data_->header.num_entries >= 0); |
1028 stats_.OnEvent(Stats::INVALID_ENTRY); | 1030 stats_.OnEvent(Stats::INVALID_ENTRY); |
1029 } | 1031 } |
1030 | 1032 |
1031 void BackendImpl::TrimCache(bool empty) { | 1033 void BackendImpl::TrimCache(bool empty) { |
1032 Trace("*** Trim Cache ***"); | 1034 Trace("*** Trim Cache ***"); |
1033 if (disabled_) | 1035 if (disabled_) |
1034 return; | 1036 return; |
1035 | 1037 |
1036 Time start = Time::Now(); | 1038 Time start = Time::Now(); |
1037 Rankings::ScopedRankingsBlock node(&rankings_); | 1039 Rankings::ScopedRankingsBlock node(&rankings_); |
1038 Rankings::ScopedRankingsBlock next(&rankings_, rankings_.GetPrev(node.get())); | 1040 Rankings::ScopedRankingsBlock next(&rankings_, |
| 1041 rankings_.GetPrev(node.get(), Rankings::NO_USE)); |
1039 DCHECK(next.get()); | 1042 DCHECK(next.get()); |
1040 int target_size = empty ? 0 : LowWaterAdjust(max_size_); | 1043 int target_size = empty ? 0 : LowWaterAdjust(max_size_); |
1041 int deleted = 0; | 1044 int deleted = 0; |
1042 while (data_->header.num_bytes > target_size && next.get()) { | 1045 while (data_->header.num_bytes > target_size && next.get()) { |
1043 node.reset(next.release()); | 1046 node.reset(next.release()); |
1044 next.reset(rankings_.GetPrev(node.get())); | 1047 next.reset(rankings_.GetPrev(node.get(), Rankings::NO_USE)); |
1045 if (!node->Data()->pointer || empty) { | 1048 if (!node->Data()->pointer || empty) { |
1046 // This entry is not being used by anybody. | 1049 // This entry is not being used by anybody. |
1047 EntryImpl* entry; | 1050 EntryImpl* entry; |
1048 bool dirty; | 1051 bool dirty; |
1049 if (NewEntry(Addr(node->Data()->contents), &entry, &dirty)) { | 1052 if (NewEntry(Addr(node->Data()->contents), &entry, &dirty)) { |
1050 Trace("NewEntry failed on Trim 0x%x", node->address().value()); | 1053 Trace("NewEntry failed on Trim 0x%x", node->address().value()); |
1051 continue; | 1054 continue; |
1052 } | 1055 } |
1053 | 1056 |
1054 if (node->Data()->pointer) { | 1057 if (node->Data()->pointer) { |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1204 | 1207 |
1205 return num_dirty; | 1208 return num_dirty; |
1206 } | 1209 } |
1207 | 1210 |
1208 bool BackendImpl::CheckEntry(EntryImpl* cache_entry) { | 1211 bool BackendImpl::CheckEntry(EntryImpl* cache_entry) { |
1209 RankingsNode* rankings = cache_entry->rankings()->Data(); | 1212 RankingsNode* rankings = cache_entry->rankings()->Data(); |
1210 return !rankings->pointer; | 1213 return !rankings->pointer; |
1211 } | 1214 } |
1212 | 1215 |
1213 } // namespace disk_cache | 1216 } // namespace disk_cache |
OLD | NEW |