| 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 |
| 54 bool DiscardableMemoryManager::ReduceMemoryUsage() { | 82 bool DiscardableMemoryManager::ReduceMemoryUsage() { |
| 55 return PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); | 83 return PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit(); |
| 56 } | 84 } |
| 57 | 85 |
| 58 void DiscardableMemoryManager::ReduceMemoryUsageUntilWithinLimit(size_t bytes) { | 86 void DiscardableMemoryManager::ReduceMemoryUsageUntilWithinLimit(size_t bytes) { |
| 59 AutoLock lock(lock_); | 87 AutoLock lock(lock_); |
| 60 PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired(Now(), | 88 PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired(Now(), |
| 61 bytes); | 89 bytes); |
| 62 } | 90 } |
| 63 | 91 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 base::debug::SetCrashKeyValue(kDiscardableMemoryUsageKey, | 237 base::debug::SetCrashKeyValue(kDiscardableMemoryUsageKey, |
| 210 Uint64ToString(new_bytes_allocated)); | 238 Uint64ToString(new_bytes_allocated)); |
| 211 } | 239 } |
| 212 | 240 |
| 213 TimeTicks DiscardableMemoryManager::Now() const { | 241 TimeTicks DiscardableMemoryManager::Now() const { |
| 214 return TimeTicks::Now(); | 242 return TimeTicks::Now(); |
| 215 } | 243 } |
| 216 | 244 |
| 217 } // namespace internal | 245 } // namespace internal |
| 218 } // namespace base | 246 } // namespace base |
| OLD | NEW |