| 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/adapters.h" | 8 #include "base/containers/adapters.h" |
| 9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
| 10 #include "base/containers/mru_cache.h" | 10 #include "base/containers/mru_cache.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 AutoLock lock(lock_); | 44 AutoLock lock(lock_); |
| 45 soft_memory_limit_ = bytes; | 45 soft_memory_limit_ = bytes; |
| 46 } | 46 } |
| 47 | 47 |
| 48 void DiscardableMemoryManager::SetHardMemoryLimitExpirationTime( | 48 void DiscardableMemoryManager::SetHardMemoryLimitExpirationTime( |
| 49 TimeDelta hard_memory_limit_expiration_time) { | 49 TimeDelta hard_memory_limit_expiration_time) { |
| 50 AutoLock lock(lock_); | 50 AutoLock lock(lock_); |
| 51 hard_memory_limit_expiration_time_ = hard_memory_limit_expiration_time; | 51 hard_memory_limit_expiration_time_ = hard_memory_limit_expiration_time; |
| 52 } | 52 } |
| 53 | 53 |
| 54 void DiscardableMemoryManager::ReleaseFreeMemory() { | |
| 55 TRACE_EVENT0("base", "DiscardableMemoryManager::ReleaseFreeMemory"); | |
| 56 | |
| 57 AutoLock lock(lock_); | |
| 58 size_t bytes_allocated_before_releasing_memory = bytes_allocated_; | |
| 59 for (auto& entry : allocations_) { | |
| 60 Allocation* allocation = entry.first; | |
| 61 AllocationInfo* info = &entry.second; | |
| 62 | |
| 63 if (!info->purgable) | |
| 64 continue; | |
| 65 | |
| 66 // Skip if memory is still resident, otherwise purge and adjust | |
| 67 // |bytes_allocated_|. | |
| 68 if (allocation->IsMemoryResident()) | |
| 69 continue; | |
| 70 | |
| 71 size_t bytes_purgable = info->bytes; | |
| 72 DCHECK_LE(bytes_purgable, bytes_allocated_); | |
| 73 bytes_allocated_ -= bytes_purgable; | |
| 74 info->purgable = false; | |
| 75 allocation->Purge(); | |
| 76 } | |
| 77 | |
| 78 if (bytes_allocated_ != bytes_allocated_before_releasing_memory) | |
| 79 BytesAllocatedChanged(bytes_allocated_); | |
| 80 } | |
| 81 | |
| 82 bool DiscardableMemoryManager::ReduceMemoryUsage() { | 54 bool DiscardableMemoryManager::ReduceMemoryUsage() { |
| 83 return PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); | 55 return PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); |
| 84 } | 56 } |
| 85 | 57 |
| 86 void DiscardableMemoryManager::ReduceMemoryUsageUntilWithinLimit(size_t bytes) { | 58 void DiscardableMemoryManager::ReduceMemoryUsageUntilWithinLimit(size_t bytes) { |
| 87 AutoLock lock(lock_); | 59 AutoLock lock(lock_); |
| 88 PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired(Now(), | 60 PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired(Now(), |
| 89 bytes); | 61 bytes); |
| 90 } | 62 } |
| 91 | 63 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 base::debug::SetCrashKeyValue(kDiscardableMemoryUsageKey, | 209 base::debug::SetCrashKeyValue(kDiscardableMemoryUsageKey, |
| 238 Uint64ToString(new_bytes_allocated)); | 210 Uint64ToString(new_bytes_allocated)); |
| 239 } | 211 } |
| 240 | 212 |
| 241 TimeTicks DiscardableMemoryManager::Now() const { | 213 TimeTicks DiscardableMemoryManager::Now() const { |
| 242 return TimeTicks::Now(); | 214 return TimeTicks::Now(); |
| 243 } | 215 } |
| 244 | 216 |
| 245 } // namespace internal | 217 } // namespace internal |
| 246 } // namespace base | 218 } // namespace base |
| OLD | NEW |