| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_manager.h" | 5 #include "base/memory/discardable_memory_manager.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" |
| 11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 12 | 12 |
| 13 namespace base { | 13 namespace base { |
| 14 namespace internal { | 14 namespace internal { |
| 15 namespace { | |
| 16 | 15 |
| 17 // This is admittedly pretty magical. It's approximately enough memory for four | 16 DiscardableMemoryManager::DiscardableMemoryManager( |
| 18 // 2560x1600 images. | 17 size_t memory_limit, |
| 19 static const size_t kDefaultMemoryLimit = 64 * 1024 * 1024; | 18 size_t bytes_to_keep_under_moderate_pressure) |
| 20 static const size_t kDefaultBytesToKeepUnderModeratePressure = | |
| 21 kDefaultMemoryLimit / 4; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 DiscardableMemoryManager::DiscardableMemoryManager() | |
| 26 : allocations_(AllocationMap::NO_AUTO_EVICT), | 19 : allocations_(AllocationMap::NO_AUTO_EVICT), |
| 27 bytes_allocated_(0), | 20 bytes_allocated_(0), |
| 28 memory_limit_(kDefaultMemoryLimit), | 21 memory_limit_(memory_limit), |
| 29 bytes_to_keep_under_moderate_pressure_( | 22 bytes_to_keep_under_moderate_pressure_( |
| 30 kDefaultBytesToKeepUnderModeratePressure) { | 23 bytes_to_keep_under_moderate_pressure) { |
| 31 BytesAllocatedChanged(); | 24 BytesAllocatedChanged(); |
| 32 } | 25 } |
| 33 | 26 |
| 34 DiscardableMemoryManager::~DiscardableMemoryManager() { | 27 DiscardableMemoryManager::~DiscardableMemoryManager() { |
| 35 DCHECK(allocations_.empty()); | 28 DCHECK(allocations_.empty()); |
| 36 DCHECK_EQ(0u, bytes_allocated_); | 29 DCHECK_EQ(0u, bytes_allocated_); |
| 37 } | 30 } |
| 38 | 31 |
| 39 void DiscardableMemoryManager::RegisterMemoryPressureListener() { | 32 void DiscardableMemoryManager::RegisterMemoryPressureListener() { |
| 40 AutoLock lock(lock_); | 33 AutoLock lock(lock_); |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 void DiscardableMemoryManager::EnforcePolicyWithLockAcquired() { | 210 void DiscardableMemoryManager::EnforcePolicyWithLockAcquired() { |
| 218 PurgeLRUWithLockAcquiredUntilUsageIsWithin(memory_limit_); | 211 PurgeLRUWithLockAcquiredUntilUsageIsWithin(memory_limit_); |
| 219 } | 212 } |
| 220 | 213 |
| 221 void DiscardableMemoryManager::BytesAllocatedChanged() const { | 214 void DiscardableMemoryManager::BytesAllocatedChanged() const { |
| 222 TRACE_COUNTER_ID1("base", "DiscardableMemoryUsage", this, bytes_allocated_); | 215 TRACE_COUNTER_ID1("base", "DiscardableMemoryUsage", this, bytes_allocated_); |
| 223 } | 216 } |
| 224 | 217 |
| 225 } // namespace internal | 218 } // namespace internal |
| 226 } // namespace base | 219 } // namespace base |
| OLD | NEW |