| 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/memory/mem_backend_impl.h" | 5 #include "net/disk_cache/memory/mem_backend_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/sys_info.h" | 8 #include "base/sys_info.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/disk_cache/cache_util.h" | 10 #include "net/disk_cache/cache_util.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 return cache.Pass(); | 50 return cache.Pass(); |
| 51 | 51 |
| 52 LOG(ERROR) << "Unable to create cache"; | 52 LOG(ERROR) << "Unable to create cache"; |
| 53 return nullptr; | 53 return nullptr; |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool MemBackendImpl::Init() { | 56 bool MemBackendImpl::Init() { |
| 57 if (max_size_) | 57 if (max_size_) |
| 58 return true; | 58 return true; |
| 59 | 59 |
| 60 int64 total_memory = base::SysInfo::AmountOfPhysicalMemory(); | 60 int64_t total_memory = base::SysInfo::AmountOfPhysicalMemory(); |
| 61 | 61 |
| 62 if (total_memory <= 0) { | 62 if (total_memory <= 0) { |
| 63 max_size_ = kDefaultInMemoryCacheSize; | 63 max_size_ = kDefaultInMemoryCacheSize; |
| 64 return true; | 64 return true; |
| 65 } | 65 } |
| 66 | 66 |
| 67 // We want to use up to 2% of the computer's memory, with a limit of 50 MB, | 67 // We want to use up to 2% of the computer's memory, with a limit of 50 MB, |
| 68 // reached on systemd with more than 2.5 GB of RAM. | 68 // reached on systemd with more than 2.5 GB of RAM. |
| 69 total_memory = total_memory * 2 / 100; | 69 total_memory = total_memory * 2 / 100; |
| 70 if (total_memory > kDefaultInMemoryCacheSize * 5) | 70 if (total_memory > kDefaultInMemoryCacheSize * 5) |
| 71 max_size_ = kDefaultInMemoryCacheSize * 5; | 71 max_size_ = kDefaultInMemoryCacheSize * 5; |
| 72 else | 72 else |
| 73 max_size_ = static_cast<int32>(total_memory); | 73 max_size_ = static_cast<int32_t>(total_memory); |
| 74 | 74 |
| 75 return true; | 75 return true; |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool MemBackendImpl::SetMaxSize(int max_bytes) { | 78 bool MemBackendImpl::SetMaxSize(int max_bytes) { |
| 79 static_assert(sizeof(max_bytes) == sizeof(max_size_), | 79 static_assert(sizeof(max_bytes) == sizeof(max_size_), |
| 80 "unsupported int model"); | 80 "unsupported int model"); |
| 81 if (max_bytes < 0) | 81 if (max_bytes < 0) |
| 82 return false; | 82 return false; |
| 83 | 83 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 100 else | 100 else |
| 101 NOTREACHED(); | 101 NOTREACHED(); |
| 102 | 102 |
| 103 entry->InternalDoom(); | 103 entry->InternalDoom(); |
| 104 } | 104 } |
| 105 | 105 |
| 106 void MemBackendImpl::UpdateRank(MemEntryImpl* node) { | 106 void MemBackendImpl::UpdateRank(MemEntryImpl* node) { |
| 107 rankings_.UpdateRank(node); | 107 rankings_.UpdateRank(node); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void MemBackendImpl::ModifyStorageSize(int32 old_size, int32 new_size) { | 110 void MemBackendImpl::ModifyStorageSize(int32_t old_size, int32_t new_size) { |
| 111 if (old_size >= new_size) | 111 if (old_size >= new_size) |
| 112 SubstractStorageSize(old_size - new_size); | 112 SubstractStorageSize(old_size - new_size); |
| 113 else | 113 else |
| 114 AddStorageSize(new_size - old_size); | 114 AddStorageSize(new_size - old_size); |
| 115 } | 115 } |
| 116 | 116 |
| 117 int MemBackendImpl::MaxFileSize() const { | 117 int MemBackendImpl::MaxFileSize() const { |
| 118 return max_size_ / 8; | 118 return max_size_ / 8; |
| 119 } | 119 } |
| 120 | 120 |
| 121 void MemBackendImpl::InsertIntoRankingList(MemEntryImpl* entry) { | 121 void MemBackendImpl::InsertIntoRankingList(MemEntryImpl* entry) { |
| 122 rankings_.Insert(entry); | 122 rankings_.Insert(entry); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void MemBackendImpl::RemoveFromRankingList(MemEntryImpl* entry) { | 125 void MemBackendImpl::RemoveFromRankingList(MemEntryImpl* entry) { |
| 126 rankings_.Remove(entry); | 126 rankings_.Remove(entry); |
| 127 } | 127 } |
| 128 | 128 |
| 129 net::CacheType MemBackendImpl::GetCacheType() const { | 129 net::CacheType MemBackendImpl::GetCacheType() const { |
| 130 return net::MEMORY_CACHE; | 130 return net::MEMORY_CACHE; |
| 131 } | 131 } |
| 132 | 132 |
| 133 int32 MemBackendImpl::GetEntryCount() const { | 133 int32_t MemBackendImpl::GetEntryCount() const { |
| 134 return static_cast<int32>(entries_.size()); | 134 return static_cast<int32_t>(entries_.size()); |
| 135 } | 135 } |
| 136 | 136 |
| 137 int MemBackendImpl::OpenEntry(const std::string& key, Entry** entry, | 137 int MemBackendImpl::OpenEntry(const std::string& key, Entry** entry, |
| 138 const CompletionCallback& callback) { | 138 const CompletionCallback& callback) { |
| 139 if (OpenEntry(key, entry)) | 139 if (OpenEntry(key, entry)) |
| 140 return net::OK; | 140 return net::OK; |
| 141 | 141 |
| 142 return net::ERR_FAILED; | 142 return net::ERR_FAILED; |
| 143 } | 143 } |
| 144 | 144 |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 MemEntryImpl* node = next; | 328 MemEntryImpl* node = next; |
| 329 next = rankings_.GetPrev(next); | 329 next = rankings_.GetPrev(next); |
| 330 if (!node->InUse() || empty) { | 330 if (!node->InUse() || empty) { |
| 331 node->Doom(); | 331 node->Doom(); |
| 332 } | 332 } |
| 333 } | 333 } |
| 334 | 334 |
| 335 return; | 335 return; |
| 336 } | 336 } |
| 337 | 337 |
| 338 void MemBackendImpl::AddStorageSize(int32 bytes) { | 338 void MemBackendImpl::AddStorageSize(int32_t bytes) { |
| 339 current_size_ += bytes; | 339 current_size_ += bytes; |
| 340 DCHECK_GE(current_size_, 0); | 340 DCHECK_GE(current_size_, 0); |
| 341 | 341 |
| 342 if (current_size_ > max_size_) | 342 if (current_size_ > max_size_) |
| 343 TrimCache(false); | 343 TrimCache(false); |
| 344 } | 344 } |
| 345 | 345 |
| 346 void MemBackendImpl::SubstractStorageSize(int32 bytes) { | 346 void MemBackendImpl::SubstractStorageSize(int32_t bytes) { |
| 347 current_size_ -= bytes; | 347 current_size_ -= bytes; |
| 348 DCHECK_GE(current_size_, 0); | 348 DCHECK_GE(current_size_, 0); |
| 349 } | 349 } |
| 350 | 350 |
| 351 } // namespace disk_cache | 351 } // namespace disk_cache |
| OLD | NEW |