| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/memory/discardable_memory_provider.h" | 5 #include "base/memory/discardable_memory_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/containers/hash_tables.h" | 8 #include "base/containers/hash_tables.h" |
| 9 #include "base/containers/mru_cache.h" | 9 #include "base/containers/mru_cache.h" |
| 10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 void DiscardableMemoryProvider::SetDiscardableMemoryLimit(size_t bytes) { | 79 void DiscardableMemoryProvider::SetDiscardableMemoryLimit(size_t bytes) { |
| 80 AutoLock lock(lock_); | 80 AutoLock lock(lock_); |
| 81 discardable_memory_limit_ = bytes; | 81 discardable_memory_limit_ = bytes; |
| 82 EnforcePolicyWithLockAcquired(); | 82 EnforcePolicyWithLockAcquired(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 void DiscardableMemoryProvider::SetBytesToReclaimUnderModeratePressure( | 85 void DiscardableMemoryProvider::SetBytesToReclaimUnderModeratePressure( |
| 86 size_t bytes) { | 86 size_t bytes) { |
| 87 AutoLock lock(lock_); | 87 AutoLock lock(lock_); |
| 88 bytes_to_reclaim_under_moderate_pressure_ = bytes; | 88 bytes_to_reclaim_under_moderate_pressure_ = bytes; |
| 89 EnforcePolicyWithLockAcquired(); | |
| 90 } | 89 } |
| 91 | 90 |
| 92 void DiscardableMemoryProvider::Register( | 91 void DiscardableMemoryProvider::Register( |
| 93 const DiscardableMemory* discardable, size_t bytes) { | 92 const DiscardableMemory* discardable, size_t bytes) { |
| 94 AutoLock lock(lock_); | 93 AutoLock lock(lock_); |
| 95 DCHECK(allocations_.Peek(discardable) == allocations_.end()); | 94 DCHECK(allocations_.Peek(discardable) == allocations_.end()); |
| 96 allocations_.Put(discardable, Allocation(bytes)); | 95 allocations_.Put(discardable, Allocation(bytes)); |
| 97 } | 96 } |
| 98 | 97 |
| 99 void DiscardableMemoryProvider::Unregister( | 98 void DiscardableMemoryProvider::Unregister( |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 return bytes_allocated_; | 183 return bytes_allocated_; |
| 185 } | 184 } |
| 186 | 185 |
| 187 void DiscardableMemoryProvider::Purge() { | 186 void DiscardableMemoryProvider::Purge() { |
| 188 AutoLock lock(lock_); | 187 AutoLock lock(lock_); |
| 189 | 188 |
| 190 if (bytes_to_reclaim_under_moderate_pressure_ == 0) | 189 if (bytes_to_reclaim_under_moderate_pressure_ == 0) |
| 191 return; | 190 return; |
| 192 | 191 |
| 193 size_t limit = 0; | 192 size_t limit = 0; |
| 194 if (bytes_to_reclaim_under_moderate_pressure_ < discardable_memory_limit_) | 193 if (bytes_to_reclaim_under_moderate_pressure_ < bytes_allocated_) |
| 195 limit = bytes_allocated_ - bytes_to_reclaim_under_moderate_pressure_; | 194 limit = bytes_allocated_ - bytes_to_reclaim_under_moderate_pressure_; |
| 196 | 195 |
| 197 PurgeLRUWithLockAcquiredUntilUsageIsWithin(limit); | 196 PurgeLRUWithLockAcquiredUntilUsageIsWithin(limit); |
| 198 } | 197 } |
| 199 | 198 |
| 200 void DiscardableMemoryProvider::PurgeLRUWithLockAcquiredUntilUsageIsWithin( | 199 void DiscardableMemoryProvider::PurgeLRUWithLockAcquiredUntilUsageIsWithin( |
| 201 size_t limit) { | 200 size_t limit) { |
| 202 TRACE_EVENT1( | 201 TRACE_EVENT1( |
| 203 "base", | 202 "base", |
| 204 "DiscardableMemoryProvider::PurgeLRUWithLockAcquiredUntilUsageIsWithin", | 203 "DiscardableMemoryProvider::PurgeLRUWithLockAcquiredUntilUsageIsWithin", |
| (...skipping 11 matching lines...) Expand all Loading... |
| 216 | 215 |
| 217 size_t bytes = it->second.bytes; | 216 size_t bytes = it->second.bytes; |
| 218 DCHECK_LE(bytes, bytes_allocated_); | 217 DCHECK_LE(bytes, bytes_allocated_); |
| 219 bytes_allocated_ -= bytes; | 218 bytes_allocated_ -= bytes; |
| 220 free(it->second.memory); | 219 free(it->second.memory); |
| 221 it->second.memory = NULL; | 220 it->second.memory = NULL; |
| 222 } | 221 } |
| 223 } | 222 } |
| 224 | 223 |
| 225 void DiscardableMemoryProvider::EnforcePolicyWithLockAcquired() { | 224 void DiscardableMemoryProvider::EnforcePolicyWithLockAcquired() { |
| 226 lock_.AssertAcquired(); | 225 PurgeLRUWithLockAcquiredUntilUsageIsWithin(discardable_memory_limit_); |
| 227 | |
| 228 bool exceeded_bound = bytes_allocated_ > discardable_memory_limit_; | |
| 229 if (!exceeded_bound || !bytes_to_reclaim_under_moderate_pressure_) | |
| 230 return; | |
| 231 | |
| 232 size_t limit = 0; | |
| 233 if (bytes_to_reclaim_under_moderate_pressure_ < discardable_memory_limit_) | |
| 234 limit = bytes_allocated_ - bytes_to_reclaim_under_moderate_pressure_; | |
| 235 | |
| 236 PurgeLRUWithLockAcquiredUntilUsageIsWithin(limit); | |
| 237 } | 226 } |
| 238 | 227 |
| 239 } // namespace internal | 228 } // namespace internal |
| 240 } // namespace base | 229 } // namespace base |
| OLD | NEW |