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/backend_impl.h" | 5 #include "net/disk_cache/backend_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 block_files_.DeleteBlock(entry_address, false); | 600 block_files_.DeleteBlock(entry_address, false); |
601 block_files_.DeleteBlock(node_address, false); | 601 block_files_.DeleteBlock(node_address, false); |
602 LOG(ERROR) << "Create entry failed " << key.c_str(); | 602 LOG(ERROR) << "Create entry failed " << key.c_str(); |
603 stats_.OnEvent(Stats::CREATE_ERROR); | 603 stats_.OnEvent(Stats::CREATE_ERROR); |
604 return NULL; | 604 return NULL; |
605 } | 605 } |
606 | 606 |
607 cache_entry->BeginLogging(net_log_, true); | 607 cache_entry->BeginLogging(net_log_, true); |
608 | 608 |
609 // We are not failing the operation; let's add this to the map. | 609 // We are not failing the operation; let's add this to the map. |
610 open_entries_[entry_address.value()] = cache_entry; | 610 open_entries_[entry_address.value()] = cache_entry.get(); |
611 | 611 |
612 // Save the entry. | 612 // Save the entry. |
613 cache_entry->entry()->Store(); | 613 cache_entry->entry()->Store(); |
614 cache_entry->rankings()->Store(); | 614 cache_entry->rankings()->Store(); |
615 IncreaseNumEntries(); | 615 IncreaseNumEntries(); |
616 entry_count_++; | 616 entry_count_++; |
617 | 617 |
618 // Link this entry through the index. | 618 // Link this entry through the index. |
619 if (parent.get()) { | 619 if (parent.get()) { |
620 parent->SetNextAddress(entry_address); | 620 parent->SetNextAddress(entry_address); |
621 } else { | 621 } else { |
622 data_->table[hash & mask_] = entry_address.value(); | 622 data_->table[hash & mask_] = entry_address.value(); |
623 } | 623 } |
624 | 624 |
625 // Link this entry through the lists. | 625 // Link this entry through the lists. |
626 eviction_.OnCreateEntry(cache_entry); | 626 eviction_.OnCreateEntry(cache_entry.get()); |
627 | 627 |
628 CACHE_UMA(AGE_MS, "CreateTime", 0, start); | 628 CACHE_UMA(AGE_MS, "CreateTime", 0, start); |
629 stats_.OnEvent(Stats::CREATE_HIT); | 629 stats_.OnEvent(Stats::CREATE_HIT); |
630 SIMPLE_STATS_COUNTER("disk_cache.miss"); | 630 SIMPLE_STATS_COUNTER("disk_cache.miss"); |
631 Trace("create entry hit "); | 631 Trace("create entry hit "); |
632 FlushIndex(); | 632 FlushIndex(); |
633 cache_entry->AddRef(); | 633 cache_entry->AddRef(); |
634 return cache_entry.get(); | 634 return cache_entry.get(); |
635 } | 635 } |
636 | 636 |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1139 LOG(ERROR) << "Number of entries mismatch"; | 1139 LOG(ERROR) << "Number of entries mismatch"; |
1140 #if !defined(NET_BUILD_STRESS_CACHE) | 1140 #if !defined(NET_BUILD_STRESS_CACHE) |
1141 return ERR_NUM_ENTRIES_MISMATCH; | 1141 return ERR_NUM_ENTRIES_MISMATCH; |
1142 #endif | 1142 #endif |
1143 } | 1143 } |
1144 | 1144 |
1145 return CheckAllEntries(); | 1145 return CheckAllEntries(); |
1146 } | 1146 } |
1147 | 1147 |
1148 void BackendImpl::FlushIndex() { | 1148 void BackendImpl::FlushIndex() { |
1149 if (index_ && !disabled_) | 1149 if (index_.get() && !disabled_) |
1150 index_->Flush(); | 1150 index_->Flush(); |
1151 } | 1151 } |
1152 | 1152 |
1153 // ------------------------------------------------------------------------ | 1153 // ------------------------------------------------------------------------ |
1154 | 1154 |
1155 net::CacheType BackendImpl::GetCacheType() const { | 1155 net::CacheType BackendImpl::GetCacheType() const { |
1156 return cache_type_; | 1156 return cache_type_; |
1157 } | 1157 } |
1158 | 1158 |
1159 int32 BackendImpl::GetEntryCount() const { | 1159 int32 BackendImpl::GetEntryCount() const { |
1160 if (!index_ || disabled_) | 1160 if (!index_.get() || disabled_) |
1161 return 0; | 1161 return 0; |
1162 // num_entries includes entries already evicted. | 1162 // num_entries includes entries already evicted. |
1163 int32 not_deleted = data_->header.num_entries - | 1163 int32 not_deleted = data_->header.num_entries - |
1164 data_->header.lru.sizes[Rankings::DELETED]; | 1164 data_->header.lru.sizes[Rankings::DELETED]; |
1165 | 1165 |
1166 if (not_deleted < 0) { | 1166 if (not_deleted < 0) { |
1167 NOTREACHED(); | 1167 NOTREACHED(); |
1168 not_deleted = 0; | 1168 not_deleted = 0; |
1169 } | 1169 } |
1170 | 1170 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1287 base::PLATFORM_FILE_OPEN_ALWAYS | | 1287 base::PLATFORM_FILE_OPEN_ALWAYS | |
1288 base::PLATFORM_FILE_EXCLUSIVE_WRITE; | 1288 base::PLATFORM_FILE_EXCLUSIVE_WRITE; |
1289 scoped_refptr<disk_cache::File> file(new disk_cache::File( | 1289 scoped_refptr<disk_cache::File> file(new disk_cache::File( |
1290 base::CreatePlatformFile(index_name, flags, file_created, NULL))); | 1290 base::CreatePlatformFile(index_name, flags, file_created, NULL))); |
1291 | 1291 |
1292 if (!file->IsValid()) | 1292 if (!file->IsValid()) |
1293 return false; | 1293 return false; |
1294 | 1294 |
1295 bool ret = true; | 1295 bool ret = true; |
1296 if (*file_created) | 1296 if (*file_created) |
1297 ret = CreateBackingStore(file); | 1297 ret = CreateBackingStore(file.get()); |
1298 | 1298 |
1299 file = NULL; | 1299 file = NULL; |
1300 if (!ret) | 1300 if (!ret) |
1301 return false; | 1301 return false; |
1302 | 1302 |
1303 index_ = new MappedFile(); | 1303 index_ = new MappedFile(); |
1304 data_ = reinterpret_cast<Index*>(index_->Init(index_name, 0)); | 1304 data_ = reinterpret_cast<Index*>(index_->Init(index_name, 0)); |
1305 if (!data_) { | 1305 if (!data_) { |
1306 LOG(ERROR) << "Unable to map Index file"; | 1306 LOG(ERROR) << "Unable to map Index file"; |
1307 return false; | 1307 return false; |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1517 } | 1517 } |
1518 | 1518 |
1519 // Prevent overwriting the dirty flag on the destructor. | 1519 // Prevent overwriting the dirty flag on the destructor. |
1520 cache_entry->SetDirtyFlag(GetCurrentEntryId()); | 1520 cache_entry->SetDirtyFlag(GetCurrentEntryId()); |
1521 | 1521 |
1522 if (cache_entry->dirty()) { | 1522 if (cache_entry->dirty()) { |
1523 Trace("Dirty entry 0x%p 0x%x", reinterpret_cast<void*>(cache_entry.get()), | 1523 Trace("Dirty entry 0x%p 0x%x", reinterpret_cast<void*>(cache_entry.get()), |
1524 address.value()); | 1524 address.value()); |
1525 } | 1525 } |
1526 | 1526 |
1527 open_entries_[address.value()] = cache_entry; | 1527 open_entries_[address.value()] = cache_entry.get(); |
1528 | 1528 |
1529 cache_entry->BeginLogging(net_log_, false); | 1529 cache_entry->BeginLogging(net_log_, false); |
1530 cache_entry.swap(entry); | 1530 cache_entry.swap(entry); |
1531 return 0; | 1531 return 0; |
1532 } | 1532 } |
1533 | 1533 |
1534 EntryImpl* BackendImpl::MatchEntry(const std::string& key, uint32 hash, | 1534 EntryImpl* BackendImpl::MatchEntry(const std::string& key, uint32 hash, |
1535 bool find_parent, Addr entry_addr, | 1535 bool find_parent, Addr entry_addr, |
1536 bool* match_error) { | 1536 bool* match_error) { |
1537 Addr address(data_->table[hash & mask_]); | 1537 Addr address(data_->table[hash & mask_]); |
(...skipping 25 matching lines...) Expand all Loading... |
1563 int error = NewEntry(address, &tmp); | 1563 int error = NewEntry(address, &tmp); |
1564 cache_entry.swap(&tmp); | 1564 cache_entry.swap(&tmp); |
1565 | 1565 |
1566 if (error || cache_entry->dirty()) { | 1566 if (error || cache_entry->dirty()) { |
1567 // This entry is dirty on disk (it was not properly closed): we cannot | 1567 // This entry is dirty on disk (it was not properly closed): we cannot |
1568 // trust it. | 1568 // trust it. |
1569 Addr child(0); | 1569 Addr child(0); |
1570 if (!error) | 1570 if (!error) |
1571 child.set_value(cache_entry->GetNextAddress()); | 1571 child.set_value(cache_entry->GetNextAddress()); |
1572 | 1572 |
1573 if (parent_entry) { | 1573 if (parent_entry.get()) { |
1574 parent_entry->SetNextAddress(child); | 1574 parent_entry->SetNextAddress(child); |
1575 parent_entry = NULL; | 1575 parent_entry = NULL; |
1576 } else { | 1576 } else { |
1577 data_->table[hash & mask_] = child.value(); | 1577 data_->table[hash & mask_] = child.value(); |
1578 } | 1578 } |
1579 | 1579 |
1580 Trace("MatchEntry dirty %d 0x%x 0x%x", find_parent, entry_addr.value(), | 1580 Trace("MatchEntry dirty %d 0x%x 0x%x", find_parent, entry_addr.value(), |
1581 address.value()); | 1581 address.value()); |
1582 | 1582 |
1583 if (!error) { | 1583 if (!error) { |
1584 // It is important to call DestroyInvalidEntry after removing this | 1584 // It is important to call DestroyInvalidEntry after removing this |
1585 // entry from the table. | 1585 // entry from the table. |
1586 DestroyInvalidEntry(cache_entry); | 1586 DestroyInvalidEntry(cache_entry.get()); |
1587 cache_entry = NULL; | 1587 cache_entry = NULL; |
1588 } else { | 1588 } else { |
1589 Trace("NewEntry failed on MatchEntry 0x%x", address.value()); | 1589 Trace("NewEntry failed on MatchEntry 0x%x", address.value()); |
1590 } | 1590 } |
1591 | 1591 |
1592 // Restart the search. | 1592 // Restart the search. |
1593 address.set_value(data_->table[hash & mask_]); | 1593 address.set_value(data_->table[hash & mask_]); |
1594 visited.clear(); | 1594 visited.clear(); |
1595 continue; | 1595 continue; |
1596 } | 1596 } |
1597 | 1597 |
1598 DCHECK_EQ(hash & mask_, cache_entry->entry()->Data()->hash & mask_); | 1598 DCHECK_EQ(hash & mask_, cache_entry->entry()->Data()->hash & mask_); |
1599 if (cache_entry->IsSameEntry(key, hash)) { | 1599 if (cache_entry->IsSameEntry(key, hash)) { |
1600 if (!cache_entry->Update()) | 1600 if (!cache_entry->Update()) |
1601 cache_entry = NULL; | 1601 cache_entry = NULL; |
1602 found = true; | 1602 found = true; |
1603 if (find_parent && entry_addr.value() != address.value()) { | 1603 if (find_parent && entry_addr.value() != address.value()) { |
1604 Trace("Entry not on the index 0x%x", address.value()); | 1604 Trace("Entry not on the index 0x%x", address.value()); |
1605 *match_error = true; | 1605 *match_error = true; |
1606 parent_entry = NULL; | 1606 parent_entry = NULL; |
1607 } | 1607 } |
1608 break; | 1608 break; |
1609 } | 1609 } |
1610 if (!cache_entry->Update()) | 1610 if (!cache_entry->Update()) |
1611 cache_entry = NULL; | 1611 cache_entry = NULL; |
1612 parent_entry = cache_entry; | 1612 parent_entry = cache_entry; |
1613 cache_entry = NULL; | 1613 cache_entry = NULL; |
1614 if (!parent_entry) | 1614 if (!parent_entry.get()) |
1615 break; | 1615 break; |
1616 | 1616 |
1617 address.set_value(parent_entry->GetNextAddress()); | 1617 address.set_value(parent_entry->GetNextAddress()); |
1618 } | 1618 } |
1619 | 1619 |
1620 if (parent_entry && (!find_parent || !found)) | 1620 if (parent_entry.get() && (!find_parent || !found)) |
1621 parent_entry = NULL; | 1621 parent_entry = NULL; |
1622 | 1622 |
1623 if (find_parent && entry_addr.is_initialized() && !cache_entry) { | 1623 if (find_parent && entry_addr.is_initialized() && !cache_entry.get()) { |
1624 *match_error = true; | 1624 *match_error = true; |
1625 parent_entry = NULL; | 1625 parent_entry = NULL; |
1626 } | 1626 } |
1627 | 1627 |
1628 if (cache_entry && (find_parent || !found)) | 1628 if (cache_entry.get() && (find_parent || !found)) |
1629 cache_entry = NULL; | 1629 cache_entry = NULL; |
1630 | 1630 |
1631 find_parent ? parent_entry.swap(&tmp) : cache_entry.swap(&tmp); | 1631 find_parent ? parent_entry.swap(&tmp) : cache_entry.swap(&tmp); |
1632 FlushIndex(); | 1632 FlushIndex(); |
1633 return tmp; | 1633 return tmp; |
1634 } | 1634 } |
1635 | 1635 |
1636 // This is the actual implementation for OpenNextEntry and OpenPrevEntry. | 1636 // This is the actual implementation for OpenNextEntry and OpenPrevEntry. |
1637 EntryImpl* BackendImpl::OpenFollowingEntry(bool forward, void** iter) { | 1637 EntryImpl* BackendImpl::OpenFollowingEntry(bool forward, void** iter) { |
1638 if (disabled_) | 1638 if (disabled_) |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2105 if (total_memory > kMaxBuffersSize || total_memory <= 0) | 2105 if (total_memory > kMaxBuffersSize || total_memory <= 0) |
2106 total_memory = kMaxBuffersSize; | 2106 total_memory = kMaxBuffersSize; |
2107 | 2107 |
2108 done = true; | 2108 done = true; |
2109 } | 2109 } |
2110 | 2110 |
2111 return static_cast<int>(total_memory); | 2111 return static_cast<int>(total_memory); |
2112 } | 2112 } |
2113 | 2113 |
2114 } // namespace disk_cache | 2114 } // namespace disk_cache |
OLD | NEW |