Chromium Code Reviews| Index: net/disk_cache/simple/simple_index.cc |
| diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc |
| index c41c829bdefd808db4ea55dded03e54f4574b641..2b552a6d899520536c5bf4bbd6175fbf4911a5eb 100644 |
| --- a/net/disk_cache/simple/simple_index.cc |
| +++ b/net/disk_cache/simple/simple_index.cc |
| @@ -54,13 +54,16 @@ const uint32_t kBytesInKb = 1024; |
| namespace disk_cache { |
| EntryMetadata::EntryMetadata() |
| - : last_used_time_seconds_since_epoch_(0), |
| - entry_size_(0) { |
| -} |
| + : last_used_time_seconds_since_epoch_(0), |
| + entry_size_(0), |
| + memory_entry_data_(0) {} |
| EntryMetadata::EntryMetadata(base::Time last_used_time, |
| base::StrictNumeric<uint32_t> entry_size) |
| - : last_used_time_seconds_since_epoch_(0), entry_size_(entry_size) { |
| + : last_used_time_seconds_since_epoch_(0), |
| + entry_size_(0), |
|
gavinp
2017/08/04 18:42:54
Is this needed now? Removing it might actually imp
Maks Orlovich
2017/08/23 19:29:06
Not strictly, no. Probably the same fate should be
|
| + memory_entry_data_(0) { |
| + SetEntrySize(entry_size); // to round/pack properly. |
| SetLastUsedTime(last_used_time); |
| } |
| @@ -88,11 +91,12 @@ void EntryMetadata::SetLastUsedTime(const base::Time& last_used_time) { |
| } |
| uint32_t EntryMetadata::GetEntrySize() const { |
| - return entry_size_; |
| + return entry_size_ << 8; |
| } |
| void EntryMetadata::SetEntrySize(base::StrictNumeric<uint32_t> entry_size) { |
| - entry_size_ = entry_size; |
| + // This should not overflow since we limit entries to 1/8th of the cache. |
| + entry_size_ = (static_cast<uint32_t>(entry_size) + 255) >> 8; |
|
pasko
2017/06/29 16:31:49
would be nice to rename the member to something li
Maks Orlovich
2017/08/23 19:29:06
Done.
|
| } |
| void EntryMetadata::Serialize(base::Pickle* pickle) const { |
| @@ -100,11 +104,13 @@ void EntryMetadata::Serialize(base::Pickle* pickle) const { |
| int64_t internal_last_used_time = GetLastUsedTime().ToInternalValue(); |
| // If you modify the size of the size of the pickle, be sure to update |
| // kOnDiskSizeBytes. |
| + uint32_t packed_entry_info = (entry_size_ << 8) | memory_entry_data_; |
| pickle->WriteInt64(internal_last_used_time); |
| - pickle->WriteUInt64(entry_size_); |
| + pickle->WriteUInt64(packed_entry_info); |
| } |
| -bool EntryMetadata::Deserialize(base::PickleIterator* it) { |
| +bool EntryMetadata::Deserialize(base::PickleIterator* it, |
| + bool has_memory_entry_data) { |
| DCHECK(it); |
| int64_t tmp_last_used_time; |
| uint64_t tmp_entry_size; |
| @@ -112,7 +118,14 @@ bool EntryMetadata::Deserialize(base::PickleIterator* it) { |
| tmp_entry_size > std::numeric_limits<decltype(entry_size_)>::max()) |
| return false; |
| SetLastUsedTime(base::Time::FromInternalValue(tmp_last_used_time)); |
| - entry_size_ = static_cast<uint32_t>(tmp_entry_size); |
| + if (has_memory_entry_data) { |
| + // tmp_entry_size actually packs entry_size_ and memory_entry_data_. |
| + SetEntrySize(static_cast<uint32_t>(tmp_entry_size & 0xFFFFFF00)); |
| + SetMemoryEntryData(static_cast<uint8_t>(tmp_entry_size & 0xFF)); |
| + } else { |
| + SetEntrySize(static_cast<uint32_t>(tmp_entry_size)); |
| + SetMemoryEntryData(0); |
| + } |
| return true; |
| } |
| @@ -281,6 +294,22 @@ bool SimpleIndex::Has(uint64_t hash) const { |
| return !initialized_ || entries_set_.count(hash) > 0; |
| } |
| +uint8_t SimpleIndex::GetMemoryEntryData(uint64_t entry_hash) const { |
| + DCHECK(io_thread_checker_.CalledOnValidThread()); |
| + EntrySet::const_iterator it = entries_set_.find(entry_hash); |
| + if (it == entries_set_.end()) |
| + return 0; |
| + return it->second.GetMemoryEntryData(); |
| +} |
| + |
| +void SimpleIndex::SetMemoryEntryData(uint64_t entry_hash, uint8_t value) { |
| + DCHECK(io_thread_checker_.CalledOnValidThread()); |
| + EntrySet::iterator it = entries_set_.find(entry_hash); |
| + if (it == entries_set_.end()) |
| + return; |
| + return it->second.SetMemoryEntryData(value); |
| +} |
| + |
| bool SimpleIndex::UseIfExists(uint64_t entry_hash) { |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| // Always update the last used time, even if it is during initialization. |
| @@ -410,8 +439,9 @@ void SimpleIndex::UpdateEntryIteratorSize( |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| DCHECK_GE(cache_size_, (*it)->second.GetEntrySize()); |
| cache_size_ -= (*it)->second.GetEntrySize(); |
| - cache_size_ += static_cast<uint32_t>(entry_size); |
| (*it)->second.SetEntrySize(entry_size); |
| + // We use GetEntrySize to get consistent rounding. |
| + cache_size_ += (*it)->second.GetEntrySize(); |
| } |
| void SimpleIndex::MergeInitializingSet( |